name: Run unlighthouse-tests [Downstream pipeline]
run-name: Run unlighthouse-tests [Downstream pipeline] for version ${{ inputs.containertag }}
on:
  workflow_dispatch:
    inputs:
      containertag:
        description: "Tag of the container to test"
        default: "preview"
        type: "string"

jobs:
  unlighthouse:
    runs-on: docker
    container: 
      image: forgejo.neshweb.net/ci-docker-images/unlighthouse:0.4.1
    services:
      website:
        image: forgejo.neshweb.net/firq/firq-dev-website:${{ inputs.containertag }}
        options: >-
          --hostname website
    steps:
      - name: Checkout repository
        uses: https://code.forgejo.org/actions/checkout@v3
      - name: Check availability
        run: |
          while [ "$(curl -o /dev/null -s -w '%{http_code}' http://website:8081)" -ne 200 ];
            do echo "Waiting...";
            sleep 5;
          done;
      - name: Run unlighthouse
        run: unlighthouse-ci --site "http://website:8081"
      - name: Replace URLs
        run: find ./unlighthouse-reports -type f | xargs sed -i "s|http://website:8081|https://preview.firq.dev|g";
      - name: Prepare artifacts
        run: cp serve.json unlighthouse-reports
      - name: Upload reports
        uses: actions/upload-artifact@v3
        with:
          name: unlighthouse-reports
          path: unlighthouse-reports/
      - name: Upload Dockerfile
        uses: actions/upload-artifact@v3
        with:
          name: dockerfile
          path: Dockerfile.reports

  build-site:
    needs: [ unlighthouse ]
    if: success()
    runs-on: dind
    steps:
      - name: Downloading static site artifacts
        uses: actions/download-artifact@v3
        with:
          name: dockerfile
      - name: Downloading static site artifacts
        uses: actions/download-artifact@v3
        with:
          name: unlighthouse-reports
          path: reports
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Log into Docker Package Registry
        uses: docker/login-action@v3
        with:
          registry: forgejo.neshweb.net
          username: ${{ secrets.FORGEJO_USERNAME }}
          password: ${{ secrets.FORGEJO_TOKEN }}
      - name: Build and push to Docker Package Registry
        uses: docker/build-push-action@v5
        with:
          context: .
          file: Dockerfile.reports
          push: true
          tags: forgejo.neshweb.net/firq/firq-dev-website-unlighthouse:latest

  auto-deploy-dockge:
    needs: [ build-site ]
    if: success()
    runs-on: docker
    container: forgejo.neshweb.net/firq/dockge-cli:0.1.2
    steps:
      - name: Configure Dockge CLI
        run: |
          dockge host '${{ vars.DOCKGE_HOST }}'
          dockge login --user '${{ secrets.DOCKGE_USER }}' --password '${{ secrets.DOCKGE_CREDENTIAL }}'
      - name: Check status and redeploy
        run: |
          dockge status firq-dev-unlighthouse
          dockge update firq-dev-unlighthouse
          dockge status firq-dev-unlighthouse