Repository navigation
286 lines (257 loc) · 10.7 KB
/
Copy pathpublish-preview-oidc.yml
File metadata and controls
286 lines (257 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
name: Publish Preview Builds (OIDC)
on:
workflow_call:
inputs:
pull-request:
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: ''
packages:
description: 'Comma-separated list of packages to publish (monorepo only), e.g., "@metamask/network-controller, permission-controller". Names without a scope get the source scope. Mutually exclusive with "only-changed-packages".'
type: string
required: false
default: ''
only-changed-packages:
description: 'Only publish packages changed compared to the base branch of the pull request, or the default branch (monorepo only). Mutually exclusive with "packages".'
type: boolean
required: false
default: false
npm-scope:
description: 'Target NPM scope for preview packages'
type: string
required: false
default: '@metamask-previews'
source-scope:
description: 'Source NPM scope to replace'
type: string
required: false
default: '@metamask/'
build-command:
description: 'Command to build the project'
type: string
required: false
default: 'yarn build'
is-monorepo:
description: 'Whether the consumer is a monorepo (workspace-aware prepare/publish/message)'
type: boolean
required: false
default: true
rename-after-install-and-build:
description: 'Governs where in the workflow that packages in the repo are renamed to use the preview build scope. If true, this step runs after the install and build steps; if false (default), it runs before. This option is mostly for Snaps so that artifacts (e.g. dist/bundle.js, snap.manifest.json) capture the original package name, not the preview build name.'
type: boolean
required: false
default: false
environment:
description: 'GitHub environment for the publish job (e.g., default-branch). Empty = no gate.'
type: string
required: false
default: ''
artifact-retention-days:
description: 'Days to retain build artifacts'
type: number
required: false
default: 4
docs-url:
description: 'URL to preview builds documentation (included in PR comment)'
type: string
required: false
default: ''
dry-run:
description: 'Skip actual NPM publish (for testing)'
type: boolean
required: false
default: false
secrets:
BUILD_ENV:
description: 'JSON object of environment variables to pass to the build step (e.g. ''{"FOO":"bar","API_URL":"https://..."}''). Use this for build-time configuration and secrets needed by the build command.'
required: false
jobs:
build:
name: Build
uses: ./.github/workflows/prepare-preview.yml
with:
pull-request: ${{ inputs.pull-request }}
branch: ${{ inputs.branch }}
packages: ${{ inputs.packages }}
only-changed-packages: ${{ inputs.only-changed-packages }}
npm-scope: ${{ inputs.npm-scope }}
source-scope: ${{ inputs.source-scope }}
build-command: ${{ inputs.build-command }}
is-monorepo: ${{ inputs.is-monorepo }}
rename-after-install-and-build: ${{ inputs.rename-after-install-and-build }}
artifact-retention-days: ${{ inputs.artifact-retention-days }}
pack: true
secrets:
BUILD_ENV: ${{ secrets.BUILD_ENV }}
publish-preview:
name: Publish preview
needs: build
if: ${{ needs.build.outputs.is-fork == 'false' }}
permissions:
contents: read
id-token: write
pull-requests: write
environment: ${{ inputs.environment }}
runs-on: ubuntu-latest
steps:
- name: Download tarballs
if: ${{ needs.build.outputs.publish-locations != '[]' }}
uses: actions/download-artifact@v8
with:
name: preview-build-tarballs
path: ${{ runner.temp }}/preview-build-tarballs
- name: Validate tarballs
env:
NPM_SCOPE: ${{ inputs.npm-scope }}
run: |
shopt -s nullglob
has_invalid_tarballs=false
for tarball in "$RUNNER_TEMP"/preview-build-tarballs/*.tgz; do
file="$(basename "$tarball")"
if ! entries="$(tar -tzf "$tarball")"; then
echo "::error::${file} isn't a valid tarball."
has_invalid_tarballs=true
continue
fi
if grep -qv '^package/' <<< "$entries" || grep -qE '//|(^|/)\.\.?(/|$)' <<< "$entries"; then
echo "::error::${file} contains unexpected paths."
has_invalid_tarballs=true
continue
fi
if [[ "$(grep -cx 'package/package.json' <<< "$entries")" -ne 1 ]]; then
echo "::error::${file} must contain exactly one package/package.json."
has_invalid_tarballs=true
continue
fi
# The name must be in the preview scope, and the version must be a
# preview version. publishConfig may only set the access and the
# npm registry, since npm applies it when publishing.
if ! tar -xzOf "$tarball" package/package.json | jq -e --arg scope "${NPM_SCOPE}/" '
(.name | type == "string" and startswith($scope))
and (.version | type == "string" and test("-preview-[0-9a-f]+$"))
and ((.publishConfig // {}) | type == "object"
and (keys - ["access", "registry"] | length == 0)
and ((.registry // "https://registry.npmjs.org/") | IN("https://registry.npmjs.org", "https://registry.npmjs.org/")))
' > /dev/null; then
echo "::error::${file} has an invalid name, version, or publishConfig."
has_invalid_tarballs=true
fi
done
if [[ "$has_invalid_tarballs" == "true" ]]; then
exit 1
fi
- name: Publish preview builds (monorepo)
if: ${{ inputs.is-monorepo && !inputs.dry-run }}
run: |
shopt -s nullglob
for tarball in "$RUNNER_TEMP"/preview-build-tarballs/*.tgz; do
package="$(tar -xzOf "$tarball" package/package.json | jq -r '"\(.name)@\(.version)"')"
echo "::group::Publishing $package"
if npm publish "$tarball" \
--tag preview \
--ignore-scripts \
--provenance=false \
--registry https://registry.npmjs.org/; then
echo "$package" >> "$RUNNER_TEMP/published.txt"
else
echo "::warning::Failed to publish $package"
echo "$package" >> "$RUNNER_TEMP/failed.txt"
fi
echo "::endgroup::"
done
- name: Publish preview build (polyrepo)
if: ${{ !inputs.is-monorepo && !inputs.dry-run }}
run: |
tarballs=("$RUNNER_TEMP"/preview-build-tarballs/*.tgz)
if [[ ${#tarballs[@]} -ne 1 ]]; then
echo "::error::Expected exactly one tarball, found ${#tarballs[@]}."
exit 1
fi
npm publish "${tarballs[0]}" \
--tag preview \
--ignore-scripts \
--provenance=false \
--registry https://registry.npmjs.org/
- name: Dry run notice
if: ${{ inputs.dry-run }}
run: echo "Dry run — skipping publish"
- name: Generate preview build message (monorepo)
if: ${{ inputs.is-monorepo }}
env:
DOCS_URL: ${{ inputs.docs-url }}
run: |
docs_link=""
if [[ -n "$DOCS_URL" ]]; then
docs_link="[Learn how to use preview builds in other projects](${DOCS_URL})."
fi
published="$(cat "$RUNNER_TEMP/published.txt" 2>/dev/null || true)"
failed="$(cat "$RUNNER_TEMP/failed.txt" 2>/dev/null || true)"
if [[ -n "$published" ]]; then
echo -n "Preview builds have been published." > preview-build-message.txt
if [[ -n "$docs_link" ]]; then
echo -n " ${docs_link}" >> preview-build-message.txt
fi
cat <<-MSGEOF >> preview-build-message.txt
<details>
<summary>Expand for full list of packages and versions.</summary>
\`\`\`
${published}
\`\`\`
</details>
MSGEOF
else
echo "No preview builds were published." > preview-build-message.txt
fi
if [[ -n "$failed" ]]; then
cat <<-MSGEOF >> preview-build-message.txt
The following packages failed to publish. [See the workflow run for details](https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}).
\`\`\`
${failed}
\`\`\`
MSGEOF
fi
- name: Generate preview build message (polyrepo)
if: ${{ !inputs.is-monorepo }}
env:
DOCS_URL: ${{ inputs.docs-url }}
run: |
package="$(tar -xzOf "$RUNNER_TEMP"/preview-build-tarballs/*.tgz package/package.json | jq -r '"\(.name)@\(.version)"')"
cat <<-MSGEOF > preview-build-message.txt
The following preview build has been published:
\`\`\`
${package}
\`\`\`
MSGEOF
if [[ -n "$DOCS_URL" ]]; then
printf '\n%s\n' "[Learn how to use preview builds in other projects](${DOCS_URL})." >> preview-build-message.txt
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:
GH_REPO: ${{ github.repository }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ inputs.pull-request }}
comment-on-failure:
name: Comment on failure
needs:
- build
- publish-preview
if: ${{ failure() && inputs.pull-request }}
permissions:
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Post failure comment
run: gh pr comment "${PR_NUMBER}" --body "Failed to publish preview builds. [See the workflow run for details](https://github.com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT})."
env:
GH_REPO: ${{ github.repository }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ inputs.pull-request }}