Repository navigation
409 lines (366 loc) · 15.1 KB
/
Copy pathprepare-preview.yml
File metadata and controls
409 lines (366 loc) · 15.1 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
name: Prepare Preview Builds
on:
workflow_call:
inputs:
npm-scope:
description: 'Target NPM scope for preview packages'
type: string
required: true
source-scope:
description: 'Source NPM scope to replace'
type: string
required: true
build-command:
description: 'Command to build the project'
type: string
required: true
is-monorepo:
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
branch:
description: 'Branch in this repository to prepare a preview build for, instead of a pull request.'
required: false
type: string
default: ''
packages:
description: 'Comma-separated list of packages to publish (monorepo only). 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
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
required: true
artifact-retention-days:
description: 'Days to retain build artifacts'
type: number
required: true
pack:
description: 'Whether to upload packed tarballs of the packages to publish ("preview-build-tarballs"), instead of the workspace ("preview-build-artifacts").'
type: boolean
required: false
default: false
outputs:
is-fork:
description: 'Whether the pull request is from a fork. The build is skipped when it is.'
value: ${{ jobs.is-fork-pull-request.outputs.IS_FORK }}
publish-locations:
description: 'JSON array of the workspace locations to publish. Empty = all workspaces.'
value: ${{ jobs.build-preview.outputs.publish-locations }}
secrets:
BUILD_ENV:
description: 'JSON object of environment variables to pass to the build step.'
required: false
jobs:
is-fork-pull-request:
name: Determine whether this PR is from a fork
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 }}
PACKAGES: ${{ inputs.packages }}
ONLY_CHANGED_PACKAGES: ${{ inputs.only-changed-packages }}
IS_MONOREPO: ${{ inputs.is-monorepo }}
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 [[ -n "$PACKAGES" && "$ONLY_CHANGED_PACKAGES" == "true" ]]; then
echo "::error::Provide either a list of packages or only publish changed packages, not both."
exit 1
fi
if [[ "$IS_MONOREPO" != "true" && ( -n "$PACKAGES" || "$ONLY_CHANGED_PACKAGES" == "true" ) ]]; then
echo "::error::Selecting packages is only supported in monorepos."
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: |
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
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
run: |
gh api \
--method POST \
"repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}/reactions" \
-f content='+1'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMMENT_ID: ${{ github.event.comment.id }}
build-preview:
name: Build preview
needs: is-fork-pull-request
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
runs-on: ubuntu-latest
outputs:
publish-locations: ${{ steps.select-packages.outputs.publish-locations }}
steps:
- 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:
is-high-risk-environment: true
- name: Get commit SHA
id: commit-sha
run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
- name: Select packages to publish
id: select-packages
if: ${{ inputs.is-monorepo && (inputs.packages != '' || inputs.only-changed-packages) }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ inputs.pull-request || github.event.issue.number }}
PACKAGES: ${{ inputs.packages }}
SOURCE_SCOPE: ${{ inputs.source-scope }}
run: |
workspaces="$(yarn workspaces list --no-private --json)"
locations=()
if [[ -n "$PACKAGES" ]]; then
selected_names="$(jq -rn --arg input "$PACKAGES" --arg scope "$SOURCE_SCOPE" '
$input
| split(",")
| map(gsub("^\\s+|\\s+$"; ""))
| map(select(. != ""))
| map(if startswith("@") then . else $scope + . end)
| unique
| .[]
')"
names=()
if [[ -n "$selected_names" ]]; then
mapfile -t names <<< "$selected_names"
fi
if [[ ${#names[@]} -eq 0 ]]; then
echo "::error::No package names found in \"$PACKAGES\"."
exit 1
fi
has_unknown_packages=false
for name in "${names[@]}"; do
location="$(jq -r --arg name "$name" 'select(.name == $name) | .location' <<< "$workspaces")"
if [[ -z "$location" ]]; then
echo "::error::Unknown package: ${name}."
has_unknown_packages=true
else
locations+=("$location")
fi
done
if [[ "$has_unknown_packages" == "true" ]]; then
exit 1
fi
else
if [[ -n "$PR_NUMBER" ]]; then
base="$(gh pr view "$PR_NUMBER" --json baseRefName --jq '.baseRefName')"
else
base="$(gh api "repos/${GITHUB_REPOSITORY}" --jq '.default_branch')"
fi
# Get the merge base from the GitHub API, so only that commit has to
# be fetched instead of the history leading up to it.
head="$(git rev-parse HEAD)"
merge_base="$(gh api "repos/${GITHUB_REPOSITORY}/compare/${base}...${head}" --jq '.merge_base_commit.sha')"
git fetch --no-tags --depth=1 origin "$merge_base"
changed_files="$(git diff --name-only --no-renames "$merge_base" "$head")"
changed_locations="$(jq -r --arg files "$changed_files" '
select(.location as $location | $files | split("\n") | any(startswith($location + "/")))
| .location
' <<< "$workspaces")"
if [[ -n "$changed_locations" ]]; then
mapfile -t locations <<< "$changed_locations"
fi
if [[ ${#locations[@]} -eq 0 ]]; then
echo "::warning::No packages changed compared to ${base}."
fi
fi
echo "Packages to publish:"
for location in "${locations[@]}"; do
echo "- $location"
done
echo "publish-locations=$(jq -cn '$ARGS.positional' --args "${locations[@]}")" >> "$GITHUB_OUTPUT"
- name: Install dependencies (pre-build)
if: ${{ inputs.rename-after-install-and-build }}
run: yarn install --no-immutable
- name: Mask build environment values
env:
BUILD_ENV: ${{ secrets.BUILD_ENV }}
run: |
if [[ -n "$BUILD_ENV" ]]; then
while IFS= read -r line; do
if [[ -n "$line" ]]; then
echo "::add-mask::$line"
fi
done < <(jq --raw-output '.[] | tostring' <<< "$BUILD_ENV")
fi
- name: Build (pre-rename)
if: ${{ inputs.rename-after-install-and-build }}
env: ${{ fromJSON(secrets.BUILD_ENV || '{}') }}
run: ${{ inputs.build-command }}
- name: Prepare preview manifests
env:
NPM_SCOPE: ${{ inputs.npm-scope }}
COMMIT_SHA: ${{ steps.commit-sha.outputs.COMMIT_SHA }}
SOURCE_SCOPE: ${{ inputs.source-scope }}
IS_MONOREPO: ${{ inputs.is-monorepo }}
run: |
prepare_manifest() {
local manifest_file="$1"
jq --raw-output \
--arg npm_scope "$NPM_SCOPE" \
--arg hash "$COMMIT_SHA" \
--arg source_scope "$SOURCE_SCOPE" \
'
.name |= sub($source_scope; "\($npm_scope)/") |
.version |= (split("-")[0] + "-preview-\($hash)")
' \
"$manifest_file" > temp.json
mv temp.json "$manifest_file"
}
if [[ "$IS_MONOREPO" == "true" ]]; then
workspace_list="$(yarn workspaces list --no-private --json \
| jq --slurp --raw-output 'map([.location, .name]) | map(@tsv) | .[]')"
# Add resolutions so renamed packages still resolve from local workspace
echo "Adding workspace resolutions to root manifest..."
resolutions='{}'
while IFS=$'\t' read -r location name; do
version="$(jq --raw-output '.version' "$location/package.json")"
resolutions="$(jq \
--arg name "$name" \
--arg version "$version" \
--arg location "$location" \
'.[$name + "@^" + $version] = "portal:./" + $location' \
<<< "$resolutions")"
done <<< "$workspace_list"
jq --argjson resolutions "$resolutions" '.resolutions = ((.resolutions // {}) + $resolutions)' package.json > temp.json
mv temp.json package.json
echo "Preparing manifests..."
while IFS=$'\t' read -r location name; do
echo "- $name"
prepare_manifest "$location/package.json"
done <<< "$workspace_list"
else
echo "Preparing manifest..."
prepare_manifest package.json
fi
- name: Install dependencies
run: yarn install --no-immutable
- name: Build (post-rename)
if: ${{ !inputs.rename-after-install-and-build }}
env: ${{ fromJSON(secrets.BUILD_ENV || '{}') }}
run: ${{ inputs.build-command }}
- name: Pack packages
if: ${{ inputs.pack }}
env:
IS_MONOREPO: ${{ inputs.is-monorepo }}
PUBLISH_LOCATIONS: ${{ steps.select-packages.outputs.publish-locations }}
run: |
if [[ "$IS_MONOREPO" == "true" ]]; then
workspaces="$(yarn workspaces list --no-private --json)"
selected="$(jq -r --argjson selected "${PUBLISH_LOCATIONS:-null}" '
select($selected == null or (.location | IN($selected[])))
| .location
' <<< "$workspaces")"
locations=()
if [[ -n "$selected" ]]; then
mapfile -t locations <<< "$selected"
fi
else
locations=(.)
fi
tarballs="${RUNNER_TEMP}/preview-build-tarballs"
mkdir -p "$tarballs"
for location in "${locations[@]}"; do
manifest="$location/package.json"
jq 'if .scripts then .scripts |= with_entries(select(.key | test("^(pre|post)?(pack|publish|prepare)$") | not)) else . end' "$manifest" > temp.json
mv temp.json "$manifest"
name="$(jq -r '.name' "$manifest")"
echo "::group::Packing $name"
(cd "$location" && yarn pack --out "${tarballs}/${name//\//-}.tgz")
echo "::endgroup::"
done
- name: Upload tarballs
if: ${{ inputs.pack }}
uses: actions/upload-artifact@v7
with:
name: preview-build-tarballs
retention-days: ${{ inputs.artifact-retention-days }}
path: ${{ runner.temp }}/preview-build-tarballs/*.tgz
- name: Upload build artifacts (monorepo)
if: ${{ !inputs.pack && inputs.is-monorepo }}
uses: actions/upload-artifact@v7
with:
name: preview-build-artifacts
include-hidden-files: true
retention-days: ${{ inputs.artifact-retention-days }}
path: |
./.yarn/patches/*
./yarn.lock
./package.json
./packages/*/
!./packages/*/node_modules/
!./packages/*/tests/
!./packages/**/*.test.*
- name: Upload build artifacts (polyrepo)
if: ${{ !inputs.pack && !inputs.is-monorepo }}
uses: actions/upload-artifact@v7
with:
name: preview-build-artifacts
include-hidden-files: true
retention-days: ${{ inputs.artifact-retention-days }}
path: |
.
!./node_modules/
!./.git/