ci(publish): the current Publish workflow, which can make a package's first npm release (#52)

@bitbaum/threadkit is a new npm name; npm trusts a publisher only for an
existing package, so the first version is published with the org token and
tokenless publishing is linked afterwards. The old copy here was OIDC-only and
failed on the merge of the rename (run 42220558994).


Claude-Session: https://claude.ai/code/session_019MDtmvpWNTLxXNfwUHS7MD

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
CatoandClaude authored and GitHub committed 2026-11-06 13:53:59 +01:00
1 parent cb2a422317
commit 5f39ada053
1 file changed
+184 -59
+184 -59
View File
@@ -1,35 +1,45 @@
# prettier-ignore
name: Publish
# Releasing is not a thing anyone does. Merge a version bump to main and the
# package ships: this workflow asks the registry whether package.json's version
# already exists, and publishes it if not.
# The same file in every @bitbaum package. A merged version bump releases
# itself; so does a pushed v* tag, and "Run workflow" re-tries by hand.
#
# Why that check rather than a tag trigger: a tag pushed by GITHUB_TOKEN does
# not start another workflow, so "push a tag, let publish.yml notice" silently
# never runs. Asking npm what is published is also idempotent — re-running this,
# or pushing a tag by hand, cannot double-publish or fail confusingly.
# Two ways to npm, picked per run:
#
# - The package is already on npm → tokenless. npmjs.com trusts exactly THIS
# workflow in THIS repo (Trusted Publishing, OIDC): no token exists, nothing
# to leak or rotate, and every release carries a signed provenance statement.
#
# - The package is NOT on npm yet → npm will not trust a publisher for a
# package that does not exist, so the first version needs the account once.
# Put a short-lived npm token in the org secret NPM_BOOTSTRAP_TOKEN (made on
# npmjs.com from a phone; see docs in bitbaum/fleet) and this job publishes
# the first version with it. Without the token it skips GREEN and says so, so
# a new package never paints CI red.
# NOTE (npm docs, `npm trust`): trust commands do NOT accept a granular token,
# not even one with "bypass 2FA" — they need the account's interactive 2FA.
# So the auto-link step below is best-effort and is EXPECTED to fall through
# to the printed fallback. The human step is then ONE form per package on
# npmjs.com (repo + "publish.yml"), or one local `npm trust` loop
# (bootstrap-local.sh, step 2). After that, every release is tokenless.
#
# Publishing is idempotent: a version already on the registry is a green
# no-op, so re-runs and repeat merges are harmless.
on:
push:
tags: ["v*"]
branches: [main]
tags: ['v*']
# The reconciler, and the reason this is reliable. A merge made by auto-merge
# uses GITHUB_TOKEN, and a push with that token starts no workflow — so the
# push trigger above silently does not fire for exactly the merges that matter.
# The schedule asks the registry the same idempotent question on a timer: is
# package.json's version published? If a release was missed by any means, it
# goes out within the hour without anyone noticing it was missed.
schedule:
- cron: '43 * * * *'
# The auto-merge sweep re-arms this after a GITHUB_TOKEN merge, which
# starts no push workflow on its own (rearm_workflows in auto-merge.yml).
workflow_dispatch:
inputs:
diagnose_auth:
description: Test the npm trusted-publisher link without publishing anything.
type: boolean
default: false
# One merge starts this workflow TWICE: once from the push (the sweep merges
# with a PAT, whose pushes do trigger workflows) and once from the sweep's
# workflow_dispatch re-arm, seconds later. Two runs asking the registry the
# same question at the same moment both hear "not published", both build, and
# the one that publishes second gets E403 — a red run that means nothing is
# wrong (bip-kit 0.2.6 and 0.2.7, 2026-09-11). Serialise them instead: the
# second run waits, then finds the version on the registry and skips. Never
# cancel — a cancelled publish is a half-shipped release.
# Never two at once, never cancelled: a cancelled publish is a half-shipped
# release.
concurrency:
group: publish
cancel-in-progress: false
@@ -38,76 +48,191 @@ jobs:
publish:
runs-on: ubuntu-latest
permissions:
# Tagging the released commit, so a version on the registry can always be
# traced back to the tree it was built from.
# write: tag the released commit v<version> when the release came from
# main rather than from a pushed tag.
contents: write
# npm provenance: proves on the registry that this tarball was built by
# this workflow from this commit.
# Required for OIDC publishing and npm provenance.
id-token: write
steps:
- uses: actions/checkout@v7
# pnpm version comes from "packageManager" in package.json (SSOT).
- uses: pnpm/action-setup@v6
# No registry-url: setup-node would write an _authToken line that makes
# npm skip the OIDC exchange whenever no token is set.
- uses: actions/setup-node@v7
with:
# npm >= 11.5.1 is required for trusted publishing (OIDC); Node 24 ships it.
node-version: '24'
registry-url: 'https://registry.npmjs.org'
node-version: "24"
- name: Is this version already on the registry?
# Trusted publishing and `npm trust` need npm >= 11.10; do not trust
# whatever the runner's Node happens to bundle.
- run: npm install -g npm@11
# "Run workflow" with diagnose_auth ticked asks npm whether it trusts
# THIS workflow for this package, and publishes nothing. HTTP 200 =
# linked; 404 = the one form on npmjs.com is still to be filled in. The
# response body holds a short-lived token, so it is never printed.
- name: Is the npm trusted publisher linked? (no publish)
if: inputs.diagnose_auth == true
run: |
node --input-type=module <<'NODE'
const url = new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL);
url.searchParams.set('audience', 'npm:registry.npmjs.org');
const identity = await fetch(url, {
headers: { authorization: `Bearer ${process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}` },
signal: AbortSignal.timeout(30000),
});
if (!identity.ok) throw new Error(`GitHub OIDC request failed: HTTP ${identity.status}`);
const { value: jwt } = await identity.json();
const claims = JSON.parse(Buffer.from(jwt.split('.')[1], 'base64url'));
console.log('Publisher identity:', JSON.stringify({ repository: claims.repository, workflow_ref: claims.workflow_ref, ref: claims.ref }));
const name = JSON.parse(await (await import('node:fs/promises')).readFile('package.json', 'utf8')).name;
const exchange = await fetch(`https://registry.npmjs.org/-/npm/v1/oidc/token/exchange/package/${encodeURIComponent(name)}`, {
method: 'POST', headers: { authorization: `Bearer ${jwt}` },
signal: AbortSignal.timeout(30000),
});
console.log(`npm trust exchange for ${name}: HTTP ${exchange.status}`);
await exchange.body?.cancel();
if (!exchange.ok) throw new Error('npm does not trust this workflow for this package yet');
NODE
- name: What does npm have?
id: check
if: inputs.diagnose_auth != true
env:
BOOTSTRAP: ${{ secrets.NPM_BOOTSTRAP_TOKEN != '' }}
run: |
name=$(node -p "require('./package.json').name")
version=$(node -p "require('./package.json').version")
echo "name=$name" >> "$GITHUB_OUTPUT"
echo "version=$version" >> "$GITHUB_OUTPUT"
if npm view "$name@$version" version >/dev/null 2>&1; then
echo "→ $name@$version is already published; nothing to do."
echo "publish=false" >> "$GITHUB_OUTPUT"
echo "✅ \`$name@$version\` is already published — nothing to do." >> "$GITHUB_STEP_SUMMARY"
else
echo "→ $name@$version is not on the registry; releasing it."
echo "✅ \`$name@$version\` is already on npm — nothing to do." >> "$GITHUB_STEP_SUMMARY"
elif npm view "$name" version >/dev/null 2>&1; then
echo "publish=true" >> "$GITHUB_OUTPUT"
echo "mode=oidc" >> "$GITHUB_OUTPUT"
elif [ "$BOOTSTRAP" = "true" ]; then
echo "publish=true" >> "$GITHUB_OUTPUT"
echo "mode=first" >> "$GITHUB_OUTPUT"
else
echo "publish=false" >> "$GITHUB_OUTPUT"
{
echo "⏸ \`$name\` is not on npm yet, and its first version needs the account once."
echo "Add the org secret NPM_BOOTSTRAP_TOKEN (a short-lived npm token) and run this workflow again."
echo "Meanwhile it installs from GitHub: \`pnpm add github:${GITHUB_REPOSITORY}\`"
} >> "$GITHUB_STEP_SUMMARY"
fi
- if: steps.check.outputs.publish == 'true'
run: pnpm install --frozen-lockfile --ignore-scripts
# verify includes the real-browser test (test/browser.mjs) where a
# package has one; without the browser it fails before reaching npm.
- if: steps.check.outputs.publish == 'true' && hashFiles('test/browser.mjs') != ''
run: pnpm exec playwright install --with-deps chromium
# Never publish something that would not have passed CI.
- if: steps.check.outputs.publish == 'true'
run: pnpm run verify
# Auth is trusted publishing (OIDC): npm accepts this job's identity token
# because the package's Trusted Publisher is pinned to exactly this repo
# and workflow. No npm token exists anywhere, so none can leak or expire.
- name: Publish to npm (OIDC, tokenless)
# Refuse to publish a tag whose version does not match package.json,
# rather than silently shipping the wrong number.
- name: Check tag matches package version
if: steps.check.outputs.publish == 'true' && github.ref_type == 'tag'
run: |
tag="${GITHUB_REF_NAME#v}"
pkg=$(node -p "require('./package.json').version")
if [ "$tag" != "$pkg" ]; then
echo "Tag v$tag does not match package.json version $pkg" >&2
exit 1
fi
- name: Publish to npm
if: steps.check.outputs.publish == 'true'
env:
NAME: ${{ steps.check.outputs.name }}
VERSION: ${{ steps.check.outputs.version }}
MODE: ${{ steps.check.outputs.mode }}
FIRST_TOKEN: ${{ secrets.NPM_BOOTSTRAP_TOKEN }}
run: |
if [ "$MODE" = "first" ]; then
# The one token-based publish in a package's life.
echo "//registry.npmjs.org/:_authToken=${FIRST_TOKEN}" > "$RUNNER_TEMP/.npmrc"
export NPM_CONFIG_USERCONFIG="$RUNNER_TEMP/.npmrc"
fi
if npm publish --access public --provenance; then
echo "🚀 Published \`$NAME@$VERSION\`." >> "$GITHUB_STEP_SUMMARY"
elif npm view "$NAME@$VERSION" version >/dev/null 2>&1; then
# Another run slipped in between the check and this publish: the
# version is on the registry, which is the goal state.
echo "✅ \`$NAME@$VERSION\` was published by a concurrent run." >> "$GITHUB_STEP_SUMMARY"
else
exit 1
fi
if [ "$MODE" = "first" ]; then
# From now on this workflow publishes without any token.
if npm trust github "$NAME" --file publish.yml --repo "$GITHUB_REPOSITORY" --allow-publish --yes; then
echo "🔗 Linked \`$NAME\` to this workflow — later releases need no token." >> "$GITHUB_STEP_SUMMARY"
else
{
echo "⚠️ Published. npm does not let a token link the workflow (expected)."
echo "Link it once on https://www.npmjs.com/package/$NAME/access → Trusted Publisher → GitHub Actions:"
echo "repository \`$GITHUB_REPOSITORY\`, workflow \`publish.yml\`, tick *Allow npm publish*."
} >> "$GITHUB_STEP_SUMMARY"
fi
fi
rm -f "$RUNNER_TEMP/.npmrc"
- name: Wait for published registry metadata
if: steps.check.outputs.publish == 'true'
env:
NAME: ${{ steps.check.outputs.name }}
VERSION: ${{ steps.check.outputs.version }}
run: |
if npm publish; then
echo "🚀 Published \`$NAME@$VERSION\`." >> "$GITHUB_STEP_SUMMARY"
elif npm view "$NAME@$VERSION" version >/dev/null 2>&1; then
# The check above and this publish are not atomic. If another run
# (a schedule tick, a hand dispatch) slipped between them, the
# version is on the registry — which is the goal state, not an error.
echo "→ $NAME@$VERSION was published by a concurrent run; nothing to do."
echo "✅ \`$NAME@$VERSION\` was published by a concurrent run — nothing to do." >> "$GITHUB_STEP_SUMMARY"
else
exit 1
fi
# npm accepts a release before its public metadata is readable. Keep
# this serialized job open until read-back succeeds, so the tag/main
# duplicate cannot try publishing a version still being processed.
node --input-type=module <<'NODE'
const { NAME: name, VERSION: version } = process.env;
const url = `https://registry.npmjs.org/${encodeURIComponent(name)}`;
let ready = false;
for (let attempt = 0; attempt < 60; attempt++) {
try {
const response = await fetch(url, {
headers: { 'cache-control': 'no-cache' },
signal: AbortSignal.timeout(10000),
});
if (response.ok) {
const metadata = await response.json();
const record = metadata.versions?.[version];
ready = record?.name === name && record.version === version &&
Boolean(record.dist?.integrity && record.dist?.tarball);
if (ready) break;
}
} catch {
// Network errors and temporarily incomplete metadata can settle.
}
if (attempt < 59) {
console.log(`Waiting for ${name}@${version} registry read-back (${attempt + 1}/60)`);
await new Promise(resolve => setTimeout(resolve, 10000));
}
}
if (!ready) throw new Error(`Publication accepted, but ${name}@${version} registry read-back did not become ready`);
console.log(`Verified registry metadata and artifact coordinates for ${name}@${version}`);
NODE
# A release from main gets its tag here, so every published version is
# still traceable to a commit by name.
- name: Tag the released commit
if: steps.check.outputs.publish == 'true'
if: steps.check.outputs.publish == 'true' && github.ref_type != 'tag'
env:
TAG: v${{ steps.check.outputs.version }}
VERSION: ${{ steps.check.outputs.version }}
run: |
# Tag after a successful publish, so a tag never claims a release that
# did not happen. Skipped silently if it already exists.
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "→ tag $TAG already exists"
if git ls-remote --exit-code --tags origin "refs/tags/v$VERSION" >/dev/null; then
echo "→ v$VERSION already tagged."
else
git tag "$TAG"
git push origin "$TAG"
git tag "v$VERSION" "$GITHUB_SHA"
git push origin "v$VERSION"
fi