Publish npm package #1
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 npm package | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: "Git tag to publish (for example, v0.1.2). Leave blank to publish the checked-out ref." | |
| required: false | |
| type: string | |
| permissions: | |
| contents: read | |
| id-token: write | |
| concurrency: | |
| group: npm-publish | |
| cancel-in-progress: false | |
| jobs: | |
| publish: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Check out repository | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ inputs.tag || github.ref }} | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| registry-url: https://registry.npmjs.org | |
| cache: npm | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Syntax check | |
| run: | | |
| find src scripts test -name '*.mjs' -print0 | xargs -0 -n1 node --check | |
| - name: Run tests | |
| run: npm test | |
| - name: Check package contents | |
| run: npm pack --dry-run | |
| - name: Verify tag matches package version | |
| if: ${{ inputs.tag != '' }} | |
| env: | |
| INPUT_TAG: ${{ inputs.tag }} | |
| run: | | |
| set -euo pipefail | |
| package_version=$(node -p "require('./package.json').version") | |
| expected_tag="v${package_version}" | |
| if [ "${expected_tag}" != "${INPUT_TAG}" ]; then | |
| echo "Input tag ${INPUT_TAG} does not match package.json version ${package_version} (${expected_tag})" | |
| exit 1 | |
| fi | |
| - name: Publish to npm | |
| run: npm publish --provenance --access public | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |