diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7057cd6..471a0ef 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,26 +1,48 @@ -# Release: pushing a version tag (vX.Y.Z) builds, tests and *stages* every OneTaxonomy package on npm. -# Nothing goes public until the mdgilardi npm account, which owns the @extantape scope, approves each with 2FA -# (npmjs.com → your avatar → Staged Packages → Approve, or `npm stage approve `). -# No token is stored: npm trusted publishing (OIDC) lets only this workflow stage. +# Release: pushing a version tag (vX.Y.Z) publishes the OneTaxonomy packages straight to npm. No approval step follows, +# so tag only a release that is ready to be public, and push its commit to main first. +# The build job checks the tag, tests, builds and packs every package. It holds no publishing rights. +# The publish job runs in the npm-publish environment and publishes the packed files with provenance +# through npm trusted publishing (OIDC). No token is stored. Confirm the release with `npm view`. +# A rerun skips each package whose version is already on npm and publishes the rest. +# The owner publishes a new package's first version by hand. name: Release on: push: tags: ['v*'] -permissions: - contents: read - id-token: write +permissions: {} jobs: - stage: + build: runs-on: ubuntu-latest + permissions: + contents: read steps: - - uses: actions/checkout@v6 - - uses: pnpm/action-setup@v6 - - uses: actions/setup-node@v6 + - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 + with: + fetch-depth: 0 + persist-credentials: false + # fetch-depth: 0 above brings every branch, so origin/main is here and the history is complete. + - name: Tagged commit is on main + run: | + set -euo pipefail + if [ "$(git rev-parse --is-shallow-repository)" != false ]; then + echo "::error::The checkout is shallow, so the main check can't run."; exit 1 + fi + COMMIT=$(git rev-parse --verify "$GITHUB_SHA^{commit}") + if [ "$(git rev-parse HEAD)" != "$COMMIT" ]; then + echo "::error::The checkout isn't the tagged commit."; exit 1 + fi + if ! MAIN=$(git rev-parse --verify --quiet "refs/remotes/origin/main^{commit}"); then + echo "::error::origin/main wasn't fetched, so the main check can't run."; exit 1 + fi + if ! git merge-base --is-ancestor "$COMMIT" "$MAIN"; then + echo "::error::$GITHUB_REF_NAME points at a commit that isn't on main. Push it to main first, then tag again."; exit 1 + fi + - uses: pnpm/action-setup@73e46992e05b19abd31f2f3c20e0e7370d762115 # v6.1.1 + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: node-version: 24 cache: pnpm - registry-url: https://registry.npmjs.org - - run: npm install -g npm@^11.15.0 + - run: npm install -g --ignore-scripts npm@11.21.3 - run: pnpm install --frozen-lockfile - name: GitHub description matches package.json env: @@ -31,22 +53,100 @@ jobs: - name: Tag matches every package run: for p in packages/*/; do test "${GITHUB_REF_NAME#v}" = "$(node -p "require('./${p}package.json').version")"; done - run: pnpm build && pnpm typecheck && pnpm test - - name: Stage on npm + - name: Pack every package run: | + set -euo pipefail VERSION=$(node -p "require('./packages/core/package.json').version") - # payload (@extantape/payload-plugin-onetaxonomy) isn't staged: its first npm version is published by hand - # by the owner, and trusted publishing is set up after that. Add it here then. + OUT="$RUNNER_TEMP/release" + mkdir -p "$OUT" + : > "$OUT/manifest.tsv" + # payload (@extantape/payload-plugin-onetaxonomy) is left out until the owner has published its first + # version by hand and set up its trusted publisher. Then remove the grep from this loop. for p in $(ls packages | grep -v -x payload); do NAME=$(node -p "require('./packages/$p/package.json').name") - # A prerelease goes to "alpha" once the package has a stable version; until then it is - # also "latest", so a plain install gets the newest build (not an old alpha). + # A prerelease goes to alpha once the package has a stable version. Until then it is also latest, + # so a plain install gets the newest build. Only E404 means the package isn't on npm yet. TAG=latest case "$VERSION" in *-*) - if npm view "$NAME" versions --json 2>/dev/null | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>process.exit([].concat(s?JSON.parse(s):[]).some(v=>typeof v==="string"&&!v.includes("-"))?0:1))'; then TAG=alpha; fi ;; + if VERSIONS=$(npm view "$NAME" versions --json 2>"$RUNNER_TEMP/npm-view.log"); then + [ -n "$VERSIONS" ] || { echo "::error::npm gave an empty answer for the versions of $NAME."; exit 1; } + if printf '%s' "$VERSIONS" | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{let v;try{v=[].concat(JSON.parse(s))}catch(e){process.exit(2)}process.exit(v.some(x=>typeof x==="string"&&!x.includes("-"))?0:1)})'; then + TAG=alpha + else + rc=$? + [ "$rc" = 1 ] || { echo "::error::npm gave an answer for the versions of $NAME that isn't a version list."; exit 1; } + fi + elif ! grep -qxE 'npm (error|ERR!) code E404' "$RUNNER_TEMP/npm-view.log"; then + cat "$RUNNER_TEMP/npm-view.log" + echo "::error::Can't tell which dist-tag $NAME needs."; exit 1 + fi ;; esac # pnpm pack turns workspace:^ into real versions. - (cd "packages/$p" && pnpm pack --pack-destination ../../dist-packages) - TGZ="dist-packages/$(echo "${NAME#@}" | tr / -)-$VERSION.tgz" - # "./" matters: without it npm reads "dist-packages/x.tgz" as a GitHub repository shorthand. - npm stage publish "./$TGZ" --access public --provenance --tag "$TAG" + (cd "packages/$p" && pnpm pack --pack-destination "$OUT") + TGZ="$(echo "${NAME#@}" | tr / -)-$VERSION.tgz" + [ -f "$OUT/$TGZ" ] || { echo "::error::pnpm pack didn't write $TGZ."; exit 1; } + printf '%s\t%s\t%s\t%s\n' "$NAME" "$VERSION" "$TAG" "$TGZ" >> "$OUT/manifest.tsv" done + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: release-packages + path: ${{ runner.temp }}/release/ + if-no-files-found: error + retention-days: 7 + # Publishes what the build job packed. It installs nothing from the project and runs none of its scripts. + publish: + needs: build + runs-on: ubuntu-latest + timeout-minutes: 10 + environment: npm-publish + permissions: + id-token: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: release-packages + path: release + - uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 + with: + node-version: 24 + registry-url: https://registry.npmjs.org + package-manager-cache: false + - run: npm install -g --ignore-scripts npm@11.21.3 + - name: Publish to npm + working-directory: release + run: | + set -euo pipefail + WANT="${GITHUB_REF_NAME#v}" + [ -s manifest.tsv ] || { echo "::error::The build job packed nothing."; exit 1; } + # Check every package and its tarball before publishing any of them. + while IFS=$'\t' read -r NAME VERSION TAG TGZ EXTRA <&3; do + if ! [[ "$NAME" =~ ^@extantape/[a-z0-9-]+$ ]] || [ "$VERSION" != "$WANT" ] || [ -n "$EXTRA" ]; then + echo "::error::Unexpected package in the build: $NAME $VERSION"; exit 1 + fi + # A stable version only ever goes to latest. A prerelease never does here. + case "$VERSION" in + *-*) [ "$TAG" = alpha ] || [ "$TAG" = latest ] ;; + *) [ "$TAG" = latest ] ;; + esac || { echo "::error::Unexpected dist-tag $TAG for $NAME@$VERSION"; exit 1; } + case "$TGZ" in */*|.*|"") echo "::error::Unexpected file name: $TGZ"; exit 1 ;; *.tgz) ;; + *) echo "::error::Unexpected file name: $TGZ"; exit 1 ;; + esac + [ -f "$TGZ" ] || { echo "::error::$TGZ is missing from the build."; exit 1; } + INSIDE=$(tar -xzOf "$TGZ" package/package.json | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{const p=JSON.parse(s);console.log(p.name+"@"+p.version)})') + [ "$INSIDE" = "$NAME@$VERSION" ] || { echo "::error::$TGZ holds $INSIDE, not $NAME@$VERSION."; exit 1; } + done 3< manifest.tsv + [ -z "$(cut -f1 manifest.tsv | sort | uniq -d)" ] || { echo "::error::A package appears twice in the build."; exit 1; } + # Every package checked out, so publish them in the order the build job packed them. + while IFS=$'\t' read -r NAME VERSION TAG TGZ <&3; do + # Skip only when npm returns exactly this version. E404 means it isn't there yet. + # Any other answer or error stops the job. + if SEEN=$(npm view "$NAME@$VERSION" version 2>"$RUNNER_TEMP/npm-view.log"); then + if [ "$SEEN" = "$VERSION" ]; then echo "::notice::$NAME@$VERSION is already on npm; skipping it."; continue; fi + if [ -n "$SEEN" ]; then echo "::error::Unexpected npm view answer for $NAME@$VERSION: $SEEN"; exit 1; fi + elif ! grep -qxE 'npm (error|ERR!) code E404' "$RUNNER_TEMP/npm-view.log"; then + cat "$RUNNER_TEMP/npm-view.log" + echo "::error::Can't tell whether $NAME@$VERSION is on npm."; exit 1 + fi + # "./" matters: without it npm reads a bare file name as a GitHub repository shorthand. + npm publish "./$TGZ" --ignore-scripts --access public --provenance --tag "$TAG" + done 3< manifest.tsv diff --git a/RELEASING.md b/RELEASING.md index fc79b65..1c74767 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -1,15 +1,23 @@ # Releasing -One description everywhere: each `package.json` (`description`, `keywords`) is the source; GitHub's description and topics are synced from the root `package.json` before each release, npm takes each package's at release, and the Release workflow refuses to stage if GitHub differs. +One description everywhere: each `package.json` (`description`, `keywords`) is the source; GitHub's description and topics are synced from the root `package.json` before each release, npm takes each package's at release, and the Release workflow refuses to publish if GitHub differs. 1. Bump the same `version` in every `packages/*/package.json` (lockstep, ADR 0007), update `CHANGELOG.md`, commit as `Release x.y.z`, tag `vx.y.z`, and push the commit and the tag. -2. The Release workflow builds, tests and **stages** every package on npm (trusted publishing, no stored token). Prereleases go to the `alpha` dist-tag once a package has a stable version; before that they are also `latest`. -3. The `mdgilardi` npm account, which owns the `@extantape` scope, approves each with 2FA: npmjs.com → avatar → **Staged Packages** → **Approve**, or `npm stage list` then `npm stage approve `. Only then are they public. +2. The Release workflow has two jobs. The build job checks that the tagged commit is on `main`, then tests, builds and packs every package except `payload`. The publish job publishes the packed files straight to npm with provenance, through trusted publishing (OIDC, no stored token). No approval step follows, so tag only a release that is ready to be public. Prereleases go to the `alpha` dist-tag once a package has a stable version; before that they are also `latest`. +3. Confirm each package with `npm view @x.y.z version`. It should print `x.y.z`. -`@extantape/payload-plugin-onetaxonomy` (`packages/payload`) moves in lockstep but isn't staged yet: the Release workflow skips it until the owner has published its first version by hand and set up its trust. Then remove `grep -v -x payload` from the workflow's stage step. +Every release note opens with a line saying whether it is a security release and what a host must do. -The first version of each package is published by hand (`pnpm publish --access public` in each `packages/*` folder, `core` first; pnpm turns `workspace:^` into the real version), because npm can only trust a workflow for a package that exists; then, once per package: +Running the workflow again skips each package whose version is already on npm and publishes the rest, so a rerun can finish a release that stopped partway. If npm can't say whether a version is there, the job fails. + +Pushing the `vX.Y.Z` tag publishes, so a session pushes it only after checks and the security review pass. Push the release commit to `main` before the tag. Sessions never run `npm publish` by hand and never handle npm sign-in or tokens. + +`@extantape/payload-plugin-onetaxonomy` (`packages/payload`) moves in lockstep, but the Release workflow leaves it out until the owner has published its first version by hand and set up its trusted publisher as below. Then remove `grep -v -x payload` from the pack step in the build job. + +The owner publishes a new package's first version by hand (`pnpm publish --access public` in each `packages/*` folder, `core` first; pnpm turns `workspace:^` into the real version), because npm can only trust a workflow for a package that exists; then, once per package: ```bash -npm trust github --repo ExtantApe/OneTaxonomy --file release.yml --allow-stage-publish --yes +npm trust github --repo ExtantApe/OneTaxonomy --file release.yml --environment npm-publish --allow-publish --yes ``` + +A new package's trusted publisher is set with this command. It allows direct publishing and names the `npm-publish` environment. A package that is already set up keeps its current trusted publisher until the owner replaces it. npm can't edit a trusted publisher, so replacing one means revoking the old one first, then running this command.