|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Publish the packages of a merged release-please PR by hand. |
| 4 | +# |
| 5 | +# npm trusted publishing (OIDC) rejects this repository's tokens: GitHub |
| 6 | +# issues them with the immutable subject format (repos created after |
| 7 | +# 2026-07-15), which npm's registry does not accept yet (npm/cli#9969). So |
| 8 | +# the npm step of release.yml fails on every release. This script does what |
| 9 | +# that step would have done, from the same release tags, and then reruns the |
| 10 | +# failed Release Please run so the container images get built: the npm step |
| 11 | +# sees each version already on npm and skips, and the GHCR job runs. |
| 12 | +# |
| 13 | +# Usage: |
| 14 | +# scripts/publish-release.sh [--pr <number>] [--dry-run] [--skip-tests] [--no-rerun] |
| 15 | +# |
| 16 | +# --pr <number> The merged release PR ("chore: release main"). Defaults to |
| 17 | +# the most recently merged one. |
| 18 | +# --dry-run Build and pack, then `npm publish --dry-run`. Reruns nothing. |
| 19 | +# --skip-tests Do not run the test suites before packing. |
| 20 | +# --no-rerun Publish only; leave the failed workflow run alone. |
| 21 | +# |
| 22 | +# Needs git, pnpm, npm, gh, node and curl, and an `npm login` against |
| 23 | +# registry.npmjs.org. `npm publish` asks for a one-time password, so run this |
| 24 | +# in a terminal. Versions already on npm are skipped, so rerunning after a |
| 25 | +# partial failure is safe. Delete this script once npm/cli#9969 is fixed. |
| 26 | + |
| 27 | +set -euo pipefail |
| 28 | + |
| 29 | +REGISTRY="https://registry.npmjs.org/" |
| 30 | +REPO="workos/sync-engine" |
| 31 | +REPO_ROOT="$(git rev-parse --show-toplevel)" |
| 32 | + |
| 33 | +PR="" |
| 34 | +DRY_RUN=false |
| 35 | +SKIP_TESTS=false |
| 36 | +RERUN=true |
| 37 | + |
| 38 | +usage() { |
| 39 | + sed -n '/^# Usage:/,/^#$/p' "$0" | sed -E 's/^# ?//' |
| 40 | +} |
| 41 | +log() { printf '\n==> %s\n' "$*"; } |
| 42 | +die() { printf 'error: %s\n' "$*" >&2; exit 1; } |
| 43 | + |
| 44 | +while [ $# -gt 0 ]; do |
| 45 | + case "$1" in |
| 46 | + --pr) [ $# -ge 2 ] || die "--pr needs a number"; PR="$2"; shift 2 ;; |
| 47 | + --pr=*) PR="${1#--pr=}"; shift ;; |
| 48 | + --dry-run) DRY_RUN=true; shift ;; |
| 49 | + --skip-tests) SKIP_TESTS=true; shift ;; |
| 50 | + --no-rerun) RERUN=false; shift ;; |
| 51 | + -h | --help) usage; exit 0 ;; |
| 52 | + [0-9]*) PR="$1"; shift ;; |
| 53 | + *) usage >&2; die "unknown argument: $1" ;; |
| 54 | + esac |
| 55 | +done |
| 56 | + |
| 57 | +for tool in git pnpm npm gh node curl; do |
| 58 | + command -v "$tool" >/dev/null 2>&1 || die "$tool is required" |
| 59 | +done |
| 60 | + |
| 61 | +# --- Which release ------------------------------------------------------------ |
| 62 | + |
| 63 | +if [ -z "$PR" ]; then |
| 64 | + PR="$(gh pr list --repo "$REPO" --state merged --search 'chore: release main in:title' \ |
| 65 | + --limit 1 --json number --jq '.[0].number // empty')" |
| 66 | + [ -n "$PR" ] || die "no merged release PR found; pass --pr <number>" |
| 67 | +fi |
| 68 | + |
| 69 | +IFS=$'\t' read -r STATE SHA TITLE < <( |
| 70 | + gh pr view "$PR" --repo "$REPO" --json state,mergeCommit,title \ |
| 71 | + --jq '[.state, (.mergeCommit.oid // ""), .title] | join("\t")' |
| 72 | +) |
| 73 | +[ "$STATE" = "MERGED" ] || die "PR #$PR is $STATE, not merged" |
| 74 | +[ -n "$SHA" ] || die "PR #$PR has no merge commit" |
| 75 | +log "PR #$PR: $TITLE (merged as ${SHA:0:7})" |
| 76 | + |
| 77 | +log "Fetching main and tags" |
| 78 | +git -C "$REPO_ROOT" fetch --quiet origin \ |
| 79 | + "+refs/heads/main:refs/remotes/origin/main" "+refs/tags/*:refs/tags/*" |
| 80 | +git -C "$REPO_ROOT" merge-base --is-ancestor "$SHA" refs/remotes/origin/main || |
| 81 | + die "${SHA:0:7} is not on origin/main" |
| 82 | + |
| 83 | +# release-please tags each released component on the merge commit, and |
| 84 | +# release.yml checks out that tag. No tags means its run has not finished. |
| 85 | +TAGS="$(git -C "$REPO_ROOT" tag --points-at "$SHA" | grep -E '^[a-z0-9-]+-v[0-9]' || true)" |
| 86 | +[ -n "$TAGS" ] || |
| 87 | + die "no release tags point at ${SHA:0:7} yet; wait for the Release Please run: gh run list --repo $REPO --commit $SHA" |
| 88 | +# The engine first: every destination pins it. |
| 89 | +ORDERED=() |
| 90 | +for TAG in $TAGS; do case "$TAG" in sync-engine-v*) ORDERED+=("$TAG") ;; esac; done |
| 91 | +for TAG in $TAGS; do case "$TAG" in sync-engine-v*) ;; *) ORDERED+=("$TAG") ;; esac; done |
| 92 | + |
| 93 | +# --- Source checkout ---------------------------------------------------------- |
| 94 | + |
| 95 | +WORK="$(mktemp -d "${TMPDIR:-/tmp}/sync-engine-release.XXXXXX")" |
| 96 | +cleanup() { |
| 97 | + git -C "$REPO_ROOT" worktree remove --force "$WORK/src" >/dev/null 2>&1 || true |
| 98 | + rm -rf "$WORK" |
| 99 | +} |
| 100 | +trap cleanup EXIT |
| 101 | +git -C "$REPO_ROOT" worktree add --quiet --detach "$WORK/src" "$SHA" |
| 102 | +mkdir -p "$WORK/pack" |
| 103 | + |
| 104 | +# --- Plan --------------------------------------------------------------------- |
| 105 | + |
| 106 | +COMPONENTS=() VERSIONS=() NAMES=() NPM_TAGS=() STATUSES=() |
| 107 | +PENDING=0 |
| 108 | +for TAG in "${ORDERED[@]}"; do |
| 109 | + COMPONENT="${TAG%-v*}" |
| 110 | + VERSION="${TAG##*-v}" |
| 111 | + PKG="$WORK/src/packages/$COMPONENT/package.json" |
| 112 | + [ -f "$PKG" ] || die "tag $TAG names no package: packages/$COMPONENT/package.json does not exist" |
| 113 | + NAME="$(node -p "require('$PKG').name")" |
| 114 | + PKG_VERSION="$(node -p "require('$PKG').version")" |
| 115 | + [ "$PKG_VERSION" = "$VERSION" ] || die "$NAME is $PKG_VERSION in package.json but tag $TAG says $VERSION" |
| 116 | + case "$VERSION" in *-*) NPM_TAG=beta ;; *) NPM_TAG=latest ;; esac |
| 117 | + if npm view "$NAME@$VERSION" version --registry "$REGISTRY" >/dev/null 2>&1; then |
| 118 | + STATUS="on npm already" |
| 119 | + else |
| 120 | + STATUS="to publish" |
| 121 | + PENDING=$((PENDING + 1)) |
| 122 | + fi |
| 123 | + COMPONENTS+=("$COMPONENT") VERSIONS+=("$VERSION") NAMES+=("$NAME") NPM_TAGS+=("$NPM_TAG") STATUSES+=("$STATUS") |
| 124 | +done |
| 125 | + |
| 126 | +log "Release plan" |
| 127 | +for i in "${!NAMES[@]}"; do |
| 128 | + printf ' %-28s %-12s %-7s %s\n' "${NAMES[$i]}" "${VERSIONS[$i]}" "${NPM_TAGS[$i]}" "${STATUSES[$i]}" |
| 129 | +done |
| 130 | + |
| 131 | +if [ "$PENDING" -eq 0 ] && ! $DRY_RUN; then |
| 132 | + log "Every version is on npm already; nothing to publish" |
| 133 | +else |
| 134 | + if ! $DRY_RUN; then |
| 135 | + npm whoami --registry "$REGISTRY" >/dev/null 2>&1 || |
| 136 | + die "not logged in to $REGISTRY; run: npm login --registry $REGISTRY" |
| 137 | + [ -t 0 ] || echo "warning: stdin is not a terminal; npm publish may fail to ask for a one-time password" >&2 |
| 138 | + fi |
| 139 | + |
| 140 | + # --- Build ------------------------------------------------------------------ |
| 141 | + |
| 142 | + log "Installing and building at ${SHA:0:7}" |
| 143 | + (cd "$WORK/src" && pnpm install --frozen-lockfile && pnpm build) |
| 144 | + if $SKIP_TESTS; then |
| 145 | + log "Skipping tests" |
| 146 | + else |
| 147 | + log "Testing" |
| 148 | + (cd "$WORK/src" && pnpm test) |
| 149 | + fi |
| 150 | + |
| 151 | + # --- Pack and publish ------------------------------------------------------- |
| 152 | + |
| 153 | + PUBLISHED=() |
| 154 | + for i in "${!NAMES[@]}"; do |
| 155 | + NAME="${NAMES[$i]}" VERSION="${VERSIONS[$i]}" COMPONENT="${COMPONENTS[$i]}" NPM_TAG="${NPM_TAGS[$i]}" |
| 156 | + if [ "${STATUSES[$i]}" = "on npm already" ] && ! $DRY_RUN; then |
| 157 | + echo "skip $NAME@$VERSION: on npm already" |
| 158 | + continue |
| 159 | + fi |
| 160 | + log "Packing $NAME@$VERSION" |
| 161 | + # `pnpm pack` rewrites `workspace:*` to the dependency's version in this |
| 162 | + # checkout, which a plain `npm publish` of the directory would not. |
| 163 | + TARBALL="$(cd "$WORK/src" && pnpm --filter "./packages/$COMPONENT" pack --json --pack-destination "$WORK/pack" | |
| 164 | + node -p "JSON.parse(require('fs').readFileSync(0, 'utf8')).filename")" |
| 165 | + TGZ_VERSION="$(tar xzOf "$TARBALL" package/package.json | node -p "JSON.parse(require('fs').readFileSync(0, 'utf8')).version")" |
| 166 | + [ "$TGZ_VERSION" = "$VERSION" ] || die "$TARBALL says $TGZ_VERSION, expected $VERSION" |
| 167 | + if $DRY_RUN; then |
| 168 | + if [ "${STATUSES[$i]}" = "on npm already" ]; then |
| 169 | + # `npm publish --dry-run` still refuses a version that is on npm. |
| 170 | + log "Dry run: packed $(basename "$TARBALL"); would skip, on npm already" |
| 171 | + else |
| 172 | + log "Dry run: npm publish $(basename "$TARBALL") --tag $NPM_TAG" |
| 173 | + npm publish "$TARBALL" --dry-run --access public --tag "$NPM_TAG" --registry "$REGISTRY" |
| 174 | + fi |
| 175 | + else |
| 176 | + log "Publishing $NAME@$VERSION with tag $NPM_TAG" |
| 177 | + npm publish "$TARBALL" --access public --tag "$NPM_TAG" --registry "$REGISTRY" |
| 178 | + PUBLISHED+=("$NAME@$VERSION") |
| 179 | + fi |
| 180 | + done |
| 181 | + |
| 182 | + # The registry's read side lags a publish by up to a couple of minutes, and |
| 183 | + # the rerun below checks it before deciding to skip. |
| 184 | + if [ "${#PUBLISHED[@]}" -gt 0 ]; then |
| 185 | + log "Waiting for the registry to show the new versions" |
| 186 | + for SPEC in "${PUBLISHED[@]}"; do |
| 187 | + n=0 |
| 188 | + until npm view "$SPEC" version --registry "$REGISTRY" >/dev/null 2>&1; do |
| 189 | + n=$((n + 1)) |
| 190 | + [ "$n" -le 40 ] || die "$SPEC is not visible after 10 minutes; check npmjs.com before rerunning the workflow" |
| 191 | + sleep 15 |
| 192 | + done |
| 193 | + echo "$SPEC is live" |
| 194 | + done |
| 195 | + fi |
| 196 | +fi |
| 197 | + |
| 198 | +# --- Container images --------------------------------------------------------- |
| 199 | + |
| 200 | +if $DRY_RUN; then |
| 201 | + log "Dry run: not rerunning the Release Please run" |
| 202 | + exit 0 |
| 203 | +fi |
| 204 | +if ! $RERUN; then |
| 205 | + log "Skipping the workflow rerun (--no-rerun)" |
| 206 | + exit 0 |
| 207 | +fi |
| 208 | + |
| 209 | +log "Rerunning the failed Release Please run for ${SHA:0:7} to build the container images" |
| 210 | +RUN_LINE="$(gh run list --repo "$REPO" --workflow release-please.yml --commit "$SHA" --limit 1 \ |
| 211 | + --json databaseId,status,conclusion --jq '.[0] | select(. != null) | [(.databaseId | tostring), .status, (.conclusion // "none")] | join(" ")')" |
| 212 | +if [ -z "$RUN_LINE" ]; then |
| 213 | + echo "no Release Please run found for ${SHA:0:7}; rerun it by hand when it exists" |
| 214 | + exit 0 |
| 215 | +fi |
| 216 | +read -r RUN_ID RUN_STATUS RUN_CONCLUSION <<<"$RUN_LINE" |
| 217 | +RUN_URL="https://github.com/$REPO/actions/runs/$RUN_ID" |
| 218 | +if [ "$RUN_STATUS" != "completed" ]; then |
| 219 | + echo "run $RUN_ID is $RUN_STATUS; waiting for it to finish" |
| 220 | + gh run watch "$RUN_ID" --repo "$REPO" >/dev/null 2>&1 || true |
| 221 | + RUN_CONCLUSION="$(gh run view "$RUN_ID" --repo "$REPO" --json conclusion --jq '.conclusion // "none"')" |
| 222 | +fi |
| 223 | +case "$RUN_CONCLUSION" in |
| 224 | + success) |
| 225 | + echo "run $RUN_ID already succeeded; nothing to rerun ($RUN_URL)" |
| 226 | + ;; |
| 227 | + *) |
| 228 | + gh run rerun "$RUN_ID" --repo "$REPO" --failed |
| 229 | + echo "rerun started: $RUN_URL" |
| 230 | + gh run watch "$RUN_ID" --repo "$REPO" --exit-status >/dev/null 2>&1 || |
| 231 | + die "the rerun failed; see $RUN_URL" |
| 232 | + echo "container images published" |
| 233 | + ;; |
| 234 | +esac |
| 235 | + |
| 236 | +log "Done" |
0 commit comments