Publish #7
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 | |
| on: | |
| release: | |
| types: [published] | |
| workflow_dispatch: | |
| # GitHub does not start new workflow runs for events caused by the default | |
| # GITHUB_TOKEN (e.g. gh release create in another workflow). After | |
| # "Release on merge" creates a release, trigger publish here instead. | |
| workflow_run: | |
| workflows: [Release on merge] | |
| types: [completed] | |
| permissions: | |
| contents: read | |
| id-token: write | |
| jobs: | |
| publish: | |
| if: >- | |
| github.event_name != 'workflow_run' || | |
| github.event.workflow_run.conclusion == 'success' | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v6 | |
| with: | |
| ref: ${{ github.event_name == 'workflow_run' && 'main' || github.event_name == 'release' && github.ref || 'main' }} | |
| - name: Decide whether to publish | |
| id: gate | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| set -euo pipefail | |
| if [[ "${{ github.event_name }}" != "workflow_run" ]]; then | |
| echo "publish=true" >> "${GITHUB_OUTPUT}" | |
| exit 0 | |
| fi | |
| VERSION="$(node -p "require('./package.json').version")" | |
| TAG="v-${VERSION}" | |
| if gh release view "${TAG}" --repo "${{ github.repository }}" >/dev/null 2>&1; then | |
| echo "publish=true" >> "${GITHUB_OUTPUT}" | |
| else | |
| echo "No GitHub release ${TAG} yet (or release job was skipped); skipping publish." | |
| echo "publish=false" >> "${GITHUB_OUTPUT}" | |
| fi | |
| # Omit registry-url: setup-node otherwise sets NODE_AUTH_TOKEN to a placeholder and npm publish uses that instead of OIDC. | |
| - name: Setup Node | |
| if: steps.gate.outputs.publish == 'true' | |
| uses: actions/setup-node@v6 | |
| with: | |
| node-version: 22 | |
| check-latest: true | |
| cache: npm | |
| - name: Upgrade npm for trusted publishing (OIDC) | |
| if: steps.gate.outputs.publish == 'true' | |
| run: npm install -g npm@">=11.5.1" | |
| - name: Ensure versions match | |
| if: steps.gate.outputs.publish == 'true' | |
| shell: bash | |
| run: | | |
| set -euo pipefail | |
| PKG_VERSION="$(node -p "require('./package.json').version")" | |
| JSR_VERSION="$(node -p "require('./jsr.json').version")" | |
| TAG_NAME="${{ github.event.release.tag_name }}" | |
| if [[ -z "$TAG_NAME" ]]; then | |
| TAG_NAME="v-${PKG_VERSION}" | |
| fi | |
| if [[ "$PKG_VERSION" != "$JSR_VERSION" ]]; then | |
| echo "Version mismatch: package.json=$PKG_VERSION, jsr.json=$JSR_VERSION" | |
| exit 1 | |
| fi | |
| if [[ "$TAG_NAME" != "v$PKG_VERSION" && "$TAG_NAME" != "$PKG_VERSION" && "$TAG_NAME" != "v-${PKG_VERSION}" ]]; then | |
| echo "Release tag '$TAG_NAME' does not match version '$PKG_VERSION' (expected '$PKG_VERSION', 'v$PKG_VERSION', or 'v-${PKG_VERSION}')." | |
| exit 1 | |
| fi | |
| - name: Install dependencies | |
| if: steps.gate.outputs.publish == 'true' | |
| run: npm install --ignore-scripts --no-package-lock | |
| - name: Publish to npm | |
| if: steps.gate.outputs.publish == 'true' | |
| run: npm publish --access public --provenance | |
| - name: Publish to JSR | |
| if: steps.gate.outputs.publish == 'true' | |
| run: npx jsr publish | |