diff --git a/.github/workflows/preview-fork-deploy.yml b/.github/workflows/preview-fork-deploy.yml index ff2978bc..4711e012 100644 --- a/.github/workflows/preview-fork-deploy.yml +++ b/.github/workflows/preview-fork-deploy.yml @@ -15,10 +15,29 @@ jobs: if: > github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' && - github.event.workflow_run.pull_requests[0].head.repo.id != github.event.workflow_run.repository.id + github.event.workflow_run.head_repository.id != github.event.repository.id runs-on: ubuntu-latest steps: + # workflow_run.pull_requests may be empty for fork PRs. Verify the + # source repository and exact HEAD before publishing untrusted HTML. + - name: Resolve PR number from completed fork build + id: pr + env: + GH_TOKEN: ${{ github.token }} + SOURCE_REPOSITORY: ${{ github.event.workflow_run.head_repository.full_name }} + SOURCE_BRANCH: ${{ github.event.workflow_run.head_branch }} + SOURCE_SHA: ${{ github.event.workflow_run.head_sha }} + run: | + set -euo pipefail + ref="${SOURCE_REPOSITORY%%/*}:$SOURCE_BRANCH" + gh pr view "$ref" -R "$GITHUB_REPOSITORY" \ + --json number,state,baseRefName,headRefOid,headRepository | + jq -er --arg sha "$SOURCE_SHA" --arg repo "$SOURCE_REPOSITORY" \ + 'select(.state == "OPEN" and .baseRefName == "main" and + .headRefOid == $sha and .headRepository.nameWithOwner == $repo) | + "number=\(.number)"' >> "$GITHUB_OUTPUT" + - name: Download preview artifact uses: actions/download-artifact@v4 with: @@ -27,14 +46,18 @@ jobs: run-id: ${{ github.event.workflow_run.id }} github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Check downloaded preview + run: test -s build_production/index.html + - name: Deploy preview uses: LibreSign/pr-preview-action@2a6da1177b47dc23173ba9a75e01da0a0d800978 with: action: deploy source-dir: ./build_production/ - pr-number: ${{ github.event.workflow_run.pull_requests[0].number }} + pr-number: ${{ steps.pr.outputs.number }} pages-base-url: libresign.github.io/site-preview deploy-repository: LibreSign/site-preview preview-branch: gh-pages - wait-for-pages-deployment: true + # The shared Pages branch can be published by another PR concurrently. + wait-for-pages-deployment: false token: ${{ secrets.PREVIEW_TOKEN }} diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index b9551dcb..dc8fbdc0 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -64,5 +64,6 @@ jobs: pages-base-url: libresign.github.io/site-preview deploy-repository: LibreSign/site-preview preview-branch: gh-pages - wait-for-pages-deployment: true + # Another PR can supersede this Pages build on the shared repository. + wait-for-pages-deployment: false token: ${{ secrets.PREVIEW_TOKEN }}