fix: improve npm publish workflow to check for existing package version before publishing

This commit is contained in:
Huakun Shen 2025-02-07 00:52:57 -05:00
parent 8376f2acf8
commit 3cde72d7f8
No known key found for this signature in database

View File

@ -1,6 +1,3 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
name: NPM Package Publish
on:
@ -25,21 +22,16 @@ jobs:
- uses: oven-sh/setup-bun@v2
- run: bun install
- run: bun run build
- run: |
bunx kksh@latest verify --publish
- run: bunx kksh@latest verify --publish
- name: Check if version is already published
run: |
PACKAGE_VERSION=$(node -p "require('./package.json').version")
PACKAGE_NAME=$(jq -r '.name' package.json)
PACKAGE_VERSION=$(jq -r '.version' package.json)
# Get the version from npm registry
REGISTRY_VERSION=$(npm show "$PACKAGE_NAME" version)
# Compare versions
if [ "$PACKAGE_VERSION" == "$REGISTRY_VERSION" ]; then
echo "Version $PACKAGE_VERSION already exists in the npm registry."
exit 0
else
echo "Version $PACKAGE_VERSION does not exist in the npm registry. Proceeding..."
npm publish --provenance --access public
fi
npm view $PACKAGE_NAME@$PACKAGE_VERSION
continue-on-error: true
id: check_version
- name: Publish
if: steps.check_version.outcome != 'success'
run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}