From d0054d2cbab92bf8c138f67307230320b97741cf Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Wed, 11 Nov 2026 03:06:56 +0000 Subject: [PATCH 1/3] fix(ci): verify live preview instead of superseded Pages builds Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .github/scripts/verify-preview-publication.sh | 44 +++++++++++++++++++ .github/workflows/preview-fork-deploy.yml | 15 ++++++- .github/workflows/preview.yml | 16 ++++++- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 .github/scripts/verify-preview-publication.sh diff --git a/.github/scripts/verify-preview-publication.sh b/.github/scripts/verify-preview-publication.sh new file mode 100644 index 00000000..99094cc7 --- /dev/null +++ b/.github/scripts/verify-preview-publication.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# Check the published revision, not an individual GitHub Pages workflow run. +# Shared Pages deployments may cancel each other while still publishing every PR. +set -euo pipefail + +if [ "$#" -ne 3 ]; then + echo "Usage: $0 PREVIEW_URL EXPECTED_SHA REVISION_FILE" >&2 + exit 2 +fi + +preview_url="${1%/}" +expected_sha="$2" +revision_file="$3" + +if [[ ! "$preview_url" =~ ^https://libresign\.github\.io/site-preview/pr-preview/pr-[0-9]+$ ]]; then + echo "::error::Invalid preview URL: $preview_url" >&2 + exit 2 +fi +if [[ ! "$expected_sha" =~ ^[0-9a-f]{40}$ ]]; then + echo "::error::Invalid expected commit SHA" >&2 + exit 2 +fi +if [ ! -s "$revision_file" ] || [ "$(cat "$revision_file")" != "$expected_sha" ]; then + echo "::error::Preview artifact does not match the PR revision" >&2 + exit 1 +fi + +max_attempts="${PREVIEW_CHECK_ATTEMPTS:-60}" +poll_seconds="${PREVIEW_CHECK_INTERVAL:-5}" +for ((attempt = 1; attempt <= max_attempts; attempt++)); do + if served_sha="$(curl -fsSL --max-time 15 --proto '=https' --proto-redir '=https' \ + -H 'Cache-Control: no-cache' \ + "$preview_url/preview-revision.txt?sha=$expected_sha&attempt=$attempt" 2>/dev/null)" && + [ "$served_sha" = "$expected_sha" ]; then + echo "Published preview matches $expected_sha: $preview_url/" + exit 0 + fi + if ((attempt < max_attempts)); then + sleep "$poll_seconds" + fi +done + +echo "::error::GitHub Pages did not serve revision $expected_sha at $preview_url/" >&2 +exit 1 diff --git a/.github/workflows/preview-fork-deploy.yml b/.github/workflows/preview-fork-deploy.yml index ff2978bc..37662eaf 100644 --- a/.github/workflows/preview-fork-deploy.yml +++ b/.github/workflows/preview-fork-deploy.yml @@ -19,6 +19,12 @@ jobs: runs-on: ubuntu-latest steps: + - name: Check out trusted preview verification script + uses: actions/checkout@v4 + with: + ref: ${{ github.event.repository.default_branch }} + persist-credentials: false + - name: Download preview artifact uses: actions/download-artifact@v4 with: @@ -36,5 +42,12 @@ jobs: 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 }} + + - name: Verify published preview revision + env: + PREVIEW_URL: https://libresign.github.io/site-preview/pr-preview/pr-${{ github.event.workflow_run.pull_requests[0].number }} + EXPECTED_SHA: ${{ github.event.workflow_run.head_sha }} + run: bash .github/scripts/verify-preview-publication.sh "$PREVIEW_URL" "$EXPECTED_SHA" build_production/preview-revision.txt diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index b9551dcb..cbfe11de 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -46,6 +46,11 @@ jobs: WC_CONSUMER_SECRET: ${{ secrets.WC_CONSUMER_SECRET }} run: composer prod + - name: Stamp build with its PR head revision + env: + EXPECTED_SHA: ${{ github.event.pull_request.head.sha }} + run: printf '%s\n' "$EXPECTED_SHA" > build_production/preview-revision.txt + - name: Upload fork preview artifact if: github.event.pull_request.head.repo.full_name != github.repository uses: actions/upload-artifact@v4 @@ -64,5 +69,14 @@ jobs: pages-base-url: libresign.github.io/site-preview deploy-repository: LibreSign/site-preview preview-branch: gh-pages - wait-for-pages-deployment: true + # Pages builds can be superseded when another PR publishes to site-preview. + # Verify the live revision below, not the individual Pages run. + wait-for-pages-deployment: false token: ${{ secrets.PREVIEW_TOKEN }} + + - name: Verify published preview revision + if: github.event.pull_request.head.repo.full_name == github.repository + env: + PREVIEW_URL: https://libresign.github.io/site-preview/pr-preview/pr-${{ github.event.pull_request.number }} + EXPECTED_SHA: ${{ github.event.pull_request.head.sha }} + run: bash .github/scripts/verify-preview-publication.sh "$PREVIEW_URL" "$EXPECTED_SHA" build_production/preview-revision.txt From 2092e7f56d4d67afb1219a8dc8819dcb7d633a91 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Wed, 11 Nov 2026 03:07:32 +0000 Subject: [PATCH 2/3] fix(ci): resolve fork preview PR from verified workflow source Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .github/workflows/preview-fork-deploy.yml | 33 ++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/.github/workflows/preview-fork-deploy.yml b/.github/workflows/preview-fork-deploy.yml index 37662eaf..5c78954d 100644 --- a/.github/workflows/preview-fork-deploy.yml +++ b/.github/workflows/preview-fork-deploy.yml @@ -15,7 +15,7 @@ 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: @@ -25,6 +25,25 @@ jobs: ref: ${{ github.event.repository.default_branch }} persist-credentials: false + # 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: @@ -33,12 +52,20 @@ jobs: run-id: ${{ github.event.workflow_run.id }} github-token: ${{ secrets.GITHUB_TOKEN }} + - name: Verify artifact matches the completed fork revision + env: + SOURCE_SHA: ${{ github.event.workflow_run.head_sha }} + run: | + test -s build_production/index.html + test -s build_production/preview-revision.txt + test "$(cat build_production/preview-revision.txt)" = "$SOURCE_SHA" + - 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 @@ -48,6 +75,6 @@ jobs: - name: Verify published preview revision env: - PREVIEW_URL: https://libresign.github.io/site-preview/pr-preview/pr-${{ github.event.workflow_run.pull_requests[0].number }} + PREVIEW_URL: https://libresign.github.io/site-preview/pr-preview/pr-${{ steps.pr.outputs.number }} EXPECTED_SHA: ${{ github.event.workflow_run.head_sha }} run: bash .github/scripts/verify-preview-publication.sh "$PREVIEW_URL" "$EXPECTED_SHA" build_production/preview-revision.txt From 78e70268784774644262c471a38d5f0639c69970 Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Wed, 11 Nov 2026 03:16:12 +0000 Subject: [PATCH 3/3] refactor(ci): use existing preview deploy action without extra verifier Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .github/scripts/verify-preview-publication.sh | 44 ------------------- .github/workflows/preview-fork-deploy.yml | 21 +-------- .github/workflows/preview.yml | 15 +------ 3 files changed, 3 insertions(+), 77 deletions(-) delete mode 100644 .github/scripts/verify-preview-publication.sh diff --git a/.github/scripts/verify-preview-publication.sh b/.github/scripts/verify-preview-publication.sh deleted file mode 100644 index 99094cc7..00000000 --- a/.github/scripts/verify-preview-publication.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env bash -# Check the published revision, not an individual GitHub Pages workflow run. -# Shared Pages deployments may cancel each other while still publishing every PR. -set -euo pipefail - -if [ "$#" -ne 3 ]; then - echo "Usage: $0 PREVIEW_URL EXPECTED_SHA REVISION_FILE" >&2 - exit 2 -fi - -preview_url="${1%/}" -expected_sha="$2" -revision_file="$3" - -if [[ ! "$preview_url" =~ ^https://libresign\.github\.io/site-preview/pr-preview/pr-[0-9]+$ ]]; then - echo "::error::Invalid preview URL: $preview_url" >&2 - exit 2 -fi -if [[ ! "$expected_sha" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::Invalid expected commit SHA" >&2 - exit 2 -fi -if [ ! -s "$revision_file" ] || [ "$(cat "$revision_file")" != "$expected_sha" ]; then - echo "::error::Preview artifact does not match the PR revision" >&2 - exit 1 -fi - -max_attempts="${PREVIEW_CHECK_ATTEMPTS:-60}" -poll_seconds="${PREVIEW_CHECK_INTERVAL:-5}" -for ((attempt = 1; attempt <= max_attempts; attempt++)); do - if served_sha="$(curl -fsSL --max-time 15 --proto '=https' --proto-redir '=https' \ - -H 'Cache-Control: no-cache' \ - "$preview_url/preview-revision.txt?sha=$expected_sha&attempt=$attempt" 2>/dev/null)" && - [ "$served_sha" = "$expected_sha" ]; then - echo "Published preview matches $expected_sha: $preview_url/" - exit 0 - fi - if ((attempt < max_attempts)); then - sleep "$poll_seconds" - fi -done - -echo "::error::GitHub Pages did not serve revision $expected_sha at $preview_url/" >&2 -exit 1 diff --git a/.github/workflows/preview-fork-deploy.yml b/.github/workflows/preview-fork-deploy.yml index 5c78954d..4711e012 100644 --- a/.github/workflows/preview-fork-deploy.yml +++ b/.github/workflows/preview-fork-deploy.yml @@ -19,12 +19,6 @@ jobs: runs-on: ubuntu-latest steps: - - name: Check out trusted preview verification script - uses: actions/checkout@v4 - with: - ref: ${{ github.event.repository.default_branch }} - persist-credentials: false - # 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 @@ -52,13 +46,8 @@ jobs: run-id: ${{ github.event.workflow_run.id }} github-token: ${{ secrets.GITHUB_TOKEN }} - - name: Verify artifact matches the completed fork revision - env: - SOURCE_SHA: ${{ github.event.workflow_run.head_sha }} - run: | - test -s build_production/index.html - test -s build_production/preview-revision.txt - test "$(cat build_production/preview-revision.txt)" = "$SOURCE_SHA" + - name: Check downloaded preview + run: test -s build_production/index.html - name: Deploy preview uses: LibreSign/pr-preview-action@2a6da1177b47dc23173ba9a75e01da0a0d800978 @@ -72,9 +61,3 @@ jobs: # The shared Pages branch can be published by another PR concurrently. wait-for-pages-deployment: false token: ${{ secrets.PREVIEW_TOKEN }} - - - name: Verify published preview revision - env: - PREVIEW_URL: https://libresign.github.io/site-preview/pr-preview/pr-${{ steps.pr.outputs.number }} - EXPECTED_SHA: ${{ github.event.workflow_run.head_sha }} - run: bash .github/scripts/verify-preview-publication.sh "$PREVIEW_URL" "$EXPECTED_SHA" build_production/preview-revision.txt diff --git a/.github/workflows/preview.yml b/.github/workflows/preview.yml index cbfe11de..dc8fbdc0 100644 --- a/.github/workflows/preview.yml +++ b/.github/workflows/preview.yml @@ -46,11 +46,6 @@ jobs: WC_CONSUMER_SECRET: ${{ secrets.WC_CONSUMER_SECRET }} run: composer prod - - name: Stamp build with its PR head revision - env: - EXPECTED_SHA: ${{ github.event.pull_request.head.sha }} - run: printf '%s\n' "$EXPECTED_SHA" > build_production/preview-revision.txt - - name: Upload fork preview artifact if: github.event.pull_request.head.repo.full_name != github.repository uses: actions/upload-artifact@v4 @@ -69,14 +64,6 @@ jobs: pages-base-url: libresign.github.io/site-preview deploy-repository: LibreSign/site-preview preview-branch: gh-pages - # Pages builds can be superseded when another PR publishes to site-preview. - # Verify the live revision below, not the individual Pages run. + # Another PR can supersede this Pages build on the shared repository. wait-for-pages-deployment: false token: ${{ secrets.PREVIEW_TOKEN }} - - - name: Verify published preview revision - if: github.event.pull_request.head.repo.full_name == github.repository - env: - PREVIEW_URL: https://libresign.github.io/site-preview/pr-preview/pr-${{ github.event.pull_request.number }} - EXPECTED_SHA: ${{ github.event.pull_request.head.sha }} - run: bash .github/scripts/verify-preview-publication.sh "$PREVIEW_URL" "$EXPECTED_SHA" build_production/preview-revision.txt