From 3cde72d7f879a8125f78e3dc928992f05e4dcc1a Mon Sep 17 00:00:00 2001 From: Huakun Shen Date: Fri, 7 Feb 2025 00:52:57 -0500 Subject: [PATCH] fix: improve npm publish workflow to check for existing package version before publishing --- .github/workflows/npm-publish.yml | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index ae3b676..c3faf05 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -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}}