From 29aafde84c870c6f77a2857de20dbe699ca5a257 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sun, 2 Aug 2026 22:38:56 -0500 Subject: [PATCH] fix(ci): make Verdaccio staging idempotent Stage only release branches and preserve immutable versions. Repeated identical candidates now exit successfully, while content changes under an existing version require a prerelease bump. Signed-off-by: Diego Resendez --- .github/workflows/stage.yml | 36 +++++++++++++++++++++++----------- scripts/staging-integrity.ts | 13 ++++++++++++ test/staging-integrity.test.ts | 19 ++++++++++++++++++ test/staging-workflow.test.ts | 17 ++++++++++++++++ 4 files changed, 74 insertions(+), 11 deletions(-) create mode 100644 scripts/staging-integrity.ts create mode 100644 test/staging-integrity.test.ts create mode 100644 test/staging-workflow.test.ts diff --git a/.github/workflows/stage.yml b/.github/workflows/stage.yml index e3d2c15..792f854 100644 --- a/.github/workflows/stage.yml +++ b/.github/workflows/stage.yml @@ -1,13 +1,12 @@ name: Stage → Verdaccio -# Staging publish. Runs on GITEA ONLY (the `if` guards against GitHub), on every merge -# to main. Publishes the package as-is — version comes from package.json (a prerelease -# like 26.7.1-beta.N) — to the internal Verdaccio under the `stg` dist-tag. This registry -# rejects overwriting an existing version, so the step unpublishes it first (a no-op on the -# first publish) then publishes fresh. +# Staging publish. Runs on GITEA ONLY (the `if` guards against GitHub) for release +# branches. Publishes prereleases to the internal Verdaccio under the `stg` dist-tag. +# Existing versions are skipped only when the packed contents are identical; changed +# contents under an existing version fail and require a prerelease bump. on: push: - branches: [main] + branches: ['release/**'] jobs: publish-stg: @@ -19,13 +18,28 @@ jobs: with: node-version: '22' - run: npm ci - # `npm publish` runs prepublishOnly (lint + complexity + typecheck + test + build) - # as its own gate before anything leaves the machine. - - name: Publish to Verdaccio (unpublish + publish) + - run: npm audit + - name: Validate release candidate + run: npm run prepublishOnly + - name: Publish prerelease to Verdaccio env: NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH }} run: | npm config set //npm.svc.zoit.services/:_authToken "$NODE_AUTH_TOKEN" VER=$(node -p "require('./package.json').version") - npm unpublish "@green-tea/core@$VER" --registry https://npm.svc.zoit.services --force 2>/dev/null || true - npm publish --registry https://npm.svc.zoit.services --tag stg + case "$VER" in + *-*) ;; + *) echo "Refusing to stage non-prerelease version: $VER" >&2; exit 1 ;; + esac + + PACK_JSON=$(npm pack --json --dry-run --ignore-scripts) + LOCAL_INTEGRITY=$(node -e "let s='';process.stdin.on('data',d=>s+=d);process.stdin.on('end',()=>console.log(JSON.parse(s)[0].integrity))" <<< "$PACK_JSON") + EXISTING_INTEGRITY=$(npm view "@green-tea/core@$VER" dist.integrity --registry https://npm.svc.zoit.services 2>/dev/null || true) + ACTION=$(npx tsx scripts/staging-integrity.ts "$LOCAL_INTEGRITY" ${EXISTING_INTEGRITY:+"$EXISTING_INTEGRITY"}) + + if [ "$ACTION" = "skip" ]; then + echo "@green-tea/core@$VER is already staged with identical contents" + exit 0 + fi + + npm publish --ignore-scripts --registry https://npm.svc.zoit.services --tag stg diff --git a/scripts/staging-integrity.ts b/scripts/staging-integrity.ts new file mode 100644 index 0000000..e24199d --- /dev/null +++ b/scripts/staging-integrity.ts @@ -0,0 +1,13 @@ +export type StagingAction = 'publish' | 'skip'; + +export function decideStagingPublish(localIntegrity: string, existingIntegrity?: string): StagingAction { + if (existingIntegrity === undefined) return 'publish'; + if (existingIntegrity === localIntegrity) return 'skip'; + + throw new Error('Version already exists in Verdaccio with different contents'); +} + +if (require.main === module) { + const [, , localIntegrity, existingIntegrity] = process.argv; + console.log(decideStagingPublish(localIntegrity, existingIntegrity)); +} diff --git a/test/staging-integrity.test.ts b/test/staging-integrity.test.ts new file mode 100644 index 0000000..ea71f4c --- /dev/null +++ b/test/staging-integrity.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest'; + +import { decideStagingPublish } from '../scripts/staging-integrity'; + +describe('decideStagingPublish', () => { + it('publishes a version that is not in Verdaccio', () => { + expect(decideStagingPublish('sha512-local')).toBe('publish'); + }); + + it('skips an existing version with identical package contents', () => { + expect(decideStagingPublish('sha512-local', 'sha512-local')).toBe('skip'); + }); + + it('rejects an existing version with different package contents', () => { + expect(() => decideStagingPublish('sha512-local', 'sha512-other')).toThrow( + 'Version already exists in Verdaccio with different contents', + ); + }); +}); diff --git a/test/staging-workflow.test.ts b/test/staging-workflow.test.ts new file mode 100644 index 0000000..3d2e4f4 --- /dev/null +++ b/test/staging-workflow.test.ts @@ -0,0 +1,17 @@ +import { readFileSync } from 'node:fs'; +import { resolve } from 'node:path'; + +import { describe, expect, it } from 'vitest'; + +const workflow = readFileSync(resolve(process.cwd(), '.github/workflows/stage.yml'), 'utf8'); + +describe('Verdaccio staging workflow', () => { + it('publishes immutable prereleases only from release branches', () => { + expect(workflow).toContain("branches: ['release/**']"); + expect(workflow).toContain('npm audit'); + expect(workflow).toContain('npm run prepublishOnly'); + expect(workflow).toContain('scripts/staging-integrity.ts'); + expect(workflow).toContain('npm publish --ignore-scripts'); + expect(workflow).not.toContain('npm unpublish'); + }); +});