Skip to content

Commit

Permalink
Update npm-publish.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
newracket authored Jan 24, 2025
1 parent f10ac42 commit f986196
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
name: Publish to npm
name: Publish to npm (Only if version changes)

on:
push:
branches:
- main

jobs:
publish:
check-and-publish:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 2 # Fetch the last commit for comparison

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 18
registry-url: "https://registry.npmjs.org/"

- name: Get previous package.json version
id: prev_version
run: |
PREV_VERSION=$(git show HEAD~1:package.json | jq -r '.version')
echo "Previous version: $PREV_VERSION"
echo "PREV_VERSION=$PREV_VERSION" >> $GITHUB_ENV
- name: Get current package.json version
id: current_version
run: |
CUR_VERSION=$(jq -r '.version' package.json)
echo "Current version: $CUR_VERSION"
echo "CUR_VERSION=$CUR_VERSION" >> $GITHUB_ENV
- name: Check if version changed
id: version_check
run: |
if [ "$PREV_VERSION" != "$CUR_VERSION" ]; then
echo "Version changed, proceeding with publish."
echo "PUBLISH=true" >> $GITHUB_ENV
else
echo "Version did not change, skipping publish."
echo "PUBLISH=false" >> $GITHUB_ENV
fi
- name: Install dependencies
if: env.PUBLISH == 'true'
run: npm install

- name: Build the package (if using TypeScript)
- name: Build package (if necessary)
if: env.PUBLISH == 'true'
run: npm run build || echo "No build step"

- name: Publish to npm
if: env.PUBLISH == 'true'
run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Skip publishing
if: env.PUBLISH == 'false'
run: echo "No version change detected. Skipping npm publish."

0 comments on commit f986196

Please sign in to comment.