publish.yml ran from release/* so its version-bump commit would reach main through a PR. That guarded a package.json version field and a lockfile and left the shipped tree ungated, which is the wrong way round — and it was not theoretical. On release/cli-readme: 00:33:19 v0.2.2 published to npm 00:44:45 v0.2.3 published to npm 00:48:18 PR #10 opened — first chance to review any of it 00:48:21 Checks and Branch policy run for the first time 00:51:33 merged to main Two versions reached users from a branch with no pull request, no CI run and no reviewer, seven minutes before the code reached main. `main` is not branch protected either, so the PR route was never enforced to begin with. Publishing from main means npm gets the tree that was reviewed, merged and checked. The bump commit pushed back to main is mechanical and has nothing in it to review; branch-policy.yml still governs the route for everything else. Dry runs stay unrestricted — they publish and push nothing, and validating packaging from a branch before merging it is what they are for. The tag is now pushed on its own, ahead of the branch. main takes merges that a release/* branch did not, so the bump commit can lose a race that a tag ref cannot; rebasing to win it would move the commit off the tree that was actually published. If the branch push loses, the release is still complete and recorded by the tag, and the job says exactly which cherry-pick finishes it.
224 lines
9.9 KiB
YAML
224 lines
9.9 KiB
YAML
name: Publish (CI)
|
|
|
|
run-name: "Publish · ${{ inputs.version || inputs.bump }}${{ inputs.dry_run && ' (dry-run)' || '' }}"
|
|
|
|
# All-in-CI release for @airshiplabs/cli: pick a bump (or an explicit version)
|
|
# and this bumps apps/cli/package.json, builds, publishes, then commits and tags
|
|
# cli-vX.Y.Z and pushes — the CI equivalent of `make release` plus pushing the
|
|
# tag, with no local steps.
|
|
#
|
|
# Run it from main. What goes to npm should be what was reviewed, merged and
|
|
# checked — and the only way to guarantee that is to publish the tree main
|
|
# actually has. This used to run from release/* so the version-bump commit
|
|
# would reach main through a PR; that protected a package.json version field
|
|
# and a lockfile while leaving the shipped tree ungated, and it showed: v0.2.2
|
|
# and v0.2.3 both went out from release/cli-readme before that branch had a
|
|
# pull request or a single Checks run, reaching users seven minutes before
|
|
# they reached main.
|
|
#
|
|
# The bump commit this job pushes to main is mechanical and there is nothing in
|
|
# it to review. branch-policy.yml still governs the route for everything else.
|
|
#
|
|
# Note: the cli-v* tag is pushed with GITHUB_TOKEN, which by design does NOT
|
|
# trigger release.yml — so this workflow publishes here, and the two lanes can
|
|
# never double-publish.
|
|
#
|
|
# Nothing reaches the remote until npm has the version, so a failed run is a
|
|
# re-dispatch and nothing else. If a run ever does die between the publish and
|
|
# the push, finish it from release.yml's workflow_dispatch — don't re-cut it.
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
bump:
|
|
description: "Version bump (ignored if a version is given)"
|
|
type: choice
|
|
options: [patch, minor, major]
|
|
default: patch
|
|
version:
|
|
description: "Explicit version, e.g. 1.4.0 (overrides bump)"
|
|
type: string
|
|
default: ""
|
|
dry_run:
|
|
description: "Dry run — validate packaging only; no commit, tag, push or publish"
|
|
type: boolean
|
|
default: false
|
|
|
|
permissions:
|
|
contents: write # push the release commit + tag
|
|
id-token: write # npm provenance attestation
|
|
|
|
concurrency:
|
|
group: publish
|
|
cancel-in-progress: false
|
|
|
|
jobs:
|
|
publish:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
# First, and before the checkout, so a misdirected release costs three
|
|
# seconds instead of a version. Dry runs are exempt: they publish nothing
|
|
# and push nothing, and validating packaging from a branch before you
|
|
# merge it is exactly what they are for.
|
|
- name: Releases come from main
|
|
if: ${{ inputs.dry_run == false }}
|
|
run: |
|
|
if [ "$GITHUB_REF_NAME" != "main" ]; then
|
|
echo "::error::Publish runs on main, not '$GITHUB_REF_NAME' — this would ship code main does not have."
|
|
echo " Merge the PR first, then dispatch this on main."
|
|
echo " To validate packaging from this branch instead: make release:ci DRY=1"
|
|
exit 1
|
|
fi
|
|
|
|
- uses: actions/checkout@v7
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- uses: pnpm/action-setup@v6
|
|
|
|
# Not the setup-workspace composite: this job needs registry-url, which
|
|
# is what writes the .npmrc that NODE_AUTH_TOKEN binds to.
|
|
- uses: actions/setup-node@v7
|
|
with:
|
|
node-version-file: .nvmrc
|
|
cache: pnpm
|
|
registry-url: "https://registry.npmjs.org"
|
|
|
|
- run: pnpm install --frozen-lockfile
|
|
|
|
- name: Compute the next version
|
|
id: ver
|
|
env:
|
|
BUMP: ${{ inputs.bump }}
|
|
VERSION: ${{ inputs.version }}
|
|
run: |
|
|
if [ -n "$VERSION" ]; then
|
|
NEXT=$(node scripts/next-version.mjs --version "$VERSION")
|
|
else
|
|
NEXT=$(node scripts/next-version.mjs --bump "$BUMP")
|
|
fi
|
|
echo "next=$NEXT" >> "$GITHUB_OUTPUT"
|
|
echo "tag=cli-v$NEXT" >> "$GITHUB_OUTPUT"
|
|
echo "::notice::Releasing @airshiplabs/cli v$NEXT (tag cli-v$NEXT)"
|
|
|
|
# Asks git AND npm, because those two can disagree — that is the whole
|
|
# failure this job is now built around. A bare "tag already exists" is
|
|
# what you got when v0.2.4 was tagged but never published, and it named
|
|
# the symptom while hiding both the cause and the way out.
|
|
#
|
|
# A network failure on `npm view` reads as "not published", which sends
|
|
# the job on to publish; npm rejects the duplicate there, before anything
|
|
# is pushed. So the ambiguous case fails on the npm side, never on git's.
|
|
- name: Guard against an existing release
|
|
env:
|
|
TAG: ${{ steps.ver.outputs.tag }}
|
|
NEXT: ${{ steps.ver.outputs.next }}
|
|
run: |
|
|
tagged=0
|
|
if git rev-parse -q --verify "refs/tags/$TAG" >/dev/null; then
|
|
tagged=1
|
|
fi
|
|
published=0
|
|
if npm view "@airshiplabs/cli@$NEXT" version >/dev/null 2>&1; then
|
|
published=1
|
|
fi
|
|
|
|
if [ "$tagged" = 1 ] && [ "$published" = 1 ]; then
|
|
echo "::error::v$NEXT is already released — tag $TAG exists and npm has it. Pick a different version."
|
|
exit 1
|
|
fi
|
|
if [ "$tagged" = 1 ] && [ "$published" = 0 ]; then
|
|
echo "::error::v$NEXT is half-released: tag $TAG exists but npm never got the package."
|
|
echo " Finish it instead of re-cutting it — run the Release workflow with tag=$TAG."
|
|
echo " gh workflow run release.yml -f tag=$TAG"
|
|
exit 1
|
|
fi
|
|
if [ "$tagged" = 0 ] && [ "$published" = 1 ]; then
|
|
echo "::error::npm already has v$NEXT but tag $TAG does not exist — npm ran ahead of git."
|
|
echo " Push the tag for the commit that shipped it; do not re-publish."
|
|
exit 1
|
|
fi
|
|
|
|
- name: Bump apps/cli/package.json
|
|
run: |
|
|
node -e "const f='apps/cli/package.json';const p=require('./'+f);p.version='${{ steps.ver.outputs.next }}';require('fs').writeFileSync(f, JSON.stringify(p,null,2)+'\n');"
|
|
pnpm install --lockfile-only
|
|
|
|
# This step used to be `publish --dry-run`, which validated nothing:
|
|
# `pnpm publish` does not build the package (apps/cli has no prepack or
|
|
# prepare hook), so the dry run packed an empty tarball and exited 0.
|
|
# That is how v0.2.0 shipped with no dist/. Build explicitly, then pack
|
|
# for real and look inside.
|
|
- name: Build the CLI
|
|
run: pnpm turbo run build --filter=@airshiplabs/cli
|
|
|
|
# Pack with pnpm, publish that exact tarball with npm. Neither tool does
|
|
# both halves: only `pnpm pack` rewrites the `workspace:*` devDependencies
|
|
# into real versions, and only `npm publish` sends the `readme` field —
|
|
# which is what npmjs.com renders the package page from. `pnpm publish`
|
|
# drops it, so v0.2.2 shipped a correct tarball behind a page that still
|
|
# read "This package does not have a README".
|
|
- name: Pack the tarball
|
|
id: pack
|
|
working-directory: apps/cli
|
|
run: |
|
|
TGZ="$RUNNER_TEMP/airshiplabs-cli.tgz"
|
|
pnpm pack --out "$TGZ"
|
|
echo "tgz=$TGZ" >> "$GITHUB_OUTPUT"
|
|
|
|
# Looks inside the tarball it is about to publish, not a second one packed
|
|
# for the check.
|
|
- name: Validate packaging
|
|
run: bash scripts/verify-tarball.sh "${{ steps.pack.outputs.tgz }}"
|
|
|
|
# Publishing comes BEFORE the push, and the order is the point. This used
|
|
# to push the release commit and the tag first, so when npm failed the
|
|
# run left git claiming a release npm had never seen — and the tag guard
|
|
# above then blocked every re-run of that same version. v0.2.4 stranded
|
|
# exactly that way. Nothing here writes to the remote until the version is
|
|
# actually on npm, so every failure up to this line is just a re-dispatch.
|
|
#
|
|
# The tarball is the one "Validate packaging" just looked inside — packed
|
|
# after the bump, so it already carries the released version.
|
|
- name: Publish to npm
|
|
if: ${{ inputs.dry_run == false }}
|
|
run: bash scripts/npm-publish.sh "${{ steps.pack.outputs.tgz }}"
|
|
env:
|
|
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
|
|
- name: Commit, tag and push
|
|
if: ${{ inputs.dry_run == false }}
|
|
env:
|
|
TAG: ${{ steps.ver.outputs.tag }}
|
|
NEXT: ${{ steps.ver.outputs.next }}
|
|
run: |
|
|
git config user.name "github-actions[bot]"
|
|
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
|
git add -A
|
|
# --no-verify skips the husky commit-msg and pre-push hooks. The
|
|
# pre-push hook blocks pushes to main, and commit-msg would reject
|
|
# nothing here, but both are pointless in CI.
|
|
git commit --no-verify -m "chore(release): cli v$NEXT"
|
|
git tag -a "$TAG" -m "$TAG"
|
|
|
|
# The tag goes first and on its own, rather than riding along on
|
|
# --follow-tags. It is the release record — it names the exact tree
|
|
# npm now has — and a tag ref cannot lose a race to a merge the way a
|
|
# branch ref can. Rebasing to win that race is not an option: it would
|
|
# move the commit off the tree that was actually published.
|
|
git push --no-verify origin "refs/tags/$TAG"
|
|
|
|
if ! git push --no-verify origin "HEAD:$GITHUB_REF_NAME"; then
|
|
echo "::error::v$NEXT is published and tagged, but main moved during the run and the bump commit did not land."
|
|
echo " The release itself is complete — only the bookkeeping commit is missing."
|
|
echo " Put it on main with: git cherry-pick $TAG"
|
|
exit 1
|
|
fi
|
|
|
|
# Last, and after the push on purpose. This checks a release that npm has
|
|
# already made permanent — it cannot prevent a bad one, only make it loud
|
|
# (see the script's own header). Gating the push on it would strand a
|
|
# perfectly good release in the opposite direction.
|
|
- name: The npm page has a README
|
|
if: ${{ inputs.dry_run == false }}
|
|
run: bash scripts/verify-published-readme.sh
|