11# Publishes every package whose version the registry does not have yet, when a
2- # maintainer publishes a GitHub Release (#4472). The procedure is RELEASING.md.
2+ # maintainer publishes a GitHub Release (#4472, #4873). The procedure is
3+ # RELEASING.md, and the `release` skill.
34#
45# No job here computes, stamps or tags a version. The version in each
56# package.json / pyproject.toml on the released ref is the source of truth: it
89# release tag is only a label on tested code. So a tag can never disagree with
910# a version, and there is nothing to push back to a branch.
1011#
12+ # BUILD AND PUBLISH ARE SEPARATE JOBS, per package:
13+ #
14+ # build-* checks out the tag, installs dependencies, runs the tests, and
15+ # runs `pack:verify`: it builds the tarball or wheel, installs it
16+ # into an empty directory and boots the installed server. Holds no
17+ # `id-token` and no environment. Uploads the verified artifact.
18+ # publish-* downloads that artifact and hands it to the registry. Holds
19+ # `id-token: write` and the `release` environment. No checkout,
20+ # no dependency install, no build.
21+ #
22+ # The split is about what runs next to the OIDC token. Installing dependencies
23+ # runs their lifecycle scripts, and a build runs the toolchain; in a job that
24+ # can mint a publish credential, any of that code can publish. Here the only
25+ # things that run with the token are a pinned action or two and the registry
26+ # client. And since what is published is the artifact the build job uploaded,
27+ # every action in BOTH jobs is pinned to a commit SHA: `verify:action-pins`
28+ # enforces that for any job holding a credential and any job whose artifact
29+ # such a job downloads.
30+ #
1131# Every package is a matrix leg, and each leg decides for itself: a version
1232# already on the registry is SKIPPED, not failed. That makes a release
1333# idempotent and self-healing. A package whose publish failed is picked up by
1434# the next release with no special handling, and one that did not change costs
15- # a skipped leg.
35+ # a skipped leg. For npm the build leg decides (`npm-publish-guard.mjs`) and
36+ # uploads a `SKIP` marker instead of a tarball; for PyPI the registry decides
37+ # at upload (`skip-existing`).
1638#
1739# The trigger is `release: published` and nothing else, so editing a
1840# published Release's notes never re-runs publishing. A release event runs the
2749# Only a commit that is on `main` is published. A Release can be created for
2850# any tag on any commit, and the environment approval shows a reviewer a tag
2951# name, not what it points at, so `detect-packages` checks the released commit
30- # against `main` and every publish job needs it.
52+ # against `main` and every job after it needs it.
3153
3254name : Release
3355
3456on :
3557 release :
3658 types : [published]
3759
60+ # Nothing in this workflow needs more than read access by default; the two
61+ # publish jobs add `id-token: write` for themselves.
62+ permissions :
63+ contents : read
64+
3865jobs :
3966 detect-packages :
4067 timeout-minutes : 5
@@ -46,14 +73,14 @@ jobs:
4673 steps :
4774 # On a release event the default ref is the release's tag: the exact
4875 # commit the maintainer released. Full history, for the check below.
49- - uses : actions/checkout@v6
76+ - uses : actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
5077 with :
5178 fetch-depth : 0
5279
5380 # `main` only receives reviewed milestone merges, so "on main" is what
5481 # makes the released code reviewed code. A tag cut from `v2/main`, a
55- # feature branch or a fork's commit fails here, before any publish job
56- # is eligible to run.
82+ # feature branch or a fork's commit fails here, before any build or
83+ # publish job is eligible to run.
5784 - name : Refuse a release whose commit is not on main
5885 run : |
5986 git fetch --no-tags origin main
@@ -71,43 +98,37 @@ jobs:
7198 echo "npm_packages=$NPM" >> "$GITHUB_OUTPUT"
7299 echo "pypi_packages=$PYPI" >> "$GITHUB_OUTPUT"
73100
74- publish -npm :
101+ build -npm :
75102 timeout-minutes : 15
76103 needs : [detect-packages]
77104 if : ${{ needs.detect-packages.outputs.npm_packages != '[]' }}
78105 strategy :
79106 fail-fast : false
80107 matrix :
81108 package : ${{ fromJson(needs.detect-packages.outputs.npm_packages) }}
82- name : Publish ${{ matrix.package }} (npm)
83- environment : release
84- permissions :
85- contents : read
86- id-token : write # Required for npm trusted publishing (OIDC)
109+ name : Build ${{ matrix.package }} (npm)
87110 runs-on : ubuntu-latest
111+ env :
112+ PACKAGE : ${{ matrix.package }}
88113 steps :
89- - uses : actions/checkout@v6
114+ - uses : actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
90115
91- - uses : actions/setup-node@v7
116+ - uses : actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
92117 with :
93118 node-version : 22
94119 cache : npm
95- registry-url : " https://registry.npmjs.org"
96-
97- # OIDC trusted publishing requires npm >=11.5.1; Node 22's bundled npm is 10.x.
98- - name : Ensure npm CLI supports OIDC trusted publishing
99- run : npm install -g npm@^11.5.1
100120
101121 # The registry-diff guard: skip (not fail) a version that is already
102122 # published, and treat a never-published package as "publish it".
103123 - name : Check whether this version is already on npm
104124 id : guard
105- run : node scripts/npm-publish-guard.mjs "src/${{ matrix.package }} "
125+ run : node scripts/npm-publish-guard.mjs "src/$PACKAGE "
106126
127+ # This package plus the root, as in CI: `pack:verify` is a root script
128+ # and its MCP client is a root devDependency.
107129 - name : Install dependencies
108130 if : steps.guard.outputs.skip != 'true'
109- working-directory : src/${{ matrix.package }}
110- run : npm ci
131+ run : npm ci --workspace "src/$PACKAGE" --include-workspace-root
111132
112133 # Defense in depth: CI already validated the branch, but this tests the
113134 # exact ref being published, whatever path the tag took to get here.
@@ -116,72 +137,186 @@ jobs:
116137 working-directory : src/${{ matrix.package }}
117138 run : npm test --if-present
118139
119- - name : Build package
140+ # Packs the tarball (which builds it), installs it into an empty
141+ # directory and boots the installed server. The tarball it leaves in
142+ # `release-artifact/<package>/` is the one that gets published.
143+ - name : Pack, install and boot the tarball
120144 if : steps.guard.outputs.skip != 'true'
121- working-directory : src/${{ matrix.package }}
122- run : npm run build
145+ run : npm run pack:verify -- "$PACKAGE" --out release-artifact
123146
124- # Authenticates via OIDC trusted publishing (no token): each package's
125- # npm trusted publisher is bound to this workflow + the release environment.
147+ # An artifact is uploaded either way, so the publish leg always has
148+ # something to download; the marker is how it learns there is nothing
149+ # to publish.
150+ - name : Mark an already-published version as skipped
151+ if : steps.guard.outputs.skip == 'true'
152+ run : |
153+ mkdir -p "release-artifact/$PACKAGE"
154+ touch "release-artifact/$PACKAGE/SKIP"
155+
156+ - name : Upload the artifact for the publish job
157+ uses : actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
158+ with :
159+ name : npm-${{ matrix.package }}
160+ path : release-artifact/${{ matrix.package }}/
161+ if-no-files-found : error
162+ retention-days : 7
163+
164+ publish-npm :
165+ timeout-minutes : 10
166+ needs : [detect-packages, build-npm]
167+ # `!cancelled()` rather than the default `success()`: `build-npm` is one
168+ # job with a leg per package, so one package's failed build would
169+ # otherwise skip every package's publish. Each publish leg stands on its
170+ # own artifact instead, and the leg whose build failed has none to
171+ # download, so it fails there, before anything is published.
172+ if : ${{ !cancelled() && needs.detect-packages.result == 'success' && needs.detect-packages.outputs.npm_packages != '[]' }}
173+ strategy :
174+ fail-fast : false
175+ matrix :
176+ package : ${{ fromJson(needs.detect-packages.outputs.npm_packages) }}
177+ name : Publish ${{ matrix.package }} (npm)
178+ # The trusted-publisher binding keys off this workflow file and this
179+ # environment, so the environment stays on the job that publishes.
180+ # `build-npm` deliberately does not take it.
181+ environment : release
182+ permissions :
183+ # Required for npm trusted publishing (OIDC) and provenance. This job
184+ # runs no dependency code: see the header.
185+ id-token : write
186+ runs-on : ubuntu-latest
187+ steps :
188+ # No `cache: npm`: there is no checkout, so no lockfile to key it on, and
189+ # nothing here installs dependencies to cache.
190+ - uses : actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
191+ with :
192+ node-version : 22
193+ registry-url : " https://registry.npmjs.org"
194+
195+ # OIDC trusted publishing requires npm >= 11.5.1; Node 22's bundled npm
196+ # is 10.x. Pinned EXACTLY, not to a range: this install runs in the job
197+ # that holds `id-token: write`, so it is the one package this job
198+ # trusts, and a range would let whatever 11.x is current at release time
199+ # into it. `--ignore-scripts` keeps even its lifecycle scripts out.
200+ - name : Install the pinned npm CLI (OIDC trusted publishing)
201+ run : npm install -g --ignore-scripts npm@11.20.0
202+
203+ - name : Download the artifact built by the build job
204+ uses : actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
205+ with :
206+ name : npm-${{ matrix.package }}
207+ path : release-artifact
208+
209+ # Publishes the tarball `build-npm` verified: a file argument, so npm
210+ # runs no lifecycle scripts and builds nothing here. There is
211+ # deliberately no `NODE_AUTH_TOKEN`: publishing is tokenless, and an
212+ # empty token in `.npmrc` makes npm fail before it ever tries OIDC.
126213 - name : Publish package
127- if : steps.guard.outputs.skip != 'true'
128- working-directory : src/${{ matrix.package }}
129- run : npm publish --access public
130214 env :
131215 NPM_CONFIG_PROVENANCE : " true"
216+ run : |
217+ if [ -e release-artifact/SKIP ]; then
218+ echo "This version is already on npm; nothing to publish."
219+ exit 0
220+ fi
221+ # `./` is load-bearing: `npm publish` reads a bare `dir/file`
222+ # argument as a GitHub `owner/repo` shorthand and tries to clone it
223+ # instead of publishing the local tarball.
224+ set -- ./release-artifact/*.tgz
225+ if [ "$#" -ne 1 ] || [ ! -f "$1" ]; then
226+ echo "Expected exactly one tarball in release-artifact/, found: $*"
227+ exit 1
228+ fi
229+ # The tarball was built after dependency install scripts ran, so its
230+ # manifest is untrusted input here. npm merges a manifest's
231+ # `publishConfig` into its own config before the OIDC exchange, so an
232+ # injected registry or proxy could redirect this job's credential
233+ # flow. No package here declares one, so any present is refused, and
234+ # the registry is named on the command line as well.
235+ if tar -xOzf "$1" package/package.json | node -e "process.exit('publishConfig' in JSON.parse(require('fs').readFileSync(0, 'utf8')) ? 0 : 1)"; then
236+ echo "Refusing to publish: the tarball's package.json declares publishConfig"
237+ exit 1
238+ fi
239+ npm publish "$1" --access public --registry https://registry.npmjs.org/
132240
133- publish -pypi :
241+ build -pypi :
134242 timeout-minutes : 15
135243 needs : [detect-packages]
136244 if : ${{ needs.detect-packages.outputs.pypi_packages != '[]' }}
137245 strategy :
138246 fail-fast : false
139247 matrix :
140248 package : ${{ fromJson(needs.detect-packages.outputs.pypi_packages) }}
141- name : Publish ${{ matrix.package }} (PyPI)
142- environment : release
143- permissions :
144- contents : read
145- id-token : write # Required for trusted publishing
249+ name : Build ${{ matrix.package }} (PyPI)
146250 runs-on : ubuntu-latest
251+ env :
252+ PACKAGE : ${{ matrix.package }}
147253 steps :
148- - uses : actions/checkout@v6
254+ - uses : actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
149255
150256 - name : Install uv
151- uses : astral-sh/setup-uv@v7
257+ uses : astral-sh/setup-uv@37802adc94f370d6bfd71619e3f0bf239e1f3b78 # v7.6.0
152258
153259 - name : Set up Python
154- uses : actions/setup-python@v7
260+ uses : actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
155261 with :
156262 python-version-file : " src/${{ matrix.package }}/.python-version"
157263
158- - name : Install dependencies
159- working-directory : src/${{ matrix.package }}
160- run : uv sync --frozen --all-extras --dev
264+ - uses : actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
265+ with :
266+ node-version : 22
267+ cache : npm
161268
162- - name : Run pyright
163- working-directory : src/${{ matrix.package }}
164- run : uv run --frozen pyright
269+ # Defense in depth: the same per-server chain CI and the gate run
270+ # (locked sync, ruff, pyright, pytest, build), on the exact ref being
271+ # published.
272+ - name : Validate
273+ run : node scripts/validate-py.mjs "$PACKAGE"
165274
166- # Defense in depth: CI already validated the branch, but this tests the
167- # exact ref being published, whatever path the tag took to get here.
168- - name : Run tests
169- working-directory : src/${{ matrix.package }}
170- run : |
171- if [ -d "tests" ] || [ -d "test" ]; then
172- uv run --frozen pytest
173- else
174- echo "No tests directory, skipping"
175- fi
275+ # `pack:verify` is a root script and its MCP client is a root
276+ # devDependency. `--ignore-scripts` skips the TypeScript workspaces'
277+ # `prepare` builds, which a Python package has no use for.
278+ - name : Install the root tooling
279+ run : npm ci --ignore-scripts
176280
177- - name : Build package
178- working-directory : src/${{ matrix.package }}
179- run : uv build
281+ # Builds the wheel and sdist, installs the wheel into an empty
282+ # environment and boots the installed console script. The distributions
283+ # it leaves in `release-artifact/<package>/` are the ones published.
284+ - name : Build, install and boot the wheel
285+ run : npm run pack:verify -- "$PACKAGE" --out release-artifact
286+
287+ - name : Upload the artifact for the publish job
288+ uses : actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
289+ with :
290+ name : pypi-${{ matrix.package }}
291+ path : release-artifact/${{ matrix.package }}/
292+ if-no-files-found : error
293+ retention-days : 7
294+
295+ publish-pypi :
296+ timeout-minutes : 10
297+ needs : [detect-packages, build-pypi]
298+ # See `publish-npm`: one package's failed build must not block the rest.
299+ if : ${{ !cancelled() && needs.detect-packages.result == 'success' && needs.detect-packages.outputs.pypi_packages != '[]' }}
300+ strategy :
301+ fail-fast : false
302+ matrix :
303+ package : ${{ fromJson(needs.detect-packages.outputs.pypi_packages) }}
304+ name : Publish ${{ matrix.package }} (PyPI)
305+ environment : release
306+ permissions :
307+ id-token : write # Required for trusted publishing
308+ runs-on : ubuntu-latest
309+ steps :
310+ - name : Download the artifact built by the build job
311+ uses : actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
312+ with :
313+ name : pypi-${{ matrix.package }}
314+ path : dist
180315
181316 # `skip-existing` is the PyPI side of the registry-diff guard: an
182317 # already-published version is skipped, not failed.
183318 - name : Publish package to PyPI
184- uses : pypa/gh-action-pypi-publish@release/v1
319+ uses : pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # v1.14.2
185320 with :
186- packages-dir : src/${{ matrix.package }}/ dist
321+ packages-dir : dist
187322 skip-existing : true
0 commit comments