publish: every version is packed onto the dist branch, so a new plugin merges without waiting for npm

A new package's first npm version can't go through trusted publishing
(a Trusted Publisher is set on a package that exists, with an owner's
2FA), so reviewed plugin PRs (#84 and others) sat unmerged until an owner
published by hand. Now publish.yml's dist job packs every package's
version (scripts/dist.mjs, npm pack --ignore-scripts) onto the dist
branch: tgz/<file> and index.json with each tarball's sha512. magpie
installs a @magpie-community package from there when npm doesn't have
the version. A new package without NPM_TOKEN is a notice, not a failed
run; a version npm refuses still fails it, now naming the stage-only
default of trusts made since 2026-09-03 and --allow-publish.

Run: node scripts/dist.mjs into an empty folder packed all 22 packages
(index.json 164K); the zed tarball's sha512 equals npm pack's integrity;
a second run packs nothing. The workflow itself is tried on main after
this push (workflow_dispatch).
This commit is contained in:
yetone
2026-10-15 04:42:51 +08:00
parent 3351e47715
commit f5308cdb0f
6 changed files with 158 additions and 50 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
# Checks a pull request before it is merged: the tests publish.yml runs, and
# names any package new to npm (scripts/new-to-npm.sh), which the merge
# publishes with the NPM_TOKEN secret.
# names any package new to npm (scripts/new-to-npm.sh). A new package
# doesn't hold the merge: magpie installs it from the dist branch.
name: pr
on:
+59 -13
View File
@@ -1,11 +1,14 @@
# Publishes every package whose version isn't on npm yet, from GitHub
# 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.
# Ships every package's version two ways, from GitHub Actions:
# - dist: packs it onto the `dist` branch (scripts/dist.mjs), where magpie
# installs it from when npm doesn't have it. This needs nobody: merging a
# new plugin is all it takes to reach magpie.
# - npm: publishes every version npm doesn't have yet (scripts/publish.sh).
# npm tries trusted publishing (OIDC) first: a package that names this
# workflow as its Trusted Publisher on npmjs.com, with npm publish allowed,
# needs nothing else. A package's first version can't go that way
# (trusted publishing can't create a package): the NPM_TOKEN secret
# publishes it when set, else it waits for an owner (README, Publishing)
# and magpie installs it from dist meanwhile.
name: publish
on:
@@ -15,14 +18,60 @@ on:
workflow_dispatch:
permissions:
id-token: write # npm's OIDC exchange
contents: read
concurrency: publish
jobs:
publish:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
- uses: actions/setup-node@v4
with:
node-version: 24
- name: test
# the tests refuse a real home: no real sign-in is ever read
run: HOME=$(mktemp -d) bun test
- name: check
run: bun scripts/check.mjs
dist:
needs: test
runs-on: ubuntu-latest
permissions:
contents: write # pushes the dist branch
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 24
- name: pack
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
if git fetch --depth=1 origin dist; then
git worktree add -B dist "$RUNNER_TEMP/dist" FETCH_HEAD
else
git worktree add --orphan -b dist "$RUNNER_TEMP/dist"
fi
node scripts/dist.mjs "$RUNNER_TEMP/dist"
cd "$RUNNER_TEMP/dist"
git add -A .
if git diff --cached --quiet; then
echo "dist is current"
else
git commit -q -m "dist: ${GITHUB_SHA::7}"
git push origin dist
fi
npm:
needs: test
runs-on: ubuntu-latest
permissions:
id-token: write # npm's OIDC exchange
contents: read
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
@@ -30,9 +79,6 @@ jobs:
with:
node-version: 24 # npm 11.5+, which speaks trusted publishing
registry-url: https://registry.npmjs.org
- name: test
# the tests refuse a real home: no real sign-in is ever read
run: HOME=$(mktemp -d) bun test
- name: publish
run: scripts/publish.sh
env:
+29 -22
View File
@@ -180,29 +180,36 @@ request.
## 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.
A version bump pushed to `main` ships itself, two ways
(`.github/workflows/publish.yml`):
`NPM_TOKEN` is set once by an owner of the magpie-community npm org:
- **dist**: every package's version is packed onto the `dist` branch
(`scripts/dist.mjs`): `tgz/<file>` and `index.json`, which names each
version's tarball and its sha512. magpie (0.1.1234 and later) installs a
`@magpie-community` package from there whenever npm doesn't have the
version: a new package, or one npm refused. It installs a tarball only
when the sha512 matches. This needs nobody, so **a new plugin is merged
as soon as its review is done**; it never waits for npm.
- **npm**: every version npm doesn't have yet is published. npm tries
trusted publishing (OIDC) first, which needs the package to name
`publish.yml` as its Trusted Publisher on npmjs.com with `npm publish`
allowed. A trust made since 2026-09-03 allows only `npm stage publish`
unless told otherwise, so make it with `--allow-publish`:
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`.
```sh
npx npm@latest trust github <package> --file publish.yml \
--repo magpie-community/plugins --allow-publish \
--registry=https://registry.npmjs.org/ -y
```
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 package's first version can't go through trusted publishing: a Trusted
Publisher is set on a package that exists, and setting one takes an
owner's 2FA. Until an owner of the magpie-community npm org
publishes it (`scripts/publish.sh <otp>`) and sets its Trusted Publisher as
above, magpie serves it from `dist`, and the publish run notes it without
failing. The repository's `NPM_TOKEN` secret, a granular token with 2FA
bypassed, still publishes a first version while npm lets such tokens
publish (npm plans to end that in January 2027).
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 <otp>` from a magpie-community npm owner
publishes it by hand.
A pull request's `on-npm` check notes any package not on npm yet. It is
not a reason to hold the merge.
+55
View File
@@ -0,0 +1,55 @@
// Packs every package's version onto the `dist` branch, where magpie
// installs it from when npm doesn't have it: a new package (npm's trusted
// publishing can't create one, and its first version waits for an owner's
// 2FA), or a version npm refused. Merging is all a plugin needs to reach
// magpie.
// node scripts/dist.mjs <folder> (the dist branch checked out there)
// The branch holds tgz/<npm pack's file name> and index.json:
// {version: 1, packages: {<name>: {latest, readme,
// versions: {<version>: {file, integrity, time, manifest}}}}}
// integrity is the tarball's sha512, as npm writes it; magpie installs a
// tarball only when it matches. A version packed once is never packed
// again, so a file magpie checked stays what it checked; a package taken
// out of the repo stays in the index for those who installed it.
import { execFileSync } from "node:child_process"
import { createHash } from "node:crypto"
import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from "node:fs"
import { join, resolve } from "node:path"
const out = resolve(process.argv[2] ?? "")
if (!process.argv[2] || !existsSync(out)) {
console.error("usage: node scripts/dist.mjs <folder with the dist branch checked out>")
process.exit(2)
}
const root = resolve(import.meta.dirname, "..")
const tgz = join(out, "tgz")
mkdirSync(tgz, { recursive: true })
const indexFile = join(out, "index.json")
const index = existsSync(indexFile) ? JSON.parse(readFileSync(indexFile, "utf8")) : { version: 1, packages: {} }
const README_MOST = 200 << 10
const added = []
for (const dir of readdirSync(join(root, "packages")).sort()) {
const pj = join(root, "packages", dir, "package.json")
if (!existsSync(pj)) continue
const manifest = JSON.parse(readFileSync(pj, "utf8"))
if (manifest.private) continue
const { name, version } = manifest
const entry = (index.packages[name] ??= { versions: {} })
const had = entry.versions[version]
if (!had || !existsSync(join(out, had.file))) {
// a package's own scripts never run here: this job can push
const [packed] = JSON.parse(execFileSync("npm", ["pack", "--json", "--ignore-scripts", "--pack-destination", tgz], { cwd: join(root, "packages", dir), encoding: "utf8", stdio: ["ignore", "pipe", "inherit"] }))
const integrity = "sha512-" + createHash("sha512").update(readFileSync(join(tgz, packed.filename))).digest("base64")
entry.versions[version] = { file: "tgz/" + packed.filename, integrity, time: new Date().toISOString(), manifest }
added.push(`${name}@${version}`)
}
entry.latest = version
const readme = join(root, "packages", dir, "README.md")
entry.readme = existsSync(readme) ? readFileSync(readme, "utf8").slice(0, README_MOST) : ""
}
const sorted = { version: 1, packages: Object.fromEntries(Object.entries(index.packages).sort(([a], [b]) => a.localeCompare(b))) }
writeFileSync(indexFile, JSON.stringify(sorted, null, 2) + "\n")
console.log(added.length ? "packed: " + added.join(" ") : "every version is packed already")
+5 -4
View File
@@ -1,15 +1,16 @@
#!/bin/sh
# 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.
# publishing can't create a package. Merging doesn't wait for it: magpie
# installs the package from the dist branch until an owner publishes its
# first version (README, Publishing). A pull request runs this so the
# reviewer knows a merge brings 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 "::notice file=${dir}package.json::$name is not on npm yet. Merging publishes its first version with the NPM_TOKEN secret (README, Publishing)."
echo "::notice file=${dir}package.json::$name is not on npm yet. Merging serves it to magpie from the dist branch; npm gets its first version from an owner (README, Publishing)."
new="$new $name"
fi
done
+8 -9
View File
@@ -7,10 +7,11 @@
# 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 <otp>` from an npm account of the magpie-community
# org publishes it.
# Without the secret Actions passes a new package by and says so: magpie
# installs it from the dist branch (scripts/dist.mjs) until an owner
# publishes its first version, by hand `scripts/publish.sh <otp>` from an
# npm account of the magpie-community org. A version npm refuses fails the
# run: its Trusted Publisher needs mending.
set -e
cd "$(dirname "$0")/.."
# npm's own registry, whatever ~/.npmrc names (a mirror such as npmmirror
@@ -30,7 +31,7 @@ for dir in packages/*/; do
continue
fi
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 <otp>)."
echo "::notice::$name is not on npm yet: trusted publishing can't create it, and the repository has no NPM_TOKEN secret. magpie installs it from the dist branch meanwhile; an owner publishes its first version (README, Publishing)."
new="$new $name"
continue
fi
@@ -38,17 +39,15 @@ for dir in packages/*/; do
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)."
echo "::error::$name@$version wasn't published: this workflow isn't its Trusted Publisher on npmjs.com, or the trust allows npm stage publish only (the default since 2026-09-03): npm trust github $name --file publish.yml --repo magpie-community/plugins --allow-publish (README, Publishing)."
fi
refused="$refused $name"
fi
done
if [ -n "$new" ]; then
echo "not published, new to npm:$new"
echo "not published, new to npm (served from dist):$new"
fi
if [ -n "$refused" ]; then
echo "not published, refused by npm:$refused"
fi
if [ -n "$new$refused" ]; then
exit 1
fi