Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions .github/workflows/publish.yml

This file was deleted.

156 changes: 156 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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 <stage-id> # inspect'
echo 'npm stage approve <stage-id> # asks for 2FA'
echo '```'
} >> "$GITHUB_STEP_SUMMARY"
exit "$failed"
34 changes: 34 additions & 0 deletions RELEASING.md
Original file line number Diff line number Diff line change
@@ -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 <stage-id> # look at what will be published
npm stage approve <stage-id> # 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<version> <commit> -m "<version>"`, then `git push origin v<version>`.
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.
Loading