1111 type : string
1212
1313concurrency :
14- group : publish-pr-preview-${{ github.event.pull_request.number || inputs.pr_number }}
14+ group : publish-pr-preview-${{ github.event.pull_request.number || github.run_id }}
1515 cancel-in-progress : false
1616
1717jobs :
18- build :
18+ context :
1919 if : >-
2020 github.event_name == 'workflow_dispatch' ||
2121 (github.event.label.name == 'publish-preview' &&
2222 github.event.pull_request.head.repo.full_name == github.repository)
2323 runs-on : ubuntu-latest
24+ timeout-minutes : 5
25+ permissions :
26+ contents : read
27+ pull-requests : read
28+ outputs :
29+ pr_number : ${{ steps.context.outputs.pr_number }}
30+ head_sha : ${{ steps.context.outputs.head_sha }}
31+ steps :
32+ - name : Validate pull request context
33+ id : context
34+ uses : actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
35+ env :
36+ DEFAULT_BRANCH : ${{ github.event.repository.default_branch }}
37+ EVENT_PR_NUMBER : ${{ github.event.pull_request.number }}
38+ INPUT_PR_NUMBER : ${{ inputs.pr_number }}
39+ WORKFLOW_REF : ${{ github.ref }}
40+ with :
41+ script : |
42+ const rawPrNumber = context.eventName === 'workflow_dispatch'
43+ ? process.env.INPUT_PR_NUMBER
44+ : process.env.EVENT_PR_NUMBER;
45+ if (!/^[1-9][0-9]*$/.test(rawPrNumber || '')) {
46+ core.setFailed('Pull request number must contain ASCII digits only.');
47+ return;
48+ }
49+
50+ if (context.eventName === 'workflow_dispatch') {
51+ const defaultRef = `refs/heads/${process.env.DEFAULT_BRANCH}`;
52+ if (process.env.WORKFLOW_REF !== defaultRef) {
53+ core.setFailed(`Run manual previews from ${defaultRef}.`);
54+ return;
55+ }
56+ }
57+
58+ const prNumber = Number(rawPrNumber);
59+ if (!Number.isSafeInteger(prNumber)) {
60+ core.setFailed('Pull request number is outside the supported range.');
61+ return;
62+ }
63+ const {data: pullRequest} = await github.rest.pulls.get({
64+ owner: context.repo.owner,
65+ repo: context.repo.repo,
66+ pull_number: prNumber,
67+ });
68+ if (pullRequest.state !== 'open') {
69+ core.setFailed(`Pull request #${prNumber} is not open.`);
70+ return;
71+ }
72+ if (pullRequest.head.repo?.full_name !== `${context.repo.owner}/${context.repo.repo}`) {
73+ core.setFailed('Preview publication is limited to branches in this repository.');
74+ return;
75+ }
76+
77+ core.setOutput('pr_number', String(prNumber));
78+ core.setOutput('head_sha', pullRequest.head.sha);
79+
80+ build :
81+ needs : context
82+ runs-on : ubuntu-latest
2483 timeout-minutes : 10
2584 permissions :
2685 contents : read
2786 outputs :
2887 preview_version : ${{ steps.version.outputs.preview_version }}
29- pr_number : ${{ steps.context.outputs.pr_number }}
3088 steps :
3189 - uses : actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
3290 with :
33- ref : ${{ github.event.pull_request.head.sha || format('refs/pull/{0}/head', inputs.pr_number) }}
91+ ref : ${{ needs.context.outputs.head_sha }}
3492 fetch-depth : 0
3593 persist-credentials : false
3694
@@ -44,13 +102,6 @@ jobs:
44102 - name : Install distribution validator
45103 run : python -m pip install "twine>=4.0.0"
46104
47- - name : Record preview context
48- id : context
49- env :
50- EVENT_PR_NUMBER : ${{ github.event.pull_request.number }}
51- INPUT_PR_NUMBER : ${{ inputs.pr_number }}
52- run : echo "pr_number=${EVENT_PR_NUMBER:-$INPUT_PR_NUMBER}" >> "$GITHUB_OUTPUT"
53-
54105 - name : Inject deterministic preview version
55106 env :
56107 PREVIEW_ID : ${{ github.run_id }}
84135 retention-days : 14
85136
86137 publish :
87- needs : build
138+ needs : [context, build]
88139 runs-on : ubuntu-latest
89140 timeout-minutes : 10
90141 permissions :
@@ -108,7 +159,7 @@ jobs:
108159 uses : actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
109160 env :
110161 PREVIEW_VERSION : ${{ needs.build.outputs.preview_version }}
111- PR_NUMBER : ${{ needs.build .outputs.pr_number }}
162+ PR_NUMBER : ${{ needs.context .outputs.pr_number }}
112163 with :
113164 script : |
114165 const marker = '<!-- socketdev-pr-preview -->';
0 commit comments