diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index de7f31b..b7b50ba 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -1,4 +1,4 @@ -name: Publish to npm +name: Publish to npm (Only if version changes) on: push: @@ -6,12 +6,14 @@ on: - 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 @@ -19,13 +21,45 @@ jobs: 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."