name: build
on:
  - push
  - pull_request
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@focs
      - name: Display Python3 version
        run: python3 --version
      - name: Install PDM
        run: |
          echo curl
          curl -sSL https://ghfast.top/https://raw.githubusercontent.com/pdm-project/pdm/main/install-pdm.py -o install-pdm.py
          echo sed
          sed -i 's|https://github.com|https://ghfast.top/https://github.com|g' install-pdm.py
          echo python3
          python3 install-pdm.py -p /usr
      - name: Display PDM version
        run: pdm --version
      - name: PDM install dependencies
        run: |
          pdm install
      - name: Lint
        run: |
          pdm run lint
      - name: Run
        run: |
          pdm run app --help
  trigger-build-image:
    runs-on: ubuntu-latest
    needs: build
    if: gitea.ref == 'refs/heads/master'
    steps:
      - name: Set up SSH
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519.pub
          chmod 600 ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519.pub
          ssh-keyscan -p 2222 focs.ji.sjtu.edu.cn >> ~/.ssh/known_hosts
          ssh -T git@focs.ji.sjtu.edu.cn -p 2222
      - name: Set up Git
        run: |
          git config --global user.name "bot-joj"
          git config --global user.email "bot-joj@focs.ji.sjtu.edu.cn"
      - name: Clone, Commit and Push
        shell: bash
        run: |
          git clone ssh://git@focs.ji.sjtu.edu.cn:2222/JOJ/runner-images.git runner-images
          cd runner-images
          git submodule update --init --remote --force
          git add -A
          git commit --allow-empty -m "chore: ${{ gitea.repository }}@${{ gitea.sha }} trigger force build gitea actions"
          git push