While there is no stable release, a bare `npm install @green-tea/core` should get the same thing `@beta` gets — that is what the docs promise. Since `1a313c2` a prerelease publishes under its channel tag only, so the next release would have left `latest` behind on 26.8.0-beta.0: an older build for anyone who installs without asking for a channel. `npm publish` sets one tag, and `npm dist-tag` cannot use Trusted Publishing — the OIDC exchange lives inside `publish.js` and nowhere else. Rather than keep a long-lived npm token in a secret for one command, the step runs the same exchange itself: request the Actions id-token, trade it for a short-lived package-scoped token, move the tag. Both values are masked. The rule is bounded on purpose. Once a stable version exists it owns `latest` permanently, and a beta published afterwards must not drag it backwards; the lookup that decides this is read into a variable first, so a failed request fails the step instead of passing for "no stable exists". Both publishes also become skip-if-present. That is not belt-and-braces: the new step runs after them and can fail, so a re-run has to be able to finish the remaining work instead of dying on a version npm would reject anyway. The JSR guard asks api.jsr.io rather than jsr.io/meta.json, which is 404 for a package with no versions yet — the state this package is actually in. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
108 lines
6.3 KiB
YAML
108 lines
6.3 KiB
YAML
name: Release → npmjs
|
|
|
|
# Production publish. Runs on GITHUB ONLY, on a version tag (`v*`). Publishes the stable
|
|
# package to npmjs via OIDC **Trusted Publishing** — no long-lived token. Requires a trusted
|
|
# publisher configured on npmjs for this package (org Expressive-Tea, repo green-tea,
|
|
# workflow release.yml). npm also attaches build provenance automatically.
|
|
on:
|
|
push:
|
|
tags: ['v*']
|
|
|
|
jobs:
|
|
publish-prod:
|
|
if: ${{ github.server_url == 'https://github.com' }}
|
|
runs-on: ubuntu-latest
|
|
env:
|
|
PKG: '@green-tea/core'
|
|
permissions:
|
|
id-token: write # OIDC for Trusted Publishing (also enables provenance)
|
|
contents: read
|
|
steps:
|
|
- uses: actions/checkout@v4
|
|
- uses: actions/setup-node@v4
|
|
with:
|
|
node-version: '22'
|
|
registry-url: 'https://registry.npmjs.org'
|
|
- run: npm install -g npm@latest # npm >= 11.5.1 is required for OIDC trusted publishing
|
|
- run: npm ci
|
|
# Same scoped gate as ci.yml: only what we ship blocks a publish. With `reflect-metadata`
|
|
# as the sole runtime dependency, a blocking advisory here is rare and genuinely reaches
|
|
# users; a build-tooling one never does. The full audit still reports.
|
|
- run: npm audit --omit=dev
|
|
- run: npm audit
|
|
continue-on-error: true
|
|
# `npm publish` runs prepublishOnly (lint + complexity + typecheck + test + build) first.
|
|
# No NODE_AUTH_TOKEN — npm exchanges the OIDC id-token for a short-lived publish token.
|
|
# A prerelease (26.7.0-beta.N) publishes under its channel tag (beta/alpha/rc) so `latest`
|
|
# never points at a beta; a stable version publishes under `latest`.
|
|
- uses: denoland/setup-deno@v2
|
|
with:
|
|
deno-version: v2.x
|
|
# Gate both registries before either one ships. JSR checks things npm never looks at — slow
|
|
# 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
|
|
- 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')")
|
|
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.
|
|
if npm view "$PKG@$VERSION" version >/dev/null 2>&1; then
|
|
echo "npm already has $VERSION"
|
|
else
|
|
npm publish --access public --tag "$TAG"
|
|
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
|
|
# from deno.json, which `test/package-metadata.test.ts` pins to package.json — JSR has no
|
|
# dist-tags, so a prerelease simply publishes under its own version. Auth is the same OIDC
|
|
# id-token npm just used; the package is linked to this repository on jsr.io.
|
|
- name: Publish to JSR
|
|
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.
|
|
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
|
|
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
|
|
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
|