From 182c4ef3c932731ac00f9eb03b17366caddc9907 Mon Sep 17 00:00:00 2001 From: Christoph Pader Date: Sat, 5 Dec 2026 17:24:18 +0100 Subject: [PATCH] ci(release): stage packages with npm stage publish Releases now stage every package with `npm stage publish`. A staged version stays private until a maintainer approves it with 2FA on npmjs.com, so a compromised workflow can no longer publish on its own. Verification waits for that approval, and an unapproved release resumes with release_tag. Co-Authored-By: Claude Opus 5.5 --- scripts/publish-release.test.ts | 23 ++++++++++------------- scripts/publish-release.ts | 18 +++++++++++++----- scripts/release-registry.ts | 6 +++--- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/scripts/publish-release.test.ts b/scripts/publish-release.test.ts index d78f0c59..f20cfd7b 100644 --- a/scripts/publish-release.test.ts +++ b/scripts/publish-release.test.ts @@ -52,7 +52,7 @@ test('publishing validates all packages and publishers before publishing in depe result.calls.filter((line) => line.startsWith('npm:')), releasePackages.map( (name) => - `npm:${name}:publish --access public --provenance --tag latest --workspaces=false --ignore-scripts`, + `npm:${name}:stage publish --access public --provenance --tag latest --workspaces=false --ignore-scripts`, ), ) }) @@ -83,14 +83,14 @@ test('a prerelease of packages without a stable release publishes to latest and for (const name of reactNativePackages) assert.ok( npm.includes( - `npm:${name}:publish --access public --provenance --tag alpha --workspaces=false --ignore-scripts`, + `npm:${name}:stage publish --access public --provenance --tag alpha --workspaces=false --ignore-scripts`, ), name, ) for (const name of nitroUIPackages) assert.ok( npm.includes( - `npm:${name}:publish --access public --provenance --tag latest --workspaces=false --ignore-scripts`, + `npm:${name}:stage publish --access public --provenance --tag latest --workspaces=false --ignore-scripts`, ), name, ) @@ -108,16 +108,10 @@ test('a prerelease of packages without a stable release publishes to latest and ) }) -test('an npm without trusted dist-tag support prevents publishing a prerelease to latest', () => { - const result = runPublication( - { - PUBLISH_TEST_PRERELEASE_LATEST: '11.0.0-alpha.0', - PUBLISH_TEST_NPM_VERSION: '11.19.1', - }, - '11.0.0-alpha.1', - ) +test('an npm without staged publishing and trusted dist-tag support prevents every upload', () => { + const result = runPublication({ PUBLISH_TEST_NPM_VERSION: '11.19.1' }) assert.notEqual(result.status, 0) - assert.match(result.stderr, /npm 11\.19\.1 cannot move dist-tags/) + assert.match(result.stderr, /npm 11\.19\.1 cannot stage packages/) assert.equal( result.calls.some((line) => line.startsWith('npm:')), false, @@ -254,7 +248,10 @@ test('a package without repository metadata prevents publishing any package', () test('other npm publish failures stop before the next package', () => { const result = runPublication({ PUBLISH_TEST_CORE_FAILS: '1' }) assert.notEqual(result.status, 0) - assert.match(result.stderr, /react-native-nitro-sqlite: npm publish failed/) + assert.match( + result.stderr, + /react-native-nitro-sqlite: npm stage publish failed/, + ) assert.equal(result.calls.filter((line) => line.startsWith('npm:')).length, 1) }) diff --git a/scripts/publish-release.ts b/scripts/publish-release.ts index 1a4e386c..4fa4921f 100644 --- a/scripts/publish-release.ts +++ b/scripts/publish-release.ts @@ -55,15 +55,17 @@ export async function publishRelease(version: string): Promise { const npmVersion = execFileSync('npm', ['--version'], { encoding: 'utf8', }).trim() - if (prereleaseOnly.size && compareVersions(npmVersion, '11.21.0') < 0) + if (compareVersions(npmVersion, '11.21.0') < 0) throw new Error( - `npm ${npmVersion} cannot move dist-tags with trusted publishing. Use npm 11.21.0 or newer.`, + `npm ${npmVersion} cannot stage packages and move dist-tags with trusted publishing. Use npm 11.21.0 or newer.`, ) for (const name of pending) { // npm's CLI exchanges GitHub OIDC credentials and records provenance without a publishing token. + // A staged version stays private until a maintainer approves it with 2FA on npmjs.com. const result = spawnSync( 'npm', [ + 'stage', 'publish', '--access', 'public', @@ -82,12 +84,18 @@ export async function publishRelease(version: string): Promise { ) const stderr = result.stderr ?? '' process.stderr.write(stderr) - // npm hides a new version for several minutes and rejects a second publish of it until then. + // npm rejects staging a version again until a maintainer approves or rejects the earlier one. const stagedByEarlierAttempt = /previously staged version/.test(stderr) if (result.status !== 0 && !stagedByEarlierAttempt) - throw new Error(`${name}: npm publish failed.`, { cause: result.error }) + throw new Error(`${name}: npm stage publish failed.`, { + cause: result.error, + }) } - // Verify after publishing every package, so npm's processing delays overlap. + if (pending.length) + console.log( + `Approve the staged ${version} packages in npmjs.com's Staged Packages tab.`, + ) + // Verify after staging every package, so one approval covers them all. for (const name of pending) await verifyPublication(name, version, commit) // Retries tag already published versions too. Each package's trusted publisher needs "Allow npm dist-tag". for (const name of prereleaseOnly) { diff --git a/scripts/release-registry.ts b/scripts/release-registry.ts index ab0899b9..347973a9 100644 --- a/scripts/release-registry.ts +++ b/scripts/release-registry.ts @@ -138,7 +138,7 @@ export async function checkTrustedPublishers( } } -/** Confirm the published version records the selected commit, allowing registry propagation. +/** Confirm the published version records the selected commit, allowing approval and registry propagation. * Registry and connection errors while waiting are retried until the deadline. * @throws After 20 minutes if the version is missing or unreadable, or at once if its commit differs. */ @@ -148,7 +148,7 @@ export async function verifyPublication( commit: string, pollMs = 5_000, ): Promise { - // npm hides a new version while it processes it. 10.3.0 took over a minute and 11.0.0-nitro-v1.0 about four. + // A staged version appears after a maintainer approves it, and npm can take several minutes to process it. const deadline = Date.now() + 20 * 60_000 let lastError: unknown while (Date.now() < deadline) { @@ -164,7 +164,7 @@ export async function verifyPublication( await new Promise((resolve) => setTimeout(resolve, pollMs)) } throw new Error( - `${name}: published version could not be verified. Resume with release_tag.`, + `${name}: published version could not be verified. Approve its staged version on npmjs.com, then resume with release_tag.`, { cause: lastError }, ) }