From cf92eaf11ece9aae3f0553e9b1e06764a9f9bafc Mon Sep 17 00:00:00 2001 From: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> Date: Tue, 10 Nov 2026 21:25:47 +0000 Subject: [PATCH] fix(ci): restore reliable, scoped PR previews Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com> --- .github/workflows/preview-deploy.yml | 40 +++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/.github/workflows/preview-deploy.yml b/.github/workflows/preview-deploy.yml index 4baed09b..f05e28e5 100644 --- a/.github/workflows/preview-deploy.yml +++ b/.github/workflows/preview-deploy.yml @@ -4,9 +4,6 @@ on: workflow_run: workflows: ["Preview Build"] types: [completed] - pull_request_target: - branches: [ main ] - types: [closed] permissions: actions: read @@ -27,14 +24,28 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Extract PR number from workflow_run payload + # workflow_run.pull_requests may be empty for fork PRs. + - name: Resolve PR number from preview 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: | - pr_number=$(jq -r '.workflow_run.pull_requests[0].number // empty' "$GITHUB_EVENT_PATH") - echo "number=$pr_number" >> "$GITHUB_OUTPUT" + set -euo pipefail + ref="$SOURCE_BRANCH" + if [[ "$SOURCE_REPOSITORY" != "$GITHUB_REPOSITORY" ]]; then + ref="${SOURCE_REPOSITORY%%/*}:$SOURCE_BRANCH" + fi + gh pr view "$ref" -R "$GITHUB_REPOSITORY" \ + --json number,state,headRefOid,headRepository,baseRefName | + 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 build artifact - if: steps.pr.outputs.number != '' uses: actions/download-artifact@v4 with: name: preview-build @@ -42,8 +53,19 @@ jobs: run-id: ${{ github.event.workflow_run.id }} github-token: ${{ secrets.GITHUB_TOKEN }} + # Installation token can only write contents in the preview repository. + # A dedicated GitHub App must be installed only on site-preview. + - name: Get scoped publication token + id: publication-token + uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + with: + client-id: ${{ vars.SITE_PREVIEW_APP_CLIENT_ID }} + private-key: ${{ secrets.SITE_PREVIEW_APP_PRIVATE_KEY }} + owner: LibreCodeCoop + repositories: site-preview + permission-contents: write + - name: Deploy preview - if: steps.pr.outputs.number != '' uses: LibreSign/pr-preview-action@2a6da1177b47dc23173ba9a75e01da0a0d800978 with: action: deploy @@ -53,4 +75,4 @@ jobs: deploy-repository: LibreCodeCoop/site-preview preview-branch: main wait-for-pages-deployment: false - token: ${{ secrets.PREVIEW_TOKEN }} + token: ${{ steps.publication-token.outputs.token }}