From 4901241334207f73bbac6be1ba2d9ab412602e02 Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Fri, 13 Nov 2026 15:44:13 +0100 Subject: [PATCH 1/2] Let the preview workflows take a pull request number NPM rejects trusted publishing tokens from `issue_comment` events, so the OIDC variant cannot publish when a comment triggers it. Add a `pull-request` input to `publish-preview-oidc.yml` and `prepare-preview.yml` so a `workflow_dispatch` caller can pass the number instead of relying on `github.event.issue.number`. In `prepare-preview.yml` the input is optional and falls back to the issue number, so `publish-preview.yml` keeps working unchanged. React to the trigger comment only when there is one, and make `build-preview` depend on the fork check directly so it still runs when the reaction is skipped. --- .github/workflows/prepare-preview.yml | 13 +++++++++---- .github/workflows/publish-preview-oidc.yml | 7 ++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/.github/workflows/prepare-preview.yml b/.github/workflows/prepare-preview.yml index 9031daa4..e65bddff 100644 --- a/.github/workflows/prepare-preview.yml +++ b/.github/workflows/prepare-preview.yml @@ -19,6 +19,10 @@ on: description: 'Whether the consumer is a monorepo (workspace-aware prepare/publish/message)' type: boolean required: true + pull-request: + description: 'Pull request number to prepare a preview build for' + required: false + type: number rename-after-install-and-build: description: 'Whether packages are renamed to the preview build scope after (true) or before (false) the install and build steps.' type: boolean @@ -49,12 +53,12 @@ jobs: run: echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUMBER: ${{ github.event.issue.number }} + PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }} react-to-comment: name: React to comment needs: is-fork-pull-request - if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }} + if: ${{ github.event.comment.id && needs.is-fork-pull-request.outputs.IS_FORK == 'false' }} runs-on: ubuntu-latest steps: - name: Add reaction to trigger comment @@ -69,7 +73,8 @@ jobs: build-preview: name: Build preview - needs: react-to-comment + needs: is-fork-pull-request + if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 @@ -78,7 +83,7 @@ jobs: run: gh pr checkout "${PR_NUMBER}" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUMBER: ${{ github.event.issue.number }} + PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }} - name: Checkout and setup environment uses: MetaMask/action-checkout-and-setup@v3 diff --git a/.github/workflows/publish-preview-oidc.yml b/.github/workflows/publish-preview-oidc.yml index bb831d7c..5d7be3cf 100644 --- a/.github/workflows/publish-preview-oidc.yml +++ b/.github/workflows/publish-preview-oidc.yml @@ -3,6 +3,10 @@ name: Publish Preview Builds (OIDC) on: workflow_call: inputs: + pull-request: + description: 'Pull request number to publish a preview build for' + required: true + type: number npm-scope: description: 'Target NPM scope for preview packages' type: string @@ -58,6 +62,7 @@ jobs: name: Build uses: ./.github/workflows/prepare-preview.yml with: + pull-request: ${{ inputs.pull-request }} npm-scope: ${{ inputs.npm-scope }} source-scope: ${{ inputs.source-scope }} build-command: ${{ inputs.build-command }} @@ -204,4 +209,4 @@ jobs: run: gh pr comment "${PR_NUMBER}" --body-file preview-build-message.txt env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - PR_NUMBER: ${{ github.event.issue.number }} + PR_NUMBER: ${{ inputs.pull-request }} From 8d2d06028b3bab1f9ea6de3123d426188a70817d Mon Sep 17 00:00:00 2001 From: Maarten Zuidhoorn Date: Fri, 13 Nov 2026 16:01:46 +0100 Subject: [PATCH 2/2] Support previews from a branch and write a job summary Add a `branch` input to `publish-preview-oidc.yml` and `prepare-preview.yml` as an alternative to `pull-request`, so a `workflow_dispatch` caller can publish a preview without a pull request. Exactly one of the two is required. The workflow validates the branch name with `git check-ref-format`, resolves it to a commit once, and checks out that commit, so a push during the run cannot change what gets built. Only branches in this repository are accepted. Other refs, such as `refs/pull/N/head` or raw SHAs, could point at fork code and bypass the fork check. The preview build message is now always appended to `$GITHUB_STEP_SUMMARY`. The pull request comment is only posted when `pull-request` is set. --- .github/workflows/prepare-preview.yml | 54 +++++++++++++++++++++- .github/workflows/publish-preview-oidc.yml | 14 +++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/.github/workflows/prepare-preview.yml b/.github/workflows/prepare-preview.yml index e65bddff..9303a7be 100644 --- a/.github/workflows/prepare-preview.yml +++ b/.github/workflows/prepare-preview.yml @@ -20,9 +20,14 @@ on: type: boolean required: true pull-request: - description: 'Pull request number to prepare a preview build for' + description: 'Pull request number to prepare a preview build for.' required: false type: number + branch: + description: 'Branch in this repository to prepare a preview build for, instead of a pull request.' + required: false + type: string + default: '' rename-after-install-and-build: description: 'Whether packages are renamed to the preview build scope after (true) or before (false) the install and build steps.' type: boolean @@ -46,15 +51,51 @@ jobs: runs-on: ubuntu-latest outputs: IS_FORK: ${{ steps.is-fork.outputs.IS_FORK }} + REF_SHA: ${{ steps.ref-sha.outputs.REF_SHA }} steps: - uses: actions/checkout@v6 + + - name: Validate inputs + env: + PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }} + BRANCH: ${{ inputs.branch }} + run: | + if [[ -n "$PR_NUMBER" && -n "$BRANCH" ]]; then + echo "::error::Provide either a pull request number or a branch, not both." + exit 1 + fi + + if [[ -z "$PR_NUMBER" && -z "$BRANCH" ]]; then + echo "::error::Provide a pull request number or a branch." + exit 1 + fi + + if [[ -n "$BRANCH" ]] && ! git check-ref-format "refs/heads/${BRANCH}"; then + echo "::error::Invalid branch name." + exit 1 + fi + - name: Determine whether this PR is from a fork id: is-fork - run: echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT" + run: | + if [[ -z "$PR_NUMBER" ]]; then + echo "IS_FORK=false" >> "$GITHUB_OUTPUT" + else + echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT" + fi env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }} + - name: Resolve the branch to a commit + id: ref-sha + if: ${{ inputs.branch != '' }} + run: | + remote_ref="$(git ls-remote --exit-code origin "refs/heads/${BRANCH}")" + echo "REF_SHA=$(cut -f1 <<< "${remote_ref}")" >> "$GITHUB_OUTPUT" + env: + BRANCH: ${{ inputs.branch }} + react-to-comment: name: React to comment needs: is-fork-pull-request @@ -80,11 +121,20 @@ jobs: - uses: actions/checkout@v6 - name: Check out pull request + if: ${{ inputs.branch == '' }} run: gh pr checkout "${PR_NUMBER}" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }} + - name: Check out branch + if: ${{ inputs.branch != '' }} + run: | + git fetch --depth=1 origin "${REF_SHA}" + git checkout --detach "${REF_SHA}" + env: + REF_SHA: ${{ needs.is-fork-pull-request.outputs.REF_SHA }} + - name: Checkout and setup environment uses: MetaMask/action-checkout-and-setup@v3 with: diff --git a/.github/workflows/publish-preview-oidc.yml b/.github/workflows/publish-preview-oidc.yml index 5d7be3cf..b344b05b 100644 --- a/.github/workflows/publish-preview-oidc.yml +++ b/.github/workflows/publish-preview-oidc.yml @@ -4,9 +4,14 @@ on: workflow_call: inputs: pull-request: - description: 'Pull request number to publish a preview build for' - required: true + description: 'Pull request number to publish a preview build for. Mutually exclusive with "branch".' + required: false type: number + branch: + description: 'Branch in this repository to publish a preview build for. Mutually exclusive with "pull-request".' + required: false + type: string + default: '' npm-scope: description: 'Target NPM scope for preview packages' type: string @@ -63,6 +68,7 @@ jobs: uses: ./.github/workflows/prepare-preview.yml with: pull-request: ${{ inputs.pull-request }} + branch: ${{ inputs.branch }} npm-scope: ${{ inputs.npm-scope }} source-scope: ${{ inputs.source-scope }} build-command: ${{ inputs.build-command }} @@ -205,7 +211,11 @@ jobs: fi fi + - name: Add build preview to the job summary + run: cat preview-build-message.txt >> "$GITHUB_STEP_SUMMARY" + - name: Post build preview in comment + if: ${{ inputs.pull-request }} run: gh pr comment "${PR_NUMBER}" --body-file preview-build-message.txt env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}