Publish to npm on tag #15
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 tag | |
| on: | |
| push: | |
| tags: | |
| - "v*" | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: "Tag to publish (e.g. v1.0.0). Defaults to current ref." | |
| required: false | |
| defaults: | |
| run: | |
| shell: bash | |
| jobs: | |
| test-and-publish: | |
| name: Test and publish package | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "24" | |
| registry-url: "https://registry.npmjs.org" | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build TypeScript | |
| run: npm run build | |
| - name: Run tests | |
| run: npm test | |
| - name: Publish to npm | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
| run: | | |
| # Ensure we are on the right tag ref when triggered by push | |
| if [ "${{ github.event_name }}" = "push" ]; then | |
| TAG_REF="${GITHUB_REF#refs/tags/}" | |
| echo "Publishing version for tag $TAG_REF" | |
| else | |
| TAG_REF="${{ github.event.inputs.tag || '' }}" | |
| echo "Manual publish for tag $TAG_REF (if empty, uses current ref)" | |
| fi | |
| if [ -n "$TAG_REF" ]; then | |
| VERSION_FROM_TAG="${TAG_REF#v}" | |
| echo "Setting package.json version to $VERSION_FROM_TAG based on tag $TAG_REF" | |
| npm version "$VERSION_FROM_TAG" --no-git-tag-version | |
| else | |
| echo "TAG_REF is empty, skipping package.json version update" | |
| fi | |
| npm publish --provenance --access public |