Files
Catoandcatomean 192b9194e2 ci: unified tokenless trusted-publishing workflow (OIDC, provenance) (#5)
Replaces the tag-only publish gated on NPM_PUBLISHING with the same
publish.yml used in accountkit. Versions unchanged; no environment.

Co-authored-by: catomean <catomean@users.noreply.github.com>
2026-10-26 10:41:40 +01:00

166 lines
7.4 KiB
YAML

name: Publish
# 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.
#
# 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]
# 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: {}
# Never two at once, never cancelled: a cancelled publish is a half-shipped
# release.
concurrency:
group: publish
cancel-in-progress: false
jobs:
publish:
runs-on: ubuntu-latest
permissions:
# write: tag the released commit v<version> when the release came from
# main rather than from a pushed tag.
contents: write
# 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:
node-version: "24"
# 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
- name: What does npm have?
id: check
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 "publish=false" >> "$GITHUB_OUTPUT"
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
# 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"
# 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' && github.ref_type != 'tag'
env:
VERSION: ${{ steps.check.outputs.version }}
run: |
if git ls-remote --exit-code --tags origin "refs/tags/v$VERSION" >/dev/null; then
echo "→ v$VERSION already tagged."
else
git tag "v$VERSION" "$GITHUB_SHA"
git push origin "v$VERSION"
fi