44 release :
55 types : [published]
66 workflow_dispatch :
7+ # GitHub does not start new workflow runs for events caused by the default
8+ # GITHUB_TOKEN (e.g. gh release create in another workflow). After
9+ # "Release on merge" creates a release, trigger publish here instead.
10+ workflow_run :
11+ workflows : [Release on merge]
12+ types : [completed]
713
814permissions :
915 contents : read
1016 id-token : write
1117
1218jobs :
1319 publish :
20+ if : >-
21+ github.event_name != 'workflow_run' ||
22+ github.event.workflow_run.conclusion == 'success'
1423 runs-on : ubuntu-latest
1524 steps :
1625 - uses : actions/checkout@v6
26+ with :
27+ ref : ${{ github.event_name == 'workflow_run' && 'main' || github.event_name == 'release' && github.ref || 'main' }}
28+
29+ - name : Decide whether to publish
30+ id : gate
31+ env :
32+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
33+ run : |
34+ set -euo pipefail
35+ if [[ "${{ github.event_name }}" != "workflow_run" ]]; then
36+ echo "publish=true" >> "${GITHUB_OUTPUT}"
37+ exit 0
38+ fi
39+ VERSION="$(node -p "require('./package.json').version")"
40+ TAG="v-${VERSION}"
41+ if gh release view "${TAG}" --repo "${{ github.repository }}" >/dev/null 2>&1; then
42+ echo "publish=true" >> "${GITHUB_OUTPUT}"
43+ else
44+ echo "No GitHub release ${TAG} yet (or release job was skipped); skipping publish."
45+ echo "publish=false" >> "${GITHUB_OUTPUT}"
46+ fi
1747
1848 # Omit registry-url: setup-node otherwise sets NODE_AUTH_TOKEN to a placeholder and npm publish uses that instead of OIDC.
1949 - name : Setup Node
50+ if : steps.gate.outputs.publish == 'true'
2051 uses : actions/setup-node@v6
2152 with :
2253 node-version : 22
2354 check-latest : true
2455 cache : npm
2556
2657 - name : Upgrade npm for trusted publishing (OIDC)
58+ if : steps.gate.outputs.publish == 'true'
2759 run : npm install -g npm@">=11.5.1"
2860
2961 - name : Ensure versions match
62+ if : steps.gate.outputs.publish == 'true'
3063 shell : bash
3164 run : |
3265 set -euo pipefail
@@ -46,11 +79,14 @@ jobs:
4679 fi
4780
4881 - name : Install dependencies
82+ if : steps.gate.outputs.publish == 'true'
4983 run : npm install --ignore-scripts --no-package-lock
5084
5185 - name : Publish to npm
86+ if : steps.gate.outputs.publish == 'true'
5287 run : npm publish --access public --provenance
5388
5489 - name : Publish to JSR
90+ if : steps.gate.outputs.publish == 'true'
5591 run : npx jsr publish
5692
0 commit comments