Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b04421ddd | ||
|
|
0fad12688d | ||
|
|
ead71dc1b1 | ||
|
|
19fcc18899 |
No files matched your search
@@ -16,7 +16,7 @@ jobs:
|
||||
PKG: '@green-tea/core'
|
||||
permissions:
|
||||
id-token: write # OIDC for Trusted Publishing (also enables provenance)
|
||||
contents: read
|
||||
contents: write # create the GitHub release for this tag
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
@@ -42,19 +42,41 @@ jobs:
|
||||
# types, module analysis — and learning about them after npm has published leaves the tag
|
||||
# half-shipped. Seconds, against a failure mode with no clean recovery.
|
||||
- run: deno publish --dry-run
|
||||
# A publish sets exactly one dist-tag, and it is the only npm write this workflow can make:
|
||||
# Trusted Publishing's OIDC exchange lives inside `npm publish` and nowhere else, so the token
|
||||
# it produces is rejected by `npm dist-tag` (E401, learned on v26.8.0-beta.1). So the one tag
|
||||
# the publish gets has to be the one that matters most.
|
||||
#
|
||||
# While no stable release exists, `latest` and the channel tag mean the same thing — there is
|
||||
# nothing else to install — and `latest` takes it, because that is what a bare
|
||||
# `npm install @green-tea/core` resolves to. Leaving it behind on an older beta would hand the
|
||||
# default install an older build than the one just released. The channel tag is then moved by
|
||||
# hand; the step prints the command. Once a stable version exists it owns `latest` for good,
|
||||
# a later beta must not drag it backwards, and the channel tag becomes the automatic one again.
|
||||
- name: Publish to npmjs
|
||||
run: |
|
||||
VERSION=$(node -p "require('./package.json').version")
|
||||
TAG=$(node -e "const v=require('./package.json').version;const m=v.match(/-([a-z]+)/);console.log(m?m[1]:'latest')")
|
||||
CHANNEL=$(node -e "const m=process.argv[1].match(/-([a-z]+)/);console.log(m?m[1]:'latest')" "$VERSION")
|
||||
# Read the list into a variable so a failed lookup fails the step, rather than passing for
|
||||
# "no stable exists" and pointing `latest` at a prerelease that should not have it.
|
||||
VERSIONS=$(npm view "$PKG" versions --json)
|
||||
if [ "$CHANNEL" != latest ] \
|
||||
&& ! node -e "process.exit([].concat(JSON.parse(process.argv[1])).some(v=>!v.includes('-'))?0:1)" "$VERSIONS"; then
|
||||
TAG=latest
|
||||
else
|
||||
TAG=$CHANNEL
|
||||
fi
|
||||
echo "$VERSION under dist-tag: $TAG"
|
||||
# Skip rather than fail when the version is already up. A step after this one can fail —
|
||||
# the dist-tag move below does — and a re-run of the job has to be able to finish the
|
||||
# remaining work instead of dying on a duplicate npm rejects anyway.
|
||||
# Skip rather than fail when the version is already up, so a re-run after a later failure
|
||||
# can finish the remaining work instead of dying on a duplicate npm rejects anyway.
|
||||
if npm view "$PKG@$VERSION" version >/dev/null 2>&1; then
|
||||
echo "npm already has $VERSION"
|
||||
else
|
||||
npm publish --access public --tag "$TAG"
|
||||
fi
|
||||
if [ "$TAG" != "$CHANNEL" ]; then
|
||||
echo "::notice::Move the channel tag by hand: npm dist-tag add $PKG@$VERSION $CHANNEL"
|
||||
fi
|
||||
# JSR last, deliberately. npm allows an unpublish within 72 hours; JSR versions are immutable
|
||||
# and can never be replaced or removed. Ordered so a failure here costs a re-run, while a
|
||||
# failure *after* it would cost a version number permanently. Version and entry points come
|
||||
@@ -65,43 +87,33 @@ jobs:
|
||||
run: |
|
||||
VERSION=$(node -p "require('./package.json').version")
|
||||
PKG=${PKG#@}
|
||||
# 404 until the version exists — and it stays 404 for a package with no versions yet, which
|
||||
# is the state this repository is actually in, so the guard must not read that as an error.
|
||||
# 404 until the version exists, and also for a package with no versions at all — which is
|
||||
# what this one was until v26.8.0-beta.1 — so the guard must not read a 404 as an error.
|
||||
if curl -fsS -o /dev/null "https://api.jsr.io/scopes/${PKG%%/*}/packages/${PKG#*/}/versions/$VERSION"; then
|
||||
echo "JSR already has $VERSION"
|
||||
else
|
||||
deno publish
|
||||
fi
|
||||
# `latest` is what a bare `npm install @green-tea/core` resolves to, and while there is no
|
||||
# stable release the docs promise it is the newest beta. `npm publish --tag beta` sets exactly
|
||||
# one tag, so `latest` has to be moved separately — and `npm dist-tag` cannot use Trusted
|
||||
# Publishing, whose OIDC exchange lives only inside `npm publish`. So we run that same exchange
|
||||
# by hand (id-token → short-lived npm token) rather than keep a long-lived token in a secret for
|
||||
# one command. Last on purpose: both registries already have the version, and a dist-tag is the
|
||||
# only thing here that can be redone. If this step fails, the release is out but `latest` is
|
||||
# stale — recover with `npm dist-tag add @green-tea/core@<version> latest`.
|
||||
- name: Point `latest` at this release while no stable exists
|
||||
|
||||
# The release page is where people who do not read a changelog find out what changed, and a
|
||||
# repository with tags and no releases reads as abandoned. Notes come from CHANGELOG.md rather
|
||||
# than from the commit range: the section was written for humans and already exists by the time
|
||||
# a tag is cut, and generated notes would restate merge subjects nobody chose.
|
||||
#
|
||||
# Idempotent like the publishes above, so a re-run after a later failure does not stop here.
|
||||
# `--verify-tag` refuses to invent a tag: the release attaches to the one that triggered this
|
||||
# run and can never move it. A prerelease is marked as such, which is what keeps GitHub's
|
||||
# "Latest" badge off a beta.
|
||||
- name: Create the GitHub release
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
VERSION=$(node -p "require('./package.json').version")
|
||||
case "$VERSION" in
|
||||
*-*) ;;
|
||||
*) echo "$VERSION is stable — npm publish already put it on latest"; exit 0 ;;
|
||||
esac
|
||||
# A stable release owns `latest` for good; a beta published afterwards must never drag it
|
||||
# backwards, so the newest-beta rule applies only until the first stable exists.
|
||||
# Read the list first so a failed lookup fails the step, rather than reading as "no stable
|
||||
# exists" and moving `latest` backwards on the strength of a network error.
|
||||
VERSIONS=$(npm view "$PKG" versions --json)
|
||||
if node -e "process.exit([].concat(JSON.parse(process.argv[1])).some(v=>!v.includes('-'))?0:1)" "$VERSIONS"; then
|
||||
echo "a stable version exists — leaving latest where it is"; exit 0
|
||||
TAG=${GITHUB_REF_NAME}
|
||||
if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then
|
||||
echo "release $TAG already exists"; exit 0
|
||||
fi
|
||||
URL=$(node -p "const u=new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL);u.searchParams.set('audience','npm:registry.npmjs.org');u.href")
|
||||
ID_TOKEN=$(curl -fsS -H "Authorization: Bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "$URL" \
|
||||
| node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).value")
|
||||
echo "::add-mask::$ID_TOKEN"
|
||||
NODE_AUTH_TOKEN=$(curl -fsS -X POST -H "Authorization: Bearer $ID_TOKEN" \
|
||||
"https://registry.npmjs.org/-/npm/v1/oidc/token/exchange/package/$(node -p "encodeURIComponent(process.env.PKG)")" \
|
||||
| node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).token")
|
||||
echo "::add-mask::$NODE_AUTH_TOKEN"
|
||||
export NODE_AUTH_TOKEN
|
||||
npm dist-tag add "$PKG@$VERSION" latest
|
||||
node scripts/changelog-section.mjs "$VERSION" "$TAG" > /tmp/notes.md
|
||||
case "$VERSION" in *-*) PRE=--prerelease ;; *) PRE= ;; esac
|
||||
gh release create "$TAG" --repo "$GITHUB_REPOSITORY" --title "$TAG" \
|
||||
--notes-file /tmp/notes.md --verify-tag $PRE
|
||||
@@ -85,6 +85,20 @@ Bumping `package.json` and `deno.json` ahead of the tag is fine and necessary
|
||||
*next* publish will be called, and the docs repo's `verify:release` compares the tag against them.
|
||||
A version number is a plan; a changelog heading is a receipt.
|
||||
|
||||
**After a beta release, move the channel tag by hand.** A publish sets exactly one dist-tag, and it
|
||||
is the only npm write the release workflow can make — Trusted Publishing's OIDC exchange lives inside
|
||||
`npm publish`, so the token it hands back is rejected by `npm dist-tag` (`E401`, learned the hard way
|
||||
on `v26.8.0-beta.1`). While there is no stable release the publish spends that one tag on `latest`,
|
||||
since that is what a bare `npm install @green-tea/core` resolves to, and the workflow prints the
|
||||
command for the rest:
|
||||
|
||||
```bash
|
||||
npm dist-tag add @green-tea/core@<version> beta
|
||||
```
|
||||
|
||||
Once a stable version exists this stops being a chore: `latest` belongs to the stable release, the
|
||||
publish gives a beta its channel tag directly, and there is nothing left to move.
|
||||
|
||||
## CI
|
||||
|
||||
The audit gate is scoped, not severity-filtered: `npm audit --omit=dev` blocks, everything else is `continue-on-error`. A critical in the test runner reaches nobody; a high in `reflect-metadata` reaches everyone. It runs *after* the code gates, because an advisory published by a third party must never decide whether a contributor sees their own lint and typecheck output.
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env node
|
||||
// Prints one version's section of CHANGELOG.md, for a GitHub release body.
|
||||
//
|
||||
// Reads the current file rather than the tag's copy on purpose: the changelog is the record, so a
|
||||
// correction to an old entry should reach the release that describes it.
|
||||
//
|
||||
// Usage: node scripts/changelog-section.mjs <version> [tag]
|
||||
// The optional tag makes relative links absolute — a release body resolves them against the release
|
||||
// page, not the repository, and pinning to the tag keeps the link on the file as that version had it.
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const REPO = 'Expressive-Tea/green-tea';
|
||||
const [version, tag] = process.argv.slice(2);
|
||||
if (!version) {
|
||||
console.error('usage: changelog-section.mjs <version> [tag]');
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const lines = readFileSync(new URL('../CHANGELOG.md', import.meta.url), 'utf8').split('\n');
|
||||
const isHeading = (l) => l.startsWith('## [');
|
||||
// The `[label]: url` block at the bottom of the file belongs to no section. Without this the last
|
||||
// section swallows it, since nothing else comes after.
|
||||
const isLinkRef = (l) => /^\[[^\]]+\]:\s+\S/.test(l);
|
||||
|
||||
const start = lines.findIndex((l) => l.startsWith(`## [${version}]`));
|
||||
if (start === -1) {
|
||||
console.error(`no section for ${version}. Headings in CHANGELOG.md:\n${lines.filter(isHeading).join('\n')}`);
|
||||
process.exit(1);
|
||||
}
|
||||
const rest = lines.slice(start + 1);
|
||||
const stop = rest.findIndex((l) => isHeading(l) || isLinkRef(l));
|
||||
const body = (stop === -1 ? rest : rest.slice(0, stop)).join('\n').trim();
|
||||
|
||||
const absolute = tag
|
||||
? body.replace(/\]\(\.\/([^)\s]+)\)/g, `](https://github.com/${REPO}/blob/${tag}/$1)`)
|
||||
: body;
|
||||
|
||||
console.log(absolute);
|
||||
@@ -0,0 +1,51 @@
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { resolve } from 'node:path';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
// `scripts/changelog-section.mjs` writes the body of every GitHub release. It is the only place where
|
||||
// a mistake is invisible until it is already published, so its boundaries are pinned here rather than
|
||||
// checked by eye at release time.
|
||||
const SCRIPT = resolve(process.cwd(), 'scripts/changelog-section.mjs');
|
||||
const section = (version: string, tag?: string): string =>
|
||||
execFileSync('node', [SCRIPT, version, ...(tag ? [tag] : [])], { encoding: 'utf8' });
|
||||
|
||||
const changelog = readFileSync(resolve(process.cwd(), 'CHANGELOG.md'), 'utf8');
|
||||
const released = [...changelog.matchAll(/^## \[(\d[^\]]*)\]/gm)].map(([, v]) => v);
|
||||
|
||||
describe('changelog section extraction', () => {
|
||||
it('finds every released version', () => {
|
||||
expect(released.length).toBeGreaterThan(0);
|
||||
for (const version of released) {
|
||||
expect(section(version).trim(), version).not.toBe('');
|
||||
}
|
||||
});
|
||||
|
||||
it('stops at the next version, so a section never carries the one below it', () => {
|
||||
for (const version of released) {
|
||||
expect(section(version), version).not.toMatch(/^## \[/m);
|
||||
}
|
||||
});
|
||||
|
||||
// The oldest section is the one at risk: nothing follows it but the `[label]: url` block that
|
||||
// defines the compare links, which belongs to no version and once ended up inside it.
|
||||
it('stops at the link-reference block', () => {
|
||||
for (const version of released) {
|
||||
expect(section(version), version).not.toMatch(/^\[[^\]]+\]:\s+\S/m);
|
||||
}
|
||||
});
|
||||
|
||||
// A release body resolves relative links against the release page, not the repository, so every
|
||||
// link has to be absolute by the time it is published.
|
||||
it('rewrites relative links against the tag', () => {
|
||||
for (const version of released) {
|
||||
const links = [...section(version, `v${version}`).matchAll(/\]\(([^)\s]+)\)/g)].map(([, url]) => url);
|
||||
expect(links.filter((url) => !url.startsWith('http')), version).toEqual([]);
|
||||
}
|
||||
});
|
||||
|
||||
it('fails rather than publishing an empty body for a version it cannot find', () => {
|
||||
expect(() => section('0.0.0-nonexistent')).toThrow();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user