|
| 1 | +name: Publish to npm |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + # The npm package lives in js/; only publish when it (or this workflow) changes. |
| 7 | + paths: |
| 8 | + - 'js/**' |
| 9 | + - '.github/workflows/publish-npm.yml' |
| 10 | + workflow_dispatch: |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: publish-npm-${{ github.ref }} |
| 14 | + cancel-in-progress: false |
| 15 | + |
| 16 | +permissions: |
| 17 | + contents: read |
| 18 | + |
| 19 | +jobs: |
| 20 | + publish: |
| 21 | + if: github.ref == 'refs/heads/main' |
| 22 | + runs-on: ubuntu-latest |
| 23 | + timeout-minutes: 15 |
| 24 | + defaults: |
| 25 | + run: |
| 26 | + working-directory: js |
| 27 | + steps: |
| 28 | + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 29 | + with: |
| 30 | + persist-credentials: false |
| 31 | + |
| 32 | + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 |
| 33 | + with: |
| 34 | + # Node 22: current LTS, comfortably covers the JS toolchain (Vite/Vitest). |
| 35 | + node-version: '22' |
| 36 | + registry-url: 'https://registry.npmjs.org' |
| 37 | + |
| 38 | + - name: Install dependencies |
| 39 | + run: npm ci |
| 40 | + |
| 41 | + - name: Build |
| 42 | + run: npm run build |
| 43 | + |
| 44 | + - name: Test |
| 45 | + run: npm test --if-present |
| 46 | + |
| 47 | + - name: Check if version is already published |
| 48 | + id: check |
| 49 | + run: | |
| 50 | + set -euo pipefail |
| 51 | + PKG_NAME=$(node -p "require('./package.json').name") |
| 52 | + PKG_VERSION=$(node -p "require('./package.json').version") |
| 53 | + echo "name=$PKG_NAME" >> "$GITHUB_OUTPUT" |
| 54 | + echo "version=$PKG_VERSION" >> "$GITHUB_OUTPUT" |
| 55 | + if npm view "$PKG_NAME@$PKG_VERSION" version > /dev/null 2>npm_view.err; then |
| 56 | + echo "$PKG_NAME@$PKG_VERSION already published on npm, skipping." |
| 57 | + echo "should_publish=false" >> "$GITHUB_OUTPUT" |
| 58 | + elif grep -q 'E404' npm_view.err; then |
| 59 | + echo "$PKG_NAME@$PKG_VERSION not found on npm, will publish." |
| 60 | + echo "should_publish=true" >> "$GITHUB_OUTPUT" |
| 61 | + else |
| 62 | + echo "npm view failed unexpectedly; aborting publish check." >&2 |
| 63 | + cat npm_view.err >&2 |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | +
|
| 67 | + - name: Publish |
| 68 | + if: steps.check.outputs.should_publish == 'true' |
| 69 | + run: npm publish --access public |
| 70 | + env: |
| 71 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
0 commit comments