diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 46ff374..6d143fb 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -1,6 +1,6 @@ # Checks a pull request before it is merged: the tests publish.yml runs, and -# that every package is on npm already (scripts/new-to-npm.sh). A new -# package merged first turned every publish run red (#39's block-patterns). +# names any package new to npm (scripts/new-to-npm.sh), which the merge +# publishes with the NPM_TOKEN secret. name: pr on: @@ -31,5 +31,5 @@ jobs: - uses: actions/setup-node@v4 with: node-version: 24 - - name: every package is on npm + - name: packages new to npm run: scripts/new-to-npm.sh diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index a683f94..1d0ba3d 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,7 +1,11 @@ # Publishes every package whose version isn't on npm yet, from GitHub -# Actions with npm's trusted publishing (OIDC): no token and no one-time -# password. Each package names this workflow as its trusted publisher on -# npmjs.com (package → Settings → Trusted Publisher). +# Actions. npm tries trusted publishing (OIDC) first: a package that names +# this workflow as its Trusted Publisher on npmjs.com needs nothing else. +# What OIDC can't do, the NPM_TOKEN secret does: a package's first version +# (trusted publishing can't create one) and a package whose Trusted +# Publisher isn't set. NPM_TOKEN is an npm granular access token of a +# magpie-community owner: read and write to the @magpie-community scope, +# 2FA bypassed. With it nobody publishes by hand. name: publish on: @@ -31,3 +35,5 @@ jobs: run: HOME=$(mktemp -d) bun test - name: publish run: scripts/publish.sh + env: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/README.md b/README.md index 43aec47..07b4d0e 100644 --- a/README.md +++ b/README.md @@ -178,9 +178,31 @@ it can't. To list a plugin, add an entry with its npm `package`, a `summary` in English and Chinese (`{"en": …, "zh": …}`), and open a pull request. -A new package's first version is published by hand before its pull request -is merged: npm's trusted publishing, which publishes every later version -from `main`, can't create a package. A magpie-community npm owner runs -`scripts/publish.sh ` on the pull request's branch and sets -`.github/workflows/publish.yml` as the package's Trusted Publisher on -npmjs.com. The pull request's `on-npm` check fails until then. +## Publishing + +A version bump pushed to `main` publishes itself: +`.github/workflows/publish.yml` publishes every package whose version +isn't on npm yet. npm tries trusted publishing (OIDC) first, which needs +the package to name `publish.yml` as its Trusted Publisher on npmjs.com. +What that can't do, the repository's `NPM_TOKEN` secret does: a new +package's first version (trusted publishing can't create a package) and a +package whose Trusted Publisher isn't set. With the secret, merging a new +package publishes it within minutes, and nobody publishes by hand. + +`NPM_TOKEN` is set once by an owner of the magpie-community npm org: + +1. On npmjs.com: avatar → Access Tokens → Generate New Token → Granular + Access Token. Name it `magpie-community/plugins publish`, tick *Bypass + two-factor authentication*, give *Packages and scopes* Read and write + on the `@magpie-community` scope, and the longest expiry npm allows. +2. `gh secret set NPM_TOKEN -R magpie-community/plugins` and paste it. +3. Re-run the latest `publish` run, or `gh workflow run publish.yml -R + magpie-community/plugins`. + +npm's write tokens expire (90 days at most). When one has, the publish +run fails saying so; generate a new one and set the secret again. + +A pull request's `on-npm` check notes any package not on npm yet; with +`NPM_TOKEN` set it is published when the pull request is merged. Without +the secret, `scripts/publish.sh ` from a magpie-community npm owner +publishes it by hand. diff --git a/scripts/new-to-npm.sh b/scripts/new-to-npm.sh index a9fb06f..0f6b15e 100755 --- a/scripts/new-to-npm.sh +++ b/scripts/new-to-npm.sh @@ -1,22 +1,20 @@ #!/bin/sh -# Fails, naming them, when a package here has never been published to npm. -# Trusted publishing can't create a package, so one merged before its first -# version is on npm leaves every publish run on main red until someone -# publishes it by hand (scripts/publish.sh ) and sets publish.yml as -# its Trusted Publisher on npmjs.com. A pull request runs this, so that is -# done before the merge, not found after. +# Names any package here that has never been published to npm. Trusted +# publishing can't create a package; publish.yml does it with the NPM_TOKEN +# secret when the pull request is merged (README, Publishing). A pull +# request runs this so the reviewer knows a merge publishes a new name. set -e cd "$(dirname "$0")/.." export npm_config_registry=https://registry.npmjs.org for dir in packages/*/; do name=$(node -p "require('./${dir}package.json').name") if [ "$(npm view "$name" name 2>/dev/null)" != "$name" ]; then - echo "::error file=${dir}package.json::$name is not on npm yet, and trusted publishing can't create it. Before this is merged, a magpie-community npm owner publishes its first version by hand (scripts/publish.sh ) and sets .github/workflows/publish.yml as its Trusted Publisher on npmjs.com; then re-run this check." + echo "::notice file=${dir}package.json::$name is not on npm yet. Merging publishes its first version with the NPM_TOKEN secret (README, Publishing)." new="$new $name" fi done if [ -n "$new" ]; then echo "new to npm:$new" - exit 1 +else + echo "every package is on npm" fi -echo "every package is on npm" diff --git a/scripts/publish.sh b/scripts/publish.sh index f980d5d..5527d01 100755 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -4,18 +4,22 @@ # GitHub Actions runs it with no password on each version bump pushed to # main (.github/workflows/publish.yml, npm's trusted publishing). # One password covers the lot: it is sent with each publish while it lasts. -# Trusted publishing can't make a package npm has never seen: a new one's -# first version goes by hand (an npm account of the magpie-community org, -# `scripts/publish.sh `), and its Trusted Publisher is set after. Till -# then Actions passes it by, publishes the rest, and fails at the end -# naming it. So does a package whose publish npm refuses (one published -# by hand whose Trusted Publisher isn't set yet answers 404): the packages -# after it are still published. +# Trusted publishing can't make a package npm has never seen, and npm +# refuses (404) one whose Trusted Publisher isn't set. Actions publishes +# both with the NPM_TOKEN secret, which npm falls back to when OIDC fails. +# Without the secret Actions passes a new package by, publishes the rest, +# and fails at the end naming what it couldn't publish; by hand, +# `scripts/publish.sh ` from an npm account of the magpie-community +# org publishes it. set -e cd "$(dirname "$0")/.." # npm's own registry, whatever ~/.npmrc names (a mirror such as npmmirror # takes no publish) export npm_config_registry=https://registry.npmjs.org +# setup-node's .npmrc reads the token from NODE_AUTH_TOKEN +if [ -n "$NPM_TOKEN" ]; then + export NODE_AUTH_TOKEN="$NPM_TOKEN" +fi bun scripts/check.mjs otp="$1" for dir in packages/*/; do @@ -25,13 +29,17 @@ for dir in packages/*/; do echo "= $name@$version is on npm already" continue fi - if [ -n "$GITHUB_ACTIONS" ] && [ -z "$otp" ] && ! npm view "$name" name >/dev/null 2>&1; then - echo "::error::$name is not on npm yet: trusted publishing can't create it. Publish its first version by hand (scripts/publish.sh ), then set this workflow as its Trusted Publisher on npmjs.com." + if [ -n "$GITHUB_ACTIONS" ] && [ -z "$otp$NPM_TOKEN" ] && ! npm view "$name" name >/dev/null 2>&1; then + echo "::error::$name is not on npm yet: trusted publishing can't create it, and the repository has no NPM_TOKEN secret. Add one (README, Publishing) and re-run this workflow, or publish its first version by hand (scripts/publish.sh )." new="$new $name" continue fi if ! (cd "$dir" && npm publish --access public ${otp:+--otp="$otp"}); then - [ -n "$GITHUB_ACTIONS" ] && echo "::error::$name@$version wasn't published: is this workflow its Trusted Publisher on npmjs.com?" + if [ -n "$GITHUB_ACTIONS" ] && [ -n "$NPM_TOKEN" ]; then + echo "::error::$name@$version wasn't published with OIDC or NPM_TOKEN: the token may have expired (npm's write tokens last 90 days at most) or lost access to @magpie-community. Replace the NPM_TOKEN secret (README, Publishing)." + elif [ -n "$GITHUB_ACTIONS" ]; then + echo "::error::$name@$version wasn't published: this workflow isn't its Trusted Publisher on npmjs.com, and the repository has no NPM_TOKEN secret (README, Publishing)." + fi refused="$refused $name" fi done