Skip to content

Commit c3dfa94

Browse files
authored
fix(publish-preview-oidc): support dispatching previews by pull request or branch (#323)
* 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. * 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.
1 parent b784ed2 commit c3dfa94

2 files changed

Lines changed: 76 additions & 6 deletions

File tree

‎.github/workflows/prepare-preview.yml‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ on:
1919
description: 'Whether the consumer is a monorepo (workspace-aware prepare/publish/message)'
2020
type: boolean
2121
required: true
22+
pull-request:
23+
description: 'Pull request number to prepare a preview build for.'
24+
required: false
25+
type: number
26+
branch:
27+
description: 'Branch in this repository to prepare a preview build for, instead of a pull request.'
28+
required: false
29+
type: string
30+
default: ''
2231
rename-after-install-and-build:
2332
description: 'Whether packages are renamed to the preview build scope after (true) or before (false) the install and build steps.'
2433
type: boolean
@@ -42,19 +51,55 @@ jobs:
4251
runs-on: ubuntu-latest
4352
outputs:
4453
IS_FORK: ${{ steps.is-fork.outputs.IS_FORK }}
54+
REF_SHA: ${{ steps.ref-sha.outputs.REF_SHA }}
4555
steps:
4656
- uses: actions/checkout@v6
57+
58+
- name: Validate inputs
59+
env:
60+
PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }}
61+
BRANCH: ${{ inputs.branch }}
62+
run: |
63+
if [[ -n "$PR_NUMBER" && -n "$BRANCH" ]]; then
64+
echo "::error::Provide either a pull request number or a branch, not both."
65+
exit 1
66+
fi
67+
68+
if [[ -z "$PR_NUMBER" && -z "$BRANCH" ]]; then
69+
echo "::error::Provide a pull request number or a branch."
70+
exit 1
71+
fi
72+
73+
if [[ -n "$BRANCH" ]] && ! git check-ref-format "refs/heads/${BRANCH}"; then
74+
echo "::error::Invalid branch name."
75+
exit 1
76+
fi
77+
4778
- name: Determine whether this PR is from a fork
4879
id: is-fork
49-
run: echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT"
80+
run: |
81+
if [[ -z "$PR_NUMBER" ]]; then
82+
echo "IS_FORK=false" >> "$GITHUB_OUTPUT"
83+
else
84+
echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT"
85+
fi
5086
env:
5187
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
52-
PR_NUMBER: ${{ github.event.issue.number }}
88+
PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }}
89+
90+
- name: Resolve the branch to a commit
91+
id: ref-sha
92+
if: ${{ inputs.branch != '' }}
93+
run: |
94+
remote_ref="$(git ls-remote --exit-code origin "refs/heads/${BRANCH}")"
95+
echo "REF_SHA=$(cut -f1 <<< "${remote_ref}")" >> "$GITHUB_OUTPUT"
96+
env:
97+
BRANCH: ${{ inputs.branch }}
5398

5499
react-to-comment:
55100
name: React to comment
56101
needs: is-fork-pull-request
57-
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
102+
if: ${{ github.event.comment.id && needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
58103
runs-on: ubuntu-latest
59104
steps:
60105
- name: Add reaction to trigger comment
@@ -69,16 +114,26 @@ jobs:
69114

70115
build-preview:
71116
name: Build preview
72-
needs: react-to-comment
117+
needs: is-fork-pull-request
118+
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
73119
runs-on: ubuntu-latest
74120
steps:
75121
- uses: actions/checkout@v6
76122

77123
- name: Check out pull request
124+
if: ${{ inputs.branch == '' }}
78125
run: gh pr checkout "${PR_NUMBER}"
79126
env:
80127
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81-
PR_NUMBER: ${{ github.event.issue.number }}
128+
PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }}
129+
130+
- name: Check out branch
131+
if: ${{ inputs.branch != '' }}
132+
run: |
133+
git fetch --depth=1 origin "${REF_SHA}"
134+
git checkout --detach "${REF_SHA}"
135+
env:
136+
REF_SHA: ${{ needs.is-fork-pull-request.outputs.REF_SHA }}
82137

83138
- name: Checkout and setup environment
84139
uses: MetaMask/action-checkout-and-setup@v3

‎.github/workflows/publish-preview-oidc.yml‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ name: Publish Preview Builds (OIDC)
33
on:
44
workflow_call:
55
inputs:
6+
pull-request:
7+
description: 'Pull request number to publish a preview build for. Mutually exclusive with "branch".'
8+
required: false
9+
type: number
10+
branch:
11+
description: 'Branch in this repository to publish a preview build for. Mutually exclusive with "pull-request".'
12+
required: false
13+
type: string
14+
default: ''
615
npm-scope:
716
description: 'Target NPM scope for preview packages'
817
type: string
@@ -58,6 +67,8 @@ jobs:
5867
name: Build
5968
uses: ./.github/workflows/prepare-preview.yml
6069
with:
70+
pull-request: ${{ inputs.pull-request }}
71+
branch: ${{ inputs.branch }}
6172
npm-scope: ${{ inputs.npm-scope }}
6273
source-scope: ${{ inputs.source-scope }}
6374
build-command: ${{ inputs.build-command }}
@@ -200,8 +211,12 @@ jobs:
200211
fi
201212
fi
202213
214+
- name: Add build preview to the job summary
215+
run: cat preview-build-message.txt >> "$GITHUB_STEP_SUMMARY"
216+
203217
- name: Post build preview in comment
218+
if: ${{ inputs.pull-request }}
204219
run: gh pr comment "${PR_NUMBER}" --body-file preview-build-message.txt
205220
env:
206221
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
207-
PR_NUMBER: ${{ github.event.issue.number }}
222+
PR_NUMBER: ${{ inputs.pull-request }}

0 commit comments

Comments
 (0)