Files
pi2d/.github/workflows/release.yml
T
yuez ef0d9b9125 ci: publish npm with Trusted Publishing instead of a token
The release job now authenticates with the GitHub OIDC token: `id-token: write`,
no NPM_TOKEN anywhere near the publish step, and a guard that fails when a
leftover repository secret is still mapped in. npm 11.5.1+ is required for
trusted publishing and setup-node's node 22 ships npm 10, so the job upgrades npm
and asserts npm/node versions plus the presence of the OIDC endpoint before
publishing. Provenance then comes for free.

docs/releasing.md documents the unavoidable bootstrap: npm only offers the
trusted-publisher settings page once the package exists, so the first version is
published from the maintainer's machine and every later one from CI.
2026-10-23 10:08:15 +08:00

136 lines
5.0 KiB
YAML

name: release
on:
push:
tags: ["v*"]
# Authentication is npm Trusted Publishing (OIDC): the npm CLI picks up the
# GitHub Actions OIDC token and exchanges it for a short-lived publish token.
# The publish step deliberately gets no NPM_TOKEN / NODE_AUTH_TOKEN env.
# See docs/releasing.md.
permissions:
# `contents: write` is for the `softprops/action-gh-release` step below, which
# creates the GitHub release; with `contents: read` it fails with 403. Kept at
# workflow level (single job) so the release step does not need a second job
# with its own checkout/install/test cycle.
contents: write
id-token: write # mint the OIDC token npm exchanges for publish rights
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10
- uses: actions/setup-node@v4
with:
node-version: 22
registry-url: https://registry.npmjs.org
cache: pnpm
cache-dependency-path: |
pnpm-lock.yaml
web/pnpm-lock.yaml
- name: Tag must match package.json version
run: |
tag="${GITHUB_REF_NAME#v}"
pkg="$(node -p 'require("./package.json").version')"
if [ "$tag" != "$pkg" ]; then
echo "tag $GITHUB_REF_NAME does not match package.json $pkg" >&2
exit 1
fi
- name: Install deps
run: |
pnpm install --frozen-lockfile
pnpm --dir web install --frozen-lockfile
- name: Test
run: |
pnpm typecheck
pnpm test
pnpm --dir web exec vitest run
# A leftover automation token would silently win over OIDC and hide a
# broken trusted-publisher configuration, so fail loudly instead.
# Repository secrets are NOT exported as environment variables on their
# own, so they are mapped into THIS step's env explicitly: otherwise the
# guard would see nothing and pass while the publish step (which gets no
# env at all) would be the first place a stale token could leak in.
- name: Refuse token-based publishing
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
run: |
for var in NODE_AUTH_TOKEN NPM_TOKEN; do
if [ -n "${!var:-}" ]; then
echo "::error::env var $var is set. This workflow publishes with npm Trusted Publishing (OIDC) and must run without a token. Delete the repository secret and re-run."
exit 1
fi
done
echo "no publish token present — OIDC trusted publishing only"
# actions/setup-node's node 22 ships npm 10.x, which cannot do trusted
# publishing (needs >= 11.5.1).
- name: Upgrade npm
run: npm install -g npm@11
- name: Preflight — npm/node support trusted publishing
run: |
node <<'EOF'
const { execSync } = require("node:child_process");
const atLeast = (have, want) => {
const a = have.split(".").map(Number);
const b = want.split(".").map(Number);
for (let i = 0; i < Math.max(a.length, b.length); i++) {
const x = a[i] || 0;
const y = b[i] || 0;
if (x !== y) return x > y;
}
return true;
};
const fail = [];
const npm = execSync("npm --version").toString().trim();
const node = process.version.replace(/^v/, "");
console.log(`npm ${npm} / node ${node}`);
if (!atLeast(npm, "11.5.1")) {
fail.push(`npm ${npm} is too old for trusted publishing (need >= 11.5.1); did 'npm install -g npm@11' run?`);
}
if (!atLeast(node, "22.14.0")) {
fail.push(`node ${node} is too old for trusted publishing (need >= 22.14.0)`);
}
if (!process.env.ACTIONS_ID_TOKEN_REQUEST_URL) {
fail.push("no OIDC token endpoint in the environment; 'permissions: id-token: write' is missing or the job is not running on GitHub Actions");
}
if (fail.length) {
for (const f of fail) console.error(`::error::${f}`);
process.exit(1);
}
console.log("trusted publishing preflight ok (OIDC endpoint present)");
EOF
# `prepack` builds dist/ and web/dist/ as part of pack+publish, so the
# published tarball is always built from the tagged source; no separate
# build step is needed here. No --provenance: provenance is automatic for
# a public package published from a public repo via OIDC.
# Deliberately no `env:` on this step — OIDC only.
- name: Publish to npm (trusted publishing, no token)
run: npm publish --access public
- name: Create GitHub release
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
body: |
`npm i -g pi2d@${{ github.ref_name }}`
Compatible pi2.nvim: protocol v1 (see `docs/protocol.md`).