Repository navigation
184 lines (171 loc) · 9.26 KB
/
Copy pathpublish-npm.yml
File metadata and controls
184 lines (171 loc) · 9.26 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
# Publish the three public frontend packages to npm over trusted publishing.
#
# No credentials anywhere, and that is the design — the same bargain
# `publish-pypi.yml` makes with PyPI. `id-token: write` lets the job mint a
# short-lived OIDC token; npm exchanges it for an upload credential scoped to this
# workflow in this repository. There is no token to leak, rotate or commit, and no
# one-time password to type, which is what previously made an npm release a hand
# step somebody had to be awake for.
#
# The configuration lives on each package's npm settings page and names this
# workflow **by filename**. Renaming this file breaks the exchange, and npm's own
# documentation warns that a wrong field surfaces only when a publish is attempted
# — there is nothing that validates it beforehand. A rename must move both sides.
# The npm side also has an explicit "allow npm publish" permission distinct from
# staged publishing; without it the exchange succeeds and the upload is refused.
#
# ## Why this needs pnpm 11 or newer
#
# Two requirements meet in one command. `@visionset/ui-core` declares
# `"@visionset/annotator": "workspace:*"` and `"@visionset/media": "workspace:*"`, and
# only pnpm rewrites those to a concrete version as it packs — a publish that ships the
# literal `workspace:*` string is uninstallable by anybody. Separately, pnpm gained OIDC
# support in 11; pnpm 10 had none and could only publish with a credential. Before 11
# these two needs pulled in opposite directions and the job had to be done by hand.
#
# ## The ordering is load-bearing
#
# annotator and media are published before ui-core, because ui-core's rewritten
# dependencies name versions that have to exist on the registry already. annotator
# before media is arbitrary — neither depends on the other — kept alphabetical.
name: Publish to npm
# Manual only, like the PyPI publish, and dispatched against the release tag so
# what is published is the tagged tree rather than whatever a branch holds at the
# moment somebody pressed the button.
#
# Being dispatch-only it never reports on a pull request, so it must never be added
# to `main`'s required-checks ruleset — a required check that never reports leaves
# every pull request blocked forever with nothing red to read.
on:
workflow_dispatch:
inputs:
dry-run:
description: "Pack and check only; upload nothing. Set to false to publish."
required: true
default: true
type: boolean
# `auto` resolves through scripts/npm_dist_tag.mjs: `latest` for a stable version,
# and for a prerelease only while no stable version is published (otherwise
# `latest` would sit on an older beta). Once a stable version exists, a prerelease
# goes to `beta`, and naming `latest` for one is refused.
#
# npm's trusted publishing can only set a tag *at publish time*: OIDC authorises
# `npm publish` and nothing else, so `npm dist-tag add` cannot move a tag
# afterwards from this workflow (npm/cli#8547). The tag chosen here is the only
# one a release gets.
dist-tag:
description: "npm dist-tag; auto = latest for stable, beta for a prerelease once a stable exists"
required: true
default: auto
type: string
jobs:
publish:
name: Publish to npm
runs-on: ubuntu-latest
permissions:
# A `permissions:` block sets every scope it does not name to `none`, and
# `actions/checkout` authenticates with GITHUB_TOKEN — hence `contents`.
contents: read
id-token: write
steps:
# Every `uses:` here is pinned to a commit, for the reason spelled out in
# publish-pypi.yml: `id-token: write` is a job scope, so any step in this job
# can trade an OIDC token for the right to publish under the `@visionset`
# scope. A tag or a branch is a pointer its owner can move; a commit is not.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
# Nothing is pushed back from here — the packages leave over npm's API, not
# over git — so the token has no reason to stay in `.git/config`.
with:
persist-credentials: false
# v6.1.0 is the first release that can install pnpm 12, and the floating `v6`
# tag still points at a commit that cannot. reads packageManager from package.json
- uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7
with:
node-version-file: .nvmrc
cache: pnpm
# **Deliberately no `registry-url`.** Setting it makes this action write an
# `.npmrc` containing `_authToken=${NODE_AUTH_TOKEN}`. With no such variable
# in the environment the placeholder is never substituted and the literal
# string is sent as a bearer token, which takes precedence over the OIDC
# exchange and fails the publish with a 404 that says nothing about the real
# cause. The default registry is the public one, which is where these go.
- name: pnpm must be new enough to exchange an OIDC token
# OIDC landed in pnpm 11. An older pnpm does not fail on the handshake — it
# falls back to looking for a credential, finds none, and reports a missing
# token, which reads as a configuration problem rather than a version one.
run: |
have=$(pnpm --version)
if [ "$(printf '%s\n11.0.0\n' "$have" | sort -V | head -1)" != "11.0.0" ]; then
echo "::error::pnpm $have cannot perform the OIDC exchange; 11 or newer is required"
exit 1
fi
echo "pnpm $have, node $(node --version)"
- name: Install
run: pnpm install --frozen-lockfile
- name: The ref must be the release tag derived from VERSION
# `v` plus the npm spelling of VERSION, the tag the runbook names. A branch,
# or a tag that disagrees with the tagged tree, would publish bytes no
# release tag describes, and a published version cannot be replaced.
env:
REF_TYPE: ${{ github.ref_type }}
REF_NAME: ${{ github.ref_name }}
run: |
expected="v$(node -p 'require("./frontend/ui-core/package.json").version')"
if [ "$REF_TYPE" != "tag" ] || [ "$REF_NAME" != "$expected" ]; then
echo "::error::dispatch against the tag $expected, not $REF_TYPE $REF_NAME"
exit 1
fi
pnpm version:check
- name: Resolve the dist-tag
id: dist-tag
env:
REQUESTED: ${{ inputs.dist-tag }}
run: |
tag=$(node scripts/npm_dist_tag.mjs "$REQUESTED")
echo "publishing under $tag"
echo "tag=$tag" >> "$GITHUB_OUTPUT"
- name: Build
run: pnpm -r build
- name: A packed tarball must carry no workspace protocol
# The failure this whole shape exists to prevent, asserted on the bytes that
# would be uploaded rather than trusted. A package that ships `workspace:*`
# installs for nobody, and npm does not let a published version be replaced.
#
# Packed into a scratch directory and thrown away: this proves the rewrite
# happens, and the publish below packs again from the same tree.
run: |
pnpm --filter @visionset/annotator pack --pack-destination "$RUNNER_TEMP/check"
pnpm --filter @visionset/media pack --pack-destination "$RUNNER_TEMP/check"
pnpm --filter @visionset/ui-core pack --pack-destination "$RUNNER_TEMP/check"
for tarball in "$RUNNER_TEMP"/check/*.tgz; do
manifest=$(tar -xzOf "$tarball" package/package.json)
if printf '%s' "$manifest" | grep -q '"workspace:'; then
echo "::error::$(basename "$tarball") still declares a workspace: dependency"
printf '%s\n' "$manifest" | grep '"workspace:'
exit 1
fi
done
echo "no workspace: protocol in any tarball"
# No `NODE_AUTH_TOKEN`, no `.npmrc`, no `--provenance`: under trusted publishing
# npm generates and uploads provenance attestations by itself.
#
# The dist-tag reaches the shell through the environment rather than through
# `${{ }}` interpolation: an expression is pasted into the script before it
# runs, so a value carrying shell syntax would execute in a job that holds
# publish rights. `$DIST_TAG` is read by the shell as data instead.
- name: Publish @visionset/annotator
env:
DIST_TAG: ${{ steps.dist-tag.outputs.tag }}
DRY_RUN_FLAG: ${{ inputs.dry-run && '--dry-run' || '' }}
run: pnpm --filter @visionset/annotator publish --access public --tag "$DIST_TAG" --no-git-checks $DRY_RUN_FLAG
- name: Publish @visionset/media
env:
DIST_TAG: ${{ steps.dist-tag.outputs.tag }}
DRY_RUN_FLAG: ${{ inputs.dry-run && '--dry-run' || '' }}
run: pnpm --filter @visionset/media publish --access public --tag "$DIST_TAG" --no-git-checks $DRY_RUN_FLAG
- name: Publish @visionset/ui-core
env:
DIST_TAG: ${{ steps.dist-tag.outputs.tag }}
DRY_RUN_FLAG: ${{ inputs.dry-run && '--dry-run' || '' }}
run: pnpm --filter @visionset/ui-core publish --access public --tag "$DIST_TAG" --no-git-checks $DRY_RUN_FLAG