From 4a5f46953fced8ff06e71afd54ab66aa916c83ec Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Wed, 2 Dec 2026 13:48:35 -0800 Subject: [PATCH] Release through a stage-only trusted publisher, with a checklist Replaces the Publish workflow, which needed a stored NPM_TOKEN. npm is restricting tokens that bypass 2FA, and a token that can publish is the thing worth stealing. release.yml stages any version npm does not have, through npm's trusted publisher for this repository. A staged package is not public until a maintainer approves it with 2FA. RELEASING.md is the checklist. Co-Authored-By: Claude Fable 5.2 --- .github/workflows/publish.yml | 51 ----------- .github/workflows/release.yml | 156 ++++++++++++++++++++++++++++++++++ RELEASING.md | 34 ++++++++ 3 files changed, 190 insertions(+), 51 deletions(-) delete mode 100644 .github/workflows/publish.yml create mode 100644 .github/workflows/release.yml create mode 100644 RELEASING.md diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml deleted file mode 100644 index dd508f3..0000000 --- a/.github/workflows/publish.yml +++ /dev/null @@ -1,51 +0,0 @@ -name: Publish - -# Publishes the three packages to npm at the versions their manifests carry. -# Started by hand. With `dry_run` it packs and shows what would be published, -# and publishes nothing. -on: - workflow_dispatch: - inputs: - dry_run: - description: Pack and list the files, publish nothing - type: boolean - default: true - -permissions: - contents: read - -jobs: - publish: - name: Publish to npm - runs-on: ubuntu-latest - timeout-minutes: 30 - # A release is cut from main, never from a branch. - if: github.ref == 'refs/heads/main' - permissions: - contents: read - # For the provenance attestation npm records with each package. - id-token: write - steps: - - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - with: - persist-credentials: false - - - uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 - - - name: Install dependencies - run: pnpm install --frozen-lockfile - - - name: Build and test - run: pnpm test - - # `pnpm publish` writes each `workspace:*` dependency as the version it - # names, skips a version npm already has, and runs each package's - # `prepack` build. - - name: Authenticate to npm - if: ${{ !inputs.dry_run }} - run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> "$HOME/.npmrc" - env: - NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - - - name: Publish - run: pnpm -r publish --access public --no-git-checks ${{ inputs.dry_run && '--dry-run' || '--provenance' }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..ea834e9 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,156 @@ +name: release + +# Stages the three packages on npm when a commit on `main` carries a version npm +# does not have yet. Staging needs no 2FA and a staged package is not public: a +# maintainer approves each one on npmjs.com or with `npm stage approve`, which +# does need 2FA (RELEASING.md). This workflow never publishes, and its trusted +# publisher on npm must not allow `npm publish`; a stolen workflow can then +# stage a version but cannot make it public. +# +# The file name is load-bearing: npm's trusted publisher for each package names +# `release.yml` and the `publish` environment, and rejects a token minted by +# any other workflow or environment. + +on: + push: + branches: [main] + workflow_dispatch: + +# Each job declares what it needs; nothing is granted at the top. +permissions: {} + +# One release at a time, and never cancel one that is mid-stage. +concurrency: + group: release + cancel-in-progress: false + +jobs: + plan: + name: plan + if: github.ref == 'refs/heads/main' + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read # checkout + outputs: + stage: ${{ steps.versions.outputs.stage }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + # A package is staged when its version is not on npm. A version that is + # staged but not yet approved reads as unpublished here; staging it again + # fails loudly rather than replacing it. + - name: Find versions npm does not have + id: versions + shell: bash + run: | + set -euo pipefail + versions=() + missing=() + # In dependency order: scaffold depends on api, suite on both. + for dir in api scaffold suite; do + name=$(jq -r .name "$dir/package.json") + version=$(jq -r .version "$dir/package.json") + versions+=("$version") + code=$(curl -s -o /dev/null -w '%{http_code}' \ + "https://registry.npmjs.org/${name/\//%2F}/$version") + case "$code" in + 200) echo "$name@$version is on npm" ;; + 404) echo "$name@$version is not on npm"; missing+=("$dir") ;; + *) echo "::error::npm answered HTTP $code for $name@$version"; exit 1 ;; + esac + done + if [ "$(printf '%s\n' "${versions[@]}" | sort -u | wc -l)" -ne 1 ]; then + echo "::error::The packages must share one version, found: ${versions[*]}" + exit 1 + fi + echo "stage=${missing[*]:-}" >> "$GITHUB_OUTPUT" + + stage: + name: stage + needs: plan + if: needs.plan.outputs.stage != '' + runs-on: ubuntu-latest + timeout-minutes: 30 + # The npm trusted publisher names this environment. It admits only `main`. + environment: + name: publish + permissions: + contents: read # checkout + id-token: write # the OIDC token npm exchanges for a stage-only credential + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + + # npm refuses to run in this repository on any Node but the pinned one. + - uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 + with: + node-version-file: package.json + registry-url: https://registry.npmjs.org + + # Staging needs npm 11.15 or newer; trusted publishing needs 11.5.1. Node's + # bundled npm may be older, so install a pinned one only when it is. + - name: Use an npm that can stage + shell: bash + run: | + set -euo pipefail + need=11.15.0 + have=$(npm --version) + echo "runner npm: $have" + if [ "$(printf '%s\n%s\n' "$need" "$have" | sort -V | head -1)" != "$need" ]; then + npm install --global npm@11.19.1 + echo "using npm $(npm --version)" + fi + + - run: pnpm install --frozen-lockfile + + # What is staged passed the tests on this runner, in this job. + - run: pnpm test + + # pnpm packs, because it writes each `workspace:*` dependency as the + # version it names; npm then stages that archive unchanged. + - name: Pack and stage the packages + shell: bash + env: + STAGE: ${{ needs.plan.outputs.stage }} + run: | + set -uo pipefail + failed=0 + mkdir -p "$RUNNER_TEMP/packed" + { + echo "## Staged for approval" + echo + echo "Commit \`$GITHUB_SHA\`. Nothing is public until each package is approved with 2FA." + echo + } >> "$GITHUB_STEP_SUMMARY" + for dir in $STAGE; do + name=$(jq -r .name "$dir/package.json") + version=$(jq -r .version "$dir/package.json") + archive="$RUNNER_TEMP/packed/$(echo "$name" | sed 's/^@//; s#/#-#')-$version.tgz" + echo "::group::stage $name@$version" + if (cd "$dir" && pnpm pack --pack-destination "$RUNNER_TEMP/packed") && + npm stage publish "$archive" --access public; then + echo "- staged \`$name@$version\`" >> "$GITHUB_STEP_SUMMARY" + else + echo "::error::Could not stage $name@$version" + echo "- **failed** to stage \`$name@$version\`" >> "$GITHUB_STEP_SUMMARY" + failed=1 + fi + echo "::endgroup::" + done + { + echo + echo "Approve in the order listed (RELEASING.md):" + echo + echo '```sh' + echo 'npm stage list' + echo 'npm stage download # inspect' + echo 'npm stage approve # asks for 2FA' + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + exit "$failed" diff --git a/RELEASING.md b/RELEASING.md new file mode 100644 index 0000000..0a371b6 --- /dev/null +++ b/RELEASING.md @@ -0,0 +1,34 @@ +# Releasing + +The three packages, `@dor-tools/api`, `@dor-tools/scaffold`, and `@dor-tools/suite`, share one version. A release is a pull request that raises it; nothing else triggers one. + +## Checklist + +1. **Open the release pull request.** Raise `version` in `api/package.json`, `scaffold/package.json`, and `suite/package.json` to the same value, and move [CHANGELOG.md](CHANGELOG.md)'s Unreleased entries under that version with today's date. While the packages are 0.x: a patch for a fix or a compatible addition, a minor for a breaking change. +2. **Merge it once CI is green** on Linux, macOS, and Windows. +3. **Wait for the `release` workflow on `main`.** It finds each version npm does not have, runs the tests, and stages those packages. A staged package is not public. Its run summary lists what it staged. +4. **Approve each staged package, in this order: api, scaffold, suite.** `scaffold` depends on `api`, and `suite` on both, so this order keeps every published package installable. + + ```sh + npm stage list + npm stage download # look at what will be published + npm stage approve # asks for 2FA + ``` + + Run these outside this folder: npm refuses to run here on any Node but the pinned one. The same approvals are on npmjs.com under Staged Packages. +5. **Tag the released commit** and push the tag: `git tag -a v -m ""`, then `git push origin v`. +6. **Check [dor.tools/changelog](https://dor.tools/changelog/)** shows the new version. Cloudflare Pages redeploys the site on every push to `main`. +7. **Move Dormouse's pin.** While Dormouse reads this repository as its `dor-tools/` submodule, open a pull request there moving the pin to the tagged commit, with the compare link between the old and new commits in its description. + +## How publishing is locked down + +- **The workflow can stage and cannot publish.** Each package's trusted publisher on npmjs.com names this repository, the workflow file `release.yml`, and the environment `publish`, and leaves "can also publish directly with `npm publish`" unchecked. +- **The `publish` environment on GitHub admits only `main`.** +- **Each package requires 2FA and disallows tokens** (npmjs.com, the package's Settings, Publishing access). +- **No npm token is stored anywhere.** + +Renaming `release.yml` or the `publish` environment breaks the trusted publisher until npmjs.com is updated to match. + +## History + +Version 0.1.0 was published by hand from commit `73fb537`, before the trusted publisher existed.