Skip to content

Commit

Permalink
feat: Publish if version not exists in npm
Browse files Browse the repository at this point in the history
  • Loading branch information
extg5 committed Aug 19, 2024
1 parent abd81d4 commit 3e43ce8
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
push:
branches:
- master


jobs:
publish:
Expand All @@ -23,28 +22,28 @@ jobs:
node-version: '18'
registry-url: 'https://registry.npmjs.org'

- name: Check if version has changed
id: version_check
- name: Check if version exists on npm
id: npm_version_check
run: |
# Get the current version from package.json
current_version=$(jq -r '.version' package.json)
echo "Current version: $current_version"
# Get the previous version from the last commit
git fetch --depth=2
previous_version=$(git show HEAD^1:package.json | jq -r '.version')
echo "Previous version: $previous_version"
if [ "$current_version" != "$previous_version" ]; then
echo "::set-output name=version_changed::true"
echo "::set-output name=current_version::$current_version"
package_name=$(jq -r '.name' package.json)
echo "Package name: $package_name"
# Check if the version exists on npm
npm_info=$(npm view "$package_name@$current_version" version || echo "not_found")
if [ "$npm_info" == "not_found" ]; then
echo "Version $current_version does not exist on npm."
echo "::set-output name=version_exists::false"
else
echo "::set-output name=version_changed::false"
echo "Version $current_version already exists on npm."
echo "::set-output name=version_exists::true"
fi
- name: Publish to NPM
if: steps.version_check.outputs.version_changed == 'true'
if: steps.npm_version_check.outputs.version_exists == 'false'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_CI_TOKEN }}
run: |
Expand All @@ -53,7 +52,7 @@ jobs:
npm publish
- name: Create GitHub Release
if: steps.version_check.outputs.version_changed == 'true'
if: steps.npm_version_check.outputs.version_exists == 'false'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Expand Down

0 comments on commit 3e43ce8

Please sign in to comment.