feat: update npm publish workflow to validate tag version and bump pa… #3
Workflow file for this run
This file contains hidden or 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 to npm | |
| on: | |
| push: | |
| tags: | |
| - '*.*.*' | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| environment: npm | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Use Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '22' | |
| registry-url: 'https://registry.npmjs.org' | |
| cache: 'npm' | |
| - name: Validate tag matches package version | |
| run: | | |
| TAG_VERSION="${GITHUB_REF_NAME#v}" | |
| PACKAGE_VERSION="$(node -p "require('./package.json').version")" | |
| if [ "$TAG_VERSION" != "$PACKAGE_VERSION" ]; then | |
| echo "Tag version ($TAG_VERSION) does not match package.json version ($PACKAGE_VERSION)." | |
| exit 1 | |
| fi | |
| - name: Check if version already exists on npm | |
| id: check-version | |
| run: | | |
| PACKAGE_NAME="$(node -p "require('./package.json').name")" | |
| PACKAGE_VERSION="$(node -p "require('./package.json').version")" | |
| if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version >/dev/null 2>&1; then | |
| echo "exists=true" >> "$GITHUB_OUTPUT" | |
| echo "${PACKAGE_NAME}@${PACKAGE_VERSION} already exists on npm. Skipping publish." | |
| else | |
| echo "exists=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Install dependencies | |
| if: steps.check-version.outputs.exists == 'false' | |
| run: npm ci | |
| - name: Build package | |
| if: steps.check-version.outputs.exists == 'false' | |
| run: npm run build | |
| - name: Publish to npm | |
| if: steps.check-version.outputs.exists == 'false' | |
| env: | |
| RELEASE_MODE: 'true' | |
| run: npm publish --access public --provenance |