|
| 1 | +name: npm Publish |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: |
| 6 | + - published |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + ref: |
| 10 | + description: Git ref to publish, usually a release tag like v1.0.0 |
| 11 | + required: true |
| 12 | + type: string |
| 13 | + npm_tag: |
| 14 | + description: npm dist-tag to publish under |
| 15 | + required: false |
| 16 | + default: latest |
| 17 | + type: string |
| 18 | + dry_run: |
| 19 | + description: Run npm publish in dry-run mode without uploading |
| 20 | + required: false |
| 21 | + default: false |
| 22 | + type: boolean |
| 23 | + |
| 24 | +permissions: |
| 25 | + contents: read |
| 26 | + |
| 27 | +concurrency: |
| 28 | + group: npm-publish-${{ github.event_name == 'release' && github.event.release.tag_name || inputs.ref }} |
| 29 | + cancel-in-progress: false |
| 30 | + |
| 31 | +jobs: |
| 32 | + publish: |
| 33 | + runs-on: ubuntu-latest |
| 34 | + environment: npm-publish |
| 35 | + |
| 36 | + env: |
| 37 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 38 | + |
| 39 | + steps: |
| 40 | + - name: Checkout |
| 41 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 |
| 42 | + with: |
| 43 | + ref: ${{ github.event_name == 'release' && github.event.release.tag_name || inputs.ref }} |
| 44 | + |
| 45 | + - name: Setup Node.js |
| 46 | + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 |
| 47 | + with: |
| 48 | + node-version: 20 |
| 49 | + registry-url: https://registry.npmjs.org |
| 50 | + |
| 51 | + - name: Setup mise |
| 52 | + uses: jdx/mise-action@c37c93293d6b742fc901e1406b8f764f6fb19dac |
| 53 | + |
| 54 | + - name: Install dependencies |
| 55 | + run: mise run install |
| 56 | + |
| 57 | + - name: Validate npm package |
| 58 | + run: mise run npm-smoke |
| 59 | + |
| 60 | + - name: Check published version |
| 61 | + id: package |
| 62 | + shell: bash |
| 63 | + run: | |
| 64 | + set -euo pipefail |
| 65 | + PACKAGE_NAME=$(node --print "require('./package.json').name") |
| 66 | + PACKAGE_VERSION=$(node --print "require('./package.json').version") |
| 67 | + if npm view "${PACKAGE_NAME}@${PACKAGE_VERSION}" version --registry=https://registry.npmjs.org >/dev/null 2>&1; then |
| 68 | + echo "published=true" >> "$GITHUB_OUTPUT" |
| 69 | + echo "${PACKAGE_NAME}@${PACKAGE_VERSION} is already published; skipping." |
| 70 | + else |
| 71 | + echo "published=false" >> "$GITHUB_OUTPUT" |
| 72 | + fi |
| 73 | +
|
| 74 | + - name: Dry run publish |
| 75 | + if: github.event_name == 'workflow_dispatch' && inputs.dry_run |
| 76 | + env: |
| 77 | + NPM_DIST_TAG: ${{ inputs.npm_tag == 'latest' && 'dry-run' || inputs.npm_tag }} |
| 78 | + run: mise run npm-publish-dry-run |
| 79 | + |
| 80 | + - name: Publish package |
| 81 | + if: steps.package.outputs.published != 'true' && !(github.event_name == 'workflow_dispatch' && inputs.dry_run) |
| 82 | + run: npm publish --access public --tag "${{ github.event_name == 'workflow_dispatch' && inputs.npm_tag || 'latest' }}" |
0 commit comments