Skip to content

feat: implement version check and conditional publishing in workflow;… #2

feat: implement version check and conditional publishing in workflow;…

feat: implement version check and conditional publishing in workflow;… #2

Workflow file for this run

name: Publish to npm
on:
push:
branches:
- master
paths:
- 'package.json'
workflow_dispatch:
jobs:
publish:
runs-on: ubuntu-latest
environment: npm-publish
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
with:
node-version: '24'
registry-url: 'https://registry.npmjs.org'
- name: Check published version
id: version_check
run: |
PACKAGE_NAME=$(node -p "require('./package.json').name")
PACKAGE_VERSION=$(node -p "require('./package.json').version")
PUBLISHED_VERSION=$(npm view "$PACKAGE_NAME" version 2>/dev/null || true)
echo "package_name=$PACKAGE_NAME" >> "$GITHUB_OUTPUT"
echo "package_version=$PACKAGE_VERSION" >> "$GITHUB_OUTPUT"
echo "published_version=$PUBLISHED_VERSION" >> "$GITHUB_OUTPUT"
if [ "$PACKAGE_VERSION" = "$PUBLISHED_VERSION" ]; then
echo "should_publish=false" >> "$GITHUB_OUTPUT"
echo "$PACKAGE_NAME@$PACKAGE_VERSION is already published."
else
echo "should_publish=true" >> "$GITHUB_OUTPUT"
echo "Publishing $PACKAGE_NAME@$PACKAGE_VERSION."
fi
- name: Skip publish
if: steps.version_check.outputs.should_publish != 'true'
run: |
echo "Skipping publish for \
${{ steps.version_check.outputs.package_name }}@\
${{ steps.version_check.outputs.package_version }}"
- run: npm ci
if: steps.version_check.outputs.should_publish == 'true'
- run: npm publish --provenance --access public
if: steps.version_check.outputs.should_publish == 'true'
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}