diff --git a/.github/workflows/test_and_release.yml b/.github/workflows/test_and_release.yml index 9a4f0a986..2701522a5 100644 --- a/.github/workflows/test_and_release.yml +++ b/.github/workflows/test_and_release.yml @@ -46,7 +46,10 @@ jobs: name: Release NPM package runs-on: ubuntu-24.04 needs: test - if: ${{ github.event_name == 'release' }} + # A release publishes the package. A manual run only rehearses the publish: + # it checks that npm accepts this workflow as a trusted publisher, and + # publishes nothing. + if: ${{ github.event_name == 'release' || github.event_name == 'workflow_dispatch' }} # npm trusted publishing: npm exchanges the job's OIDC token for a publish # token, so no NPM_TOKEN secret is needed permissions: @@ -65,6 +68,7 @@ jobs: node-version: '24.14.1' - name: Check package version matches tag + if: ${{ github.event_name == 'release' }} env: RELEASE_TAG: ${{ github.event.release.tag_name }} run: | @@ -77,6 +81,7 @@ jobs: # X.Y-dev for dev releases, latest otherwise - name: Set release tag id: tag + if: ${{ github.event_name == 'release' }} env: RELEASE_TAG: ${{ github.event.release.tag_name }} run: | @@ -93,6 +98,23 @@ jobs: # repository.url to name this repository. --loglevel verbose is the only # level at which npm logs why an OIDC token exchange failed. - name: Publish NPM package + if: ${{ github.event_name == 'release' }} env: NPM_TAG: ${{ steps.tag.outputs.result }} run: npm publish --ignore-scripts --access public --tag "$NPM_TAG" --loglevel verbose + + # npm does the OIDC exchange on a dry run too, and without a token a dry + # run only warns, so the result is read from npm's verbose log. The text + # matched is the one printed by the npm that ships with the Node version + # pinned above. The explicit --tag is needed because npm rejects a + # prerelease version without one before the exchange; a dry run never + # applies it. + - name: Rehearse the publish + if: ${{ github.event_name == 'workflow_dispatch' }} + run: | + LOG="$RUNNER_TEMP/npm-publish-dry-run.log" + npm publish --dry-run --ignore-scripts --access public --tag rehearsal --loglevel verbose 2>&1 | tee "$LOG" + if ! grep -q 'oidc Successfully retrieved and set token' "$LOG"; then + echo "::error::npm did not accept this workflow as a trusted publisher of this package. See the oidc lines above." + exit 1 + fi