Files
CatoandClaude dab800e1b4 fix(release): a package new in a push no longer stops the GitLab publish (#16)
ci-publish.sh read each package.json's old version with `git show
BEFORE:file | node`. For a package that did not exist before the push,
git exits 128, and under pipefail the assignment ended the whole run before
it printed anything: the main push that added datakit (#15) failed in 0.6 s.
Now the missing file reads as no old version, so a new package is a target.

Checked: CI_PUBLISH_DRY=1 with BEFORE=592949d on 14574b7 exited 128 before,
and says 'would publish datakit 0.1.0' after (already on npm, so the real
run skips it).


Claude-Session: https://claude.ai/code/session_019MDtmvTCDbJAYNKQpNyKzz

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-28 18:51:34 +01:00

69 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# GitLab CI: publish to npm what this pipeline is for (.gitlab-ci.yml).
#
# a tag `<folder>-v<version>` → that one package (scripts/release.sh's way)
# a push to main → every package whose package.json version
# changed in the push (Gitea mirrors GitHub's
# main here after each merge)
#
# A version already on npm is skipped, so a tag and a main push for the same
# release cannot publish twice. An unchanged version is never published from
# main: an old release that failed (bip-kit 0.11.0) is not re-fired by an
# unrelated merge; it gets its tag, or a new version.
set -euo pipefail
published() { [ -n "$(npm view "$1@$2" version 2>/dev/null)" ]; }
targets=()
if [ -n "${CI_COMMIT_TAG:-}" ]; then
targets+=("${CI_COMMIT_TAG%-v*} ${CI_COMMIT_TAG##*-v}")
else
BEFORE="${CI_COMMIT_BEFORE_SHA:-}"
if [ -z "$BEFORE" ] || [ "$BEFORE" = 0000000000000000000000000000000000000000 ]; then
echo "no previous commit for this push (new branch?) — publishing nothing"; exit 0
fi
git cat-file -e "$BEFORE^{commit}" 2>/dev/null || git fetch -q --depth 50 origin "$BEFORE" || {
echo "cannot read $BEFORE — publishing nothing; tag the release instead"; exit 0; }
for f in $(git ls-tree -r --name-only HEAD -- packages | grep -E '^packages/[^/]+/package\.json$'); do
folder="${f#packages/}"; folder="${folder%/package.json}"
new=$(node -p "require('./$f').version")
# A package new in this push has no package.json at BEFORE: git exits 128,
# and under pipefail that killed the whole run (datakit, 2026-11-28).
old=$({ git show "$BEFORE:$f" 2>/dev/null || true; } | node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{try{console.log(JSON.parse(s).version||"")}catch{console.log("")}})')
priv=$(node -p "require('./$f').private === true")
[ "$new" != "$old" ] && [ "$priv" = false ] && targets+=("$folder $new")
done
fi
[ ${#targets[@]} -gt 0 ] || { echo "no package version changed — nothing to publish"; exit 0; }
# CI_PUBLISH_DRY=1: say what would be published, publish nothing (a local check).
[ -z "${CI_PUBLISH_DRY:-}" ] || { printf "would publish %s\n" "${targets[@]}"; exit 0; }
# Trusted publishing needs npm >= 11.5.1; pin the major, take its latest.
npm install -g npm@11 >/dev/null && npm --version
corepack enable
rc=0
for t in "${targets[@]}"; do
PKG="${t% *}"; VER="${t#* }"; DIR="packages/$PKG"
[[ "$PKG" =~ ^[a-z0-9][a-z0-9-]*$ ]] && [ -f "$DIR/package.json" ] || { echo "no package at $DIR"; rc=1; continue; }
NAME=$(node -p "require('./$DIR/package.json').name")
[ "$(node -p "require('./$DIR/package.json').version")" = "$VER" ] || { echo "$DIR/package.json is not $VER"; rc=1; continue; }
if published "$NAME" "$VER"; then echo "$NAME@$VER is already on npm"; continue; fi
echo "=== publishing $NAME@$VER"
(
set -e
pnpm install --frozen-lockfile --filter "$NAME..."
pnpm --filter "$NAME" run build
# pnpm pack writes the real versions in for `workspace:^`; npm publishes the
# tarball, because npm is what speaks trusted publishing.
rm -rf /tmp/pkg && mkdir -p /tmp/pkg && (cd "$DIR" && pnpm pack --pack-destination /tmp/pkg)
# Provenance off: npm rejects it while package.json `repository` names
# GitHub, not this GitLab project. The env var does not turn it off; the flag does.
npm publish /tmp/pkg/*.tgz --access public --provenance=false
) || { echo "$NAME@$VER: publish failed"; rc=1; continue; }
# npm answers "being processed" first; the version shows up within minutes.
for i in $(seq 1 30); do published "$NAME" "$VER" && { echo "$NAME@$VER is on npm"; continue 2; }; sleep 10; done
echo "$NAME@$VER not visible on npm after 5 min"; rc=1
done
exit $rc