Skip to content

Commit 5c6d09f

Browse files
iguangfeiclaude
andcommitted
ci: make the release publish step idempotent
Pushing a v* tag ran an unconditional `pnpm publish`, so a tag pushed after a manual publish — or re-pushed to rerun the checks — failed on a version that was already on npm. Publishing is the only step in this workflow that cannot be repeated, so it is the only one that needs a guard: the version is looked up on the registry first and the step is skipped when it is already there. A missing NPM_TOKEN now fails with the command needed to set it, instead of an opaque auth error from pnpm. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent e9a514e commit 5c6d09f

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

.github/workflows/release.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ jobs:
4242
- run: pnpm build
4343
- run: pnpm test:e2e
4444

45-
- run: pnpm publish --access public --no-git-checks --provenance
45+
# A version already on npm is not a failure: the tag may have been pushed
46+
# after a manual publish, or re-pushed to rerun the checks. Publishing is
47+
# the only step here that cannot be repeated, so it is the only one that
48+
# needs the guard.
49+
- name: Check whether this version is already published
50+
id: published
51+
run: |
52+
NAME=$(node -p "require('./package.json').name")
53+
VERSION=$(node -p "require('./package.json').version")
54+
if npm view "$NAME@$VERSION" version >/dev/null 2>&1; then
55+
echo "already=true" >> "$GITHUB_OUTPUT"
56+
echo "$NAME@$VERSION is already on npm — skipping publish."
57+
else
58+
echo "already=false" >> "$GITHUB_OUTPUT"
59+
echo "$NAME@$VERSION is not on npm yet."
60+
fi
61+
62+
- name: Publish to npm
63+
if: steps.published.outputs.already == 'false'
64+
run: |
65+
if [ -z "$NODE_AUTH_TOKEN" ]; then
66+
echo "::error::NPM_TOKEN is not configured, so this release cannot be published."
67+
echo "Create an Automation token at npmjs.com and add it with:"
68+
echo " gh secret set NPM_TOKEN -R ${{ github.repository }}"
69+
exit 1
70+
fi
71+
pnpm publish --access public --no-git-checks --provenance
4672
env:
4773
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

0 commit comments

Comments
 (0)