Skip to content

Commit 4d125de

Browse files
MrPickeringclaude
andcommitted
Add an OIDC trusted-publishing release workflow
Publishing by hand needs an OTP, so it can only ever be done by a human at a keyboard, and npm now discourages the token-with-2FA-bypass alternative. This swaps both for a short-lived OIDC token npm verifies against this repository and this workflow file — a leaked secret cannot publish, and neither can a different workflow. Three guards, each able to fail before anything reaches the registry: the tag must match package.json, the version must not already exist, and the full test suite plus `enview protect` run on the exact tree being published. Runners ship an npm too old for OIDC, so it upgrades first — otherwise the failure reads like a permissions problem. Needs a one-time Trusted Publisher entry on npmjs.com; the steps are in the file header. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 3c1021b commit 4d125de

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
name: Release
2+
3+
# Publishes to npm via OIDC trusted publishing — no NPM_TOKEN, no OTP prompt.
4+
#
5+
# Why this exists: publishing by hand needs a one-time password, which means it can only ever be
6+
# done by a human at a keyboard, and the npm docs now discourage the token-with-2FA-bypass
7+
# alternative. Trusted publishing swaps both for a short-lived OIDC token that npm verifies
8+
# against this repository and this workflow file, so a leaked secret cannot publish and neither
9+
# can a different workflow.
10+
#
11+
# ONE-TIME SETUP (npmjs.com, per package — cannot be scripted):
12+
# Package → Settings → Trusted Publisher → GitHub Actions
13+
# Organization/user: pickbitsai Repository: enView
14+
# Workflow filename: release.yml Environment: (leave blank)
15+
#
16+
# To cut a release:
17+
# npm version patch|minor|major # commits and tags
18+
# git push --follow-tags
19+
#
20+
# The tag drives it. Every guard below can fail the run before anything is published — a release
21+
# gate that cannot refuse is not a gate.
22+
23+
on:
24+
push:
25+
tags: ['v*']
26+
workflow_dispatch:
27+
28+
permissions:
29+
contents: read
30+
id-token: write # required for OIDC; without it npm falls back to looking for a token
31+
32+
jobs:
33+
release:
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v4
37+
with:
38+
fetch-depth: 0
39+
40+
- uses: actions/setup-node@v4
41+
with:
42+
node-version: 22
43+
registry-url: https://registry.npmjs.org
44+
45+
# Runners ship an npm older than the one that understands OIDC trusted publishing.
46+
# Without this the publish fails with an auth error that reads like a permissions problem.
47+
- name: Use an npm that supports trusted publishing
48+
run: |
49+
npm install -g npm@latest
50+
npm --version
51+
52+
- run: npm ci
53+
54+
- name: Configure git identity for fixture repos
55+
run: |
56+
git config --global user.email "ci@example.test"
57+
git config --global user.name "ci"
58+
git config --global init.defaultBranch main
59+
60+
- run: npm test
61+
62+
# enview auditing enview, on the exact tree about to be published.
63+
- name: enview protect (this repo)
64+
run: node bin/enview.js protect . --depth 4
65+
66+
- name: Tag must match package.json version
67+
if: startsWith(github.ref, 'refs/tags/v')
68+
run: |
69+
TAG="${GITHUB_REF_NAME#v}"
70+
PKG="$(node -p 'require("./package.json").version')"
71+
echo "tag=$TAG package.json=$PKG"
72+
if [ "$TAG" != "$PKG" ]; then
73+
echo "::error::Tag $GITHUB_REF_NAME does not match package.json version $PKG."
74+
exit 1
75+
fi
76+
77+
- name: Version must not already be published
78+
run: |
79+
NAME="$(node -p 'require("./package.json").name')"
80+
VER="$(node -p 'require("./package.json").version')"
81+
# A 404 here is the success case: nothing published at this version yet.
82+
CODE="$(curl -s -o /dev/null -w '%{http_code}' "https://registry.npmjs.org/${NAME}/${VER}")"
83+
if [ "$CODE" = "200" ]; then
84+
echo "::error::${NAME}@${VER} is already published. Bump the version."
85+
exit 1
86+
fi
87+
echo "${NAME}@${VER} is unpublished (HTTP $CODE) — proceeding."
88+
89+
# Trusted publishing attaches provenance automatically.
90+
- name: Publish
91+
run: npm publish --access public
92+
93+
- name: Confirm it resolves
94+
run: |
95+
NAME="$(node -p 'require("./package.json").name')"
96+
VER="$(node -p 'require("./package.json").version')"
97+
# The package document lags behind a successful publish by minutes; poll rather than
98+
# failing a release that actually worked.
99+
for i in $(seq 1 20); do
100+
CODE="$(curl -s -o /dev/null -w '%{http_code}' "https://registry.npmjs.org/${NAME}/${VER}")"
101+
if [ "$CODE" = "200" ]; then echo "${NAME}@${VER} is live."; exit 0; fi
102+
echo "attempt $i: HTTP $CODE — waiting"; sleep 15
103+
done
104+
echo "::warning::Publish reported success but ${NAME}@${VER} has not propagated yet."

0 commit comments

Comments
 (0)