-
Notifications
You must be signed in to change notification settings - Fork 0
546 lines (522 loc) · 20.9 KB
/
Copy pathpublish.yaml
File metadata and controls
546 lines (522 loc) · 20.9 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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
name: Stage npm Release
run-name: Stage ${{ github.ref_name }}
# Runs only at a signed release tag, dispatched by build.yml's release job.
# This file is frozen at the tag it runs from, so build.yml rehearses the same
# pack, install, and load procedure on every push to main.
on:
workflow_dispatch:
permissions:
contents: read
defaults:
run:
shell: bash
concurrency:
group: stage-${{ github.repository }}-${{ github.ref }}
cancel-in-progress: false
queue: single
jobs:
validate:
name: Validate signed release tag
runs-on: ubuntu-24.04
outputs:
package_name: ${{ steps.identity.outputs.package_name }}
tag_object_sha: ${{ steps.identity.outputs.tag_object_sha }}
version: ${{ steps.identity.outputs.version }}
steps:
- name: Checkout the release tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- name: Verify tag, version, and signatures
id: identity
env:
EXPECTED_PACKAGE_NAME: "@photostructure/sqlite"
EXPECTED_REPOSITORY_URL: git+https://github.com/photostructure/node-sqlite.git
GH_TOKEN: ${{ github.token }}
REF_NAME: ${{ github.ref_name }}
run: |
if [[ ! "$REF_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || \
[[ "$GITHUB_REF" != "refs/tags/$REF_NAME" ]]; then
echo "::error::This workflow must run at a vMAJOR.MINOR.PATCH tag, not $GITHUB_REF"
exit 1
fi
PACKAGE_NAME="$(node -p "require('./package.json').name")"
VERSION="$(node -p "require('./package.json').version")"
REPOSITORY_URL="$(node -p "require('./package.json').repository.url")"
if [[ "$PACKAGE_NAME" != "$EXPECTED_PACKAGE_NAME" || \
"$REPOSITORY_URL" != "$EXPECTED_REPOSITORY_URL" ]]; then
echo "::error::The tagged package identity does not match this repository"
exit 1
fi
if [[ "$REF_NAME" != "v$VERSION" ]]; then
echo "::error::Tag $REF_NAME does not match package version $VERSION"
exit 1
fi
REF_OBJECT_TYPE="$(
gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$REF_NAME" --jq '.object.type'
)"
TAG_OBJECT_SHA="$(
gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$REF_NAME" --jq '.object.sha'
)"
if [[ "$REF_OBJECT_TYPE" != tag ]]; then
echo "::error::$REF_NAME must be an annotated tag"
exit 1
fi
TAG_TARGET_TYPE="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.object.type'
)"
TAG_TARGET_SHA="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.object.sha'
)"
TAG_VERIFIED="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.verification.verified'
)"
COMMIT_VERIFIED="$(
gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA" --jq '.commit.verification.verified'
)"
if [[ "$TAG_TARGET_TYPE" != commit || "$TAG_TARGET_SHA" != "$GITHUB_SHA" ]]; then
echo "::error::Tag $REF_NAME does not point directly to the workflow commit"
exit 1
fi
if [[ "$TAG_VERIFIED" != true || "$COMMIT_VERIFIED" != true ]]; then
echo "::error::The release commit and annotated tag must both have verified signatures"
exit 1
fi
{
echo "package_name=$PACKAGE_NAME"
echo "tag_object_sha=$TAG_OBJECT_SHA"
echo "version=$VERSION"
} >> "$GITHUB_OUTPUT"
prebuild-mac-win:
name: Build ${{ matrix.target }} from the release tag
needs: validate
strategy:
fail-fast: false
matrix:
include:
- target: darwin-x64
runner: macos-15-intel
- target: darwin-arm64
runner: macos-14
- target: win32-x64
runner: windows-latest
- target: win32-arm64
runner: windows-11-arm
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout the release tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install the release Node.js toolchain
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: 24.19.0
package-manager-cache: false
- run: npm ci --ignore-scripts
- run: npm run build:native
- name: Upload the prebuild
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: prebuild-${{ matrix.target }}
path: prebuilds/
if-no-files-found: error
retention-days: 1
- run: git diff --exit-code
prebuild-linux-glibc:
name: Build linux-${{ matrix.arch }}-glibc from the release tag
needs: validate
strategy:
fail-fast: false
matrix:
include:
- arch: x64
runner: ubuntu-24.04
- arch: arm64
runner: ubuntu-24.04-arm
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout the release tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install the release Node.js toolchain
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: 24.19.0
package-manager-cache: false
- run: npm ci --ignore-scripts
- name: Build in Debian 11 for glibc 2.31 compatibility
env:
TARGET_ARCH: ${{ matrix.arch }}
run: npm run build:native:linux
- name: Upload the prebuild
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: prebuild-linux-${{ matrix.arch }}-glibc
path: prebuilds/
if-no-files-found: error
retention-days: 1
- run: git diff --exit-code
prebuild-linux-musl:
name: Build linux-${{ matrix.arch }}-musl from the release tag
needs: validate
strategy:
fail-fast: false
matrix:
include:
- arch: x64
runner: ubuntu-24.04
platform: linux/amd64
- arch: arm64
runner: ubuntu-24.04-arm
platform: linux/arm64
runs-on: ${{ matrix.runner }}
steps:
- name: Checkout the release tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Build in Alpine Linux
env:
DOCKER_PLATFORM: ${{ matrix.platform }}
run: |
docker run --rm \
-v "$PWD:/tmp/project" \
--entrypoint /bin/sh \
--platform "$DOCKER_PLATFORM" \
node:22-alpine \
-c 'apk add build-base git python3 py3-setuptools --update-cache && cd /tmp/project && npm ci --ignore-scripts && npm run build:native'
- name: Upload the prebuild
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: prebuild-linux-${{ matrix.arch }}-musl
path: prebuilds/
if-no-files-found: error
retention-days: 1
- run: git diff --exit-code
pack:
name: Build and pack the exact release
needs:
- validate
- prebuild-mac-win
- prebuild-linux-glibc
- prebuild-linux-musl
runs-on: ubuntu-24.04
env:
EXPECTED_PACKAGE_NAME: ${{ needs.validate.outputs.package_name }}
EXPECTED_VERSION: ${{ needs.validate.outputs.version }}
steps:
- name: Checkout the release tag
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install the release Node.js toolchain
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: 24.19.0
package-manager-cache: false
- name: Verify the release npm version
run: test "$(npm --version)" = "11.17.0"
- run: npm ci --ignore-scripts
- name: Download exact-tag prebuilds
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
pattern: prebuild-*
path: ./prebuilds
merge-multiple: true
- name: Verify that all eight prebuilds arrived
run: npm run release:verify-prebuilds -- --project-root .
- name: Build JavaScript and type declarations
run: npm run build:dist
- name: Pack and inventory the package
run: |
npm run release:pack-package -- \
--project-root . \
--artifact-dir package-artifact \
--expected-name "$EXPECTED_PACKAGE_NAME" \
--expected-version "$EXPECTED_VERSION"
- run: git diff --exit-code
- name: Upload the exact package
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: npm-package-${{ github.ref_name }}
path: package-artifact/
if-no-files-found: error
retention-days: 1
verify-package-mac-win:
name: Test packed package on Node.js ${{ matrix.node-version }} / ${{ matrix.os }}
needs: pack
strategy:
fail-fast: false
matrix:
os: [macos-15-intel, macos-14, windows-latest, windows-11-arm]
node-version: [22, 24, 26]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install the package-test toolchain
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: 24.19.0
package-manager-cache: false
- name: Verify the package-test npm version
run: test "$(npm --version)" = "11.17.0"
- run: npm ci --ignore-scripts
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: npm-package-${{ github.ref_name }}
path: package-artifact/
- name: Install the packed package with the dependency age gate
# The exact release tarball is the subject under test.
run: | # zizmor: ignore[adhoc-packages]
npm run release:install-package -- \
--project-root . \
--artifact-dir package-artifact \
--install-root package-install
- name: Install the target Node.js runtime
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: ${{ matrix.node-version }}
package-manager-cache: false
- name: Load the packed package
run: |
npm run release:load-package -- \
--project-root . \
--install-root package-install
- run: git diff --exit-code
verify-package-ubuntu:
name: Test packed package on Node.js ${{ matrix.node-version }} / ${{ matrix.os }} ${{ matrix.arch }}
needs: pack
strategy:
fail-fast: false
matrix:
os: [ubuntu-22.04, ubuntu-24.04]
arch: [x64, arm64]
node-version: [22, 24, 26]
runs-on: ${{ matrix.arch == 'arm64' && format('{0}-arm', matrix.os) || matrix.os }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Install the package-test toolchain
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: 24.19.0
package-manager-cache: false
- name: Verify the package-test npm version
run: test "$(npm --version)" = "11.17.0"
- run: npm ci --ignore-scripts
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: npm-package-${{ github.ref_name }}
path: package-artifact/
- name: Install the packed package with the dependency age gate
# The exact release tarball is the subject under test.
run: | # zizmor: ignore[adhoc-packages]
npm run release:install-package -- \
--project-root . \
--artifact-dir package-artifact \
--install-root package-install
- name: Install the target Node.js runtime
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: ${{ matrix.node-version }}
package-manager-cache: false
- name: Load the packed package
run: |
npm run release:load-package -- \
--project-root . \
--install-root package-install
- run: git diff --exit-code
verify-package-alpine:
name: Test packed package on Node.js ${{ matrix.node-version }} / Alpine ${{ matrix.arch }}
needs: pack
strategy:
fail-fast: false
matrix:
arch: [x64, arm64]
node-version: [22, 24, 26]
include:
- arch: x64
runner: ubuntu-24.04
platform: linux/amd64
- arch: arm64
runner: ubuntu-24.04-arm
platform: linux/arm64
runs-on: ${{ matrix.runner }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: npm-package-${{ github.ref_name }}
path: package-artifact/
- name: Install the packed package with the dependency age gate
env:
DOCKER_PLATFORM: ${{ matrix.platform }}
run: |
docker run --rm \
-v "$PWD:/tmp/project" \
--entrypoint /bin/sh \
--platform "$DOCKER_PLATFORM" \
node:24.19.0-alpine \
-c 'test "$(npm --version)" = "11.17.0" && cd /tmp/project && npm ci --ignore-scripts && npm run release:install-package -- --project-root . --artifact-dir package-artifact --install-root package-install'
- name: Load the packed package on the target Node.js runtime
env:
DOCKER_PLATFORM: ${{ matrix.platform }}
NODE_VERSION: ${{ matrix.node-version }}
run: |
docker run --rm \
-v "$PWD:/tmp/project" \
--entrypoint /bin/sh \
--platform "$DOCKER_PLATFORM" \
"node:${NODE_VERSION}-alpine" \
-c 'cd /tmp/project && npm run release:load-package -- --project-root . --install-root package-install'
- run: git diff --exit-code
stage:
name: Stage package on npm
needs:
- validate
- pack
- verify-package-mac-win
- verify-package-ubuntu
- verify-package-alpine
runs-on: ubuntu-24.04
env:
EXPECTED_PACKAGE_NAME: ${{ needs.validate.outputs.package_name }}
EXPECTED_TAG_OBJECT_SHA: ${{ needs.validate.outputs.tag_object_sha }}
EXPECTED_VERSION: ${{ needs.validate.outputs.version }}
permissions:
contents: read
id-token: write # Authenticate npm Trusted Publishing with OIDC.
steps:
# This job deliberately has no checkout, no cache, no project dependency
# install, no repository secret, and no third-party action: it is the only
# job that can publish, so it runs nothing from the repository.
- name: Download the exact package
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
name: npm-package-${{ github.ref_name }}
path: package-artifact/
- name: Install the release Node.js toolchain
uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
with:
node-version: 24.19.0
package-manager-cache: false
registry-url: https://registry.npmjs.org
- name: Verify the release npm version
run: test "$(npm --version)" = "11.17.0"
- name: Stage the npm package
env:
GH_TOKEN: ${{ github.token }}
REF_NAME: ${{ github.ref_name }}
run: |
# download-artifact already verified this artifact's digest. What is
# left to check is identity: that the one tarball here is the package
# this tag promised.
TARBALL_COUNT="$(find package-artifact -maxdepth 1 -type f -name '*.tgz' | wc -l)"
if [[ "$TARBALL_COUNT" -ne 1 ]]; then
echo "::error::Expected exactly one package tarball, found $TARBALL_COUNT"
exit 1
fi
PACK_FILENAME="$(node -e '
const { readFileSync } = require("node:fs")
const entries = JSON.parse(readFileSync("package-artifact/PACK.json", "utf8"))
if (entries.length !== 1) throw new Error("Expected one pack record, found " + entries.length)
const entry = entries[0]
if (entry.name !== process.env.EXPECTED_PACKAGE_NAME ||
entry.version !== process.env.EXPECTED_VERSION) {
throw new Error("Unexpected pack identity: " + entry.name + "@" + entry.version)
}
process.stdout.write(entry.filename)
')"
TARBALL="package-artifact/$PACK_FILENAME"
if [[ ! -f "$TARBALL" ]]; then
echo "::error::Pack record names $PACK_FILENAME, which is not here"
exit 1
fi
MANIFEST_PATH="$RUNNER_TEMP/packed-package.json"
tar -xOf "$TARBALL" package/package.json > "$MANIFEST_PATH"
MANIFEST_PATH="$MANIFEST_PATH" node -e '
const { readFileSync } = require("node:fs")
const pkg = JSON.parse(readFileSync(process.env.MANIFEST_PATH, "utf8"))
if (pkg.name !== process.env.EXPECTED_PACKAGE_NAME ||
pkg.version !== process.env.EXPECTED_VERSION) {
throw new Error("Unexpected packed manifest: " + pkg.name + "@" + pkg.version)
}
'
REF_OBJECT_TYPE="$(
gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$REF_NAME" --jq '.object.type'
)"
TAG_OBJECT_SHA="$(
gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$REF_NAME" --jq '.object.sha'
)"
TAG_TARGET_TYPE="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.object.type'
)"
TAG_TARGET_SHA="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.object.sha'
)"
TAG_VERIFIED="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.verification.verified'
)"
COMMIT_VERIFIED="$(
gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA" --jq '.commit.verification.verified'
)"
if [[ "$REF_OBJECT_TYPE" != tag || \
"$TAG_OBJECT_SHA" != "$EXPECTED_TAG_OBJECT_SHA" || \
"$TAG_TARGET_TYPE" != commit || "$TAG_TARGET_SHA" != "$GITHUB_SHA" || \
"$TAG_VERIFIED" != true || "$COMMIT_VERIFIED" != true ]]; then
echo "::error::Tag $REF_NAME moved or lost its verified release identity"
exit 1
fi
npm stage publish "./$TARBALL" --ignore-scripts
github-release:
name: Create immutable GitHub release
needs:
- validate
- stage
runs-on: ubuntu-24.04
permissions:
contents: write # Create the immutable release for the signed tag.
steps:
- name: Create GitHub release
env:
EXPECTED_TAG_OBJECT_SHA: ${{ needs.validate.outputs.tag_object_sha }}
GH_TOKEN: ${{ github.token }}
TAG: ${{ github.ref_name }}
run: |
REF_OBJECT_TYPE="$(
gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$TAG" --jq '.object.type'
)"
TAG_OBJECT_SHA="$(
gh api "repos/$GITHUB_REPOSITORY/git/ref/tags/$TAG" --jq '.object.sha'
)"
TAG_TARGET_TYPE="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.object.type'
)"
TAG_TARGET_SHA="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.object.sha'
)"
TAG_VERIFIED="$(
gh api "repos/$GITHUB_REPOSITORY/git/tags/$TAG_OBJECT_SHA" --jq '.verification.verified'
)"
COMMIT_VERIFIED="$(
gh api "repos/$GITHUB_REPOSITORY/commits/$GITHUB_SHA" --jq '.commit.verification.verified'
)"
if [[ "$REF_OBJECT_TYPE" != tag || "$TAG_TARGET_TYPE" != commit || \
"$TAG_OBJECT_SHA" != "$EXPECTED_TAG_OBJECT_SHA" || \
"$TAG_TARGET_SHA" != "$GITHUB_SHA" || "$TAG_VERIFIED" != true || \
"$COMMIT_VERIFIED" != true ]]; then
echo "::error::Tag $TAG moved or lost its verified release identity"
exit 1
fi
gh release create "$TAG" \
--repo "$GITHUB_REPOSITORY" \
--verify-tag \
--generate-notes