Skip to content

Commit e718eca

Browse files
authored
Merge pull request #5105 from modelcontextprotocol/v2/chore/5104-release-publish-bundled-npm
ci(release): publish with Node 24's bundled npm instead of installing one
2 parents c6f0cf7 + 28b468e commit e718eca

3 files changed

Lines changed: 219 additions & 10 deletions

File tree

‎.claude/skills/release/SKILL.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,5 +586,9 @@ ACTION=actions/checkout TAG=v6.1.0
586586
echo "uses: $ACTION@$(gh api "repos/$ACTION/commits/$TAG" --jq .sha) # $TAG"
587587
```
588588

589-
The same goes for the npm CLI the publish job installs (`npm@x.y.z` in
590-
`release.yml`): it is pinned exactly, and bumped deliberately.
589+
The same goes for the Node release the npm publish job runs (`node-version` in
590+
`publish-npm`, `release.yml`): it is pinned exactly, because its bundled npm is
591+
the CLI that publishes, and it stays on a release whose bundled npm is 11.5.1
592+
or later (the OIDC floor; the job checks it before publishing). Bump it
593+
deliberately, from a Node release whose npm version you have read in
594+
`https://nodejs.org/dist/index.json`.

‎.github/workflows/release.yml‎

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,33 @@ jobs:
213213
steps:
214214
# No `cache: npm`: there is no checkout, so no lockfile to key it on, and
215215
# nothing here installs dependencies to cache.
216+
# OIDC trusted publishing requires npm >= 11.5.1. Node 22 bundles npm
217+
# 10.x, so this job runs Node 24, whose bundled npm meets the floor, and
218+
# installs nothing: no package is fetched from the registry and run
219+
# next to `id-token: write` (#5104). Pinned EXACTLY, not to a major: the
220+
# runtime is the one thing this job trusts besides the pinned actions,
221+
# and a range would let whatever 24.x is current at release time in.
222+
# The build jobs keep Node 22; this job only reads a manifest and runs
223+
# `npm publish` on the verified tarball.
216224
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
217225
with:
218-
node-version: 22
226+
node-version: 24.21.0
219227
registry-url: "https://registry.npmjs.org"
220228

221-
# OIDC trusted publishing requires npm >= 11.5.1; Node 22's bundled npm
222-
# is 10.x. Pinned EXACTLY, not to a range: this install runs in the job
223-
# that holds `id-token: write`, so it is the one package this job
224-
# trusts, and a range would let whatever 11.x is current at release time
225-
# into it. `--ignore-scripts` keeps even its lifecycle scripts out.
226-
- name: Install the pinned npm CLI (OIDC trusted publishing)
227-
run: npm install -g --ignore-scripts npm@11.20.0
229+
# Fails closed before anything is published if the bundled npm is ever
230+
# below the OIDC floor (a different Node pin, or a runner override).
231+
- name: Check the bundled npm supports OIDC trusted publishing
232+
run: |
233+
NPM_VERSION="$(npm --version)"
234+
echo "node $(node --version), npm $NPM_VERSION"
235+
node -e '
236+
const [a, b, c] = process.argv[1].split(".").map(Number);
237+
const ok = a > 11 || (a === 11 && (b > 5 || (b === 5 && c >= 1)));
238+
if (!ok) {
239+
console.error("npm " + process.argv[1] + " is below 11.5.1, the floor for OIDC trusted publishing");
240+
process.exit(1);
241+
}
242+
' "$NPM_VERSION"
228243
229244
- name: Download the artifact built by the build job
230245
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1

‎scripts/release-npm-floor.test.mjs‎

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
// Regression tests for how `release.yml`'s `publish-npm` job gets its npm (#5104).
2+
//
3+
// That job holds the OIDC credential, so it installs nothing: it runs an
4+
// exactly pinned Node whose bundled npm meets the floor for OIDC trusted
5+
// publishing (11.5.1), and a step fails the job before anything is published
6+
// if that npm is ever below the floor. The step's comparator is inline shell
7+
// and JavaScript in the workflow file, which runs only after a Release is
8+
// published, so a later edit that let 11.5.0 through would otherwise surface
9+
// at release time.
10+
//
11+
// As in release-dist-tag.test.mjs, these tests read the step out of the
12+
// workflow file and run THAT, with `npm` on PATH replaced by a stub that
13+
// reports a chosen version. They also pin the job's shape: an exact Node
14+
// pin, the check ahead of the download and the publish, no step that
15+
// installs a package, and the `./` that makes `npm publish` read the tarball
16+
// as a local file. Without it npm reads `release-artifact/x.tgz` as GitHub
17+
// `owner/repo` shorthand and tries to clone it over SSH: the MCP Inspector's
18+
// first release through the same split publish job failed exactly that way
19+
// (modelcontextprotocol/inspector#2551).
20+
//
21+
// The step's shell is bash, so those tests are skipped where there is none.
22+
// Run via `npm run test:scripts`.
23+
24+
import { test } from "node:test";
25+
import assert from "node:assert/strict";
26+
import { spawnSync } from "node:child_process";
27+
import {
28+
chmodSync,
29+
mkdtempSync,
30+
readFileSync,
31+
rmSync,
32+
writeFileSync,
33+
} from "node:fs";
34+
import { tmpdir } from "node:os";
35+
import path from "node:path";
36+
import { fileURLToPath } from "node:url";
37+
import { parse } from "yaml";
38+
39+
const repoRoot = path.resolve(
40+
path.dirname(fileURLToPath(import.meta.url)),
41+
"..",
42+
);
43+
44+
const CHECK_STEP = "Check the bundled npm supports OIDC trusted publishing";
45+
46+
/** The `publish-npm` job's steps, as the workflow file declares them. */
47+
function publishSteps() {
48+
const workflow = parse(
49+
readFileSync(
50+
path.join(repoRoot, ".github", "workflows", "release.yml"),
51+
"utf8",
52+
),
53+
);
54+
const job = workflow.jobs["publish-npm"];
55+
assert.ok(job, "release.yml has no publish-npm job");
56+
return job.steps;
57+
}
58+
59+
const hasBash =
60+
process.platform !== "win32" &&
61+
spawnSync("bash", ["-c", "exit 0"]).status === 0;
62+
63+
/**
64+
* Run the workflow's npm floor check with `npm --version` reporting `version`.
65+
*
66+
* @param {string} version what the stub npm reports
67+
* @returns {{ status: number | null, stderr: string }}
68+
*/
69+
function runCheck(version) {
70+
const step = publishSteps().find((s) => s.name === CHECK_STEP);
71+
assert.ok(step, `publish-npm has no \`${CHECK_STEP}\` step`);
72+
const dir = mkdtempSync(path.join(tmpdir(), "npm-floor-"));
73+
try {
74+
const stub = path.join(dir, "npm");
75+
writeFileSync(stub, `#!/bin/sh\necho "${version}"\n`);
76+
chmodSync(stub, 0o755);
77+
const result = spawnSync("bash", ["-e", "-c", step.run], {
78+
encoding: "utf8",
79+
env: {
80+
...process.env,
81+
PATH: `${dir}${path.delimiter}${process.env.PATH}`,
82+
},
83+
});
84+
return { status: result.status, stderr: result.stderr };
85+
} finally {
86+
rmSync(dir, { recursive: true, force: true });
87+
}
88+
}
89+
90+
test(
91+
"the npm floor check accepts 11.5.1 and later",
92+
{ skip: !hasBash && "needs bash" },
93+
() => {
94+
for (const version of ["11.5.1", "11.6.0", "11.19.0", "12.0.0"]) {
95+
const { status, stderr } = runCheck(version);
96+
assert.equal(status, 0, `npm ${version} was refused: ${stderr}`);
97+
}
98+
},
99+
);
100+
101+
test(
102+
"the npm floor check refuses anything below 11.5.1",
103+
{ skip: !hasBash && "needs bash" },
104+
() => {
105+
for (const version of ["11.5.0", "11.4.9", "10.9.9", "9.0.0"]) {
106+
const { status, stderr } = runCheck(version);
107+
assert.notEqual(status, 0, `npm ${version} was accepted`);
108+
assert.match(stderr, /below 11\.5\.1/);
109+
}
110+
},
111+
);
112+
113+
test("publish-npm pins Node exactly, to a release whose npm meets the floor", () => {
114+
const setup = publishSteps().find((s) =>
115+
String(s.uses ?? "").startsWith("actions/setup-node@"),
116+
);
117+
assert.ok(setup, "publish-npm has no actions/setup-node step");
118+
const pin = String(setup.with?.["node-version"] ?? "");
119+
assert.match(
120+
pin,
121+
/^\d+\.\d+\.\d+$/,
122+
`node-version ${pin} is not an exact x.y.z pin`,
123+
);
124+
// Node 24.5.0 is the first release whose bundled npm is 11.5.1.
125+
const [major, minor] = pin.split(".").map(Number);
126+
assert.ok(
127+
major > 24 || (major === 24 && minor >= 5),
128+
`Node ${pin} bundles an npm below the OIDC floor`,
129+
);
130+
});
131+
132+
test("publish-npm checks npm before it downloads or publishes anything", () => {
133+
const names = publishSteps().map((s) => s.name ?? s.uses);
134+
const check = names.indexOf(CHECK_STEP);
135+
assert.notEqual(check, -1, `publish-npm has no \`${CHECK_STEP}\` step`);
136+
const download = names.findIndex((n) =>
137+
String(n).startsWith("Download the artifact"),
138+
);
139+
const publish = names.indexOf("Publish package");
140+
assert.ok(
141+
download !== -1 && publish !== -1,
142+
"publish-npm lost its download or publish step",
143+
);
144+
assert.ok(
145+
check < download && check < publish,
146+
`the npm check runs after: ${names.join(" > ")}`,
147+
);
148+
});
149+
150+
test("publish-npm installs no package next to the OIDC credential", () => {
151+
const install =
152+
/\b(?:npm|pnpm|yarn)\s+(?:install|i|ci|add|exec|dlx)\b|\bnpx\b|\bcorepack\b|\bpip3?\s+install\b|\buvx?\b/;
153+
for (const step of publishSteps()) {
154+
if (!step.run) continue;
155+
// Shell comments may name these tools in prose; only commands count.
156+
const commands = step.run
157+
.split("\n")
158+
.filter((line) => !line.trim().startsWith("#"))
159+
.join("\n");
160+
assert.doesNotMatch(
161+
commands,
162+
install,
163+
`publish-npm step \`${step.name}\` installs or runs a fetched package`,
164+
);
165+
}
166+
});
167+
168+
test("publish-npm hands npm the tarball as a ./ path, not a bare dir/file", () => {
169+
const step = publishSteps().find((s) => s.name === "Publish package");
170+
assert.ok(step, "publish-npm has no `Publish package` step");
171+
const commands = step.run
172+
.split("\n")
173+
.filter((line) => !line.trim().startsWith("#"))
174+
.join("\n");
175+
assert.match(
176+
commands,
177+
/^\s*set -- \.\/release-artifact\/\*\.tgz\s*$/m,
178+
"the publish step must resolve the tarball as ./release-artifact/*.tgz",
179+
);
180+
assert.doesNotMatch(
181+
commands,
182+
/set -- release-artifact\//,
183+
"a bare release-artifact/ path makes npm read the tarball as a GitHub repo",
184+
);
185+
assert.match(
186+
commands,
187+
/npm publish "\$1"/,
188+
"npm publish must be given the ./ path resolved above",
189+
);
190+
});

0 commit comments

Comments
 (0)