Publish package to npm #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish package to npm | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- package.json | |
workflow_dispatch: | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: 22 | |
registry-url: 'https://registry.npmjs.org' | |
- name: Get current version and package name | |
id: get_version_and_name | |
run: | | |
PACKAGE_NAME=$(node -p "require('./package.json').name") | |
CURRENT_VERSION=$(node -p "require('./package.json').version") | |
echo "PACKAGE_NAME=${PACKAGE_NAME}" >> $GITHUB_ENV | |
echo "CURRENT_VERSION=${CURRENT_VERSION}" >> $GITHUB_ENV | |
- name: Fetch npm version and compare | |
run: | | |
NPM_VERSION=$(npm show ${PACKAGE_NAME} version || echo "not_found") | |
if [ "$NPM_VERSION" = "not_found" ]; then | |
echo "Package not found in registry. Proceeding with publish." | |
elif [ "$CURRENT_VERSION" != "$NPM_VERSION" ]; then | |
echo "Version has changed. Proceeding with publish." | |
else | |
echo "Version unchanged. Skipping publish." | |
exit 0 | |
fi | |
env: | |
PACKAGE_NAME: ${{ env.PACKAGE_NAME }} | |
CURRENT_VERSION: ${{ env.CURRENT_VERSION }} | |
- name: Install dependencies | |
run: npm install | |
- name: Publish to npm | |
run: npm publish | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |