Publish to npm #140
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': | |
| workflow_run: | |
| workflows: ['CI'] | |
| types: | |
| - completed | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| jobs: | |
| publish: | |
| # Only run if CI succeeded | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 2 | |
| - name: Check if version changed | |
| id: version-check | |
| run: | | |
| CURRENT_VERSION=$(node -p "require('./package.json').version") | |
| echo "Current version: $CURRENT_VERSION" | |
| PREVIOUS_VERSION=$(git show HEAD~1:package.json 2>/dev/null | \ | |
| node -p "JSON.parse(require('fs').readFileSync(0, 'utf8')).version" 2>/dev/null || echo "") | |
| echo "Previous version: $PREVIOUS_VERSION" | |
| if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then | |
| echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION" | |
| echo "should-publish=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Version unchanged ($CURRENT_VERSION)" | |
| echo "should-publish=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Block pre-release versions | |
| id: pre-release-check | |
| if: steps.version-check.outputs.should-publish == 'true' | |
| run: | | |
| CURRENT_VERSION=$(node -p "require('./package.json').version") | |
| # Check for pre-release identifiers (dev, alpha, beta, rc, etc.) | |
| if [[ "$CURRENT_VERSION" =~ -dev$|-alpha$|-beta$|-rc$ ]]; then | |
| echo "❌ Pre-release version detected: $CURRENT_VERSION" | |
| echo "Pre-release versions are not published to npm registry." | |
| echo "Please use a stable version for publication." | |
| echo "should-publish=false" >> $GITHUB_OUTPUT | |
| else | |
| echo "✅ Stable version: $CURRENT_VERSION" | |
| echo "should-publish=true" >> $GITHUB_OUTPUT | |
| fi | |
| - uses: actions/setup-node@v4 | |
| if: steps.pre-release-check.outputs.should-publish == 'true' | |
| with: | |
| node-version: '20' | |
| registry-url: 'https://registry.npmjs.org' | |
| - run: npm ci | |
| if: steps.pre-release-check.outputs.should-publish == 'true' | |
| - run: npm run build | |
| if: steps.pre-release-check.outputs.should-publish == 'true' | |
| - run: npm publish --provenance --access public | |
| if: steps.pre-release-check.outputs.should-publish == 'true' | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |