# Manual-dispatch publish to npm via Trusted Publishing (OIDC). PR-based, two-phase. # # WHY TWO PHASES: `main` is meant to accept changes only through a PR with a green `test` check, so nothing — this # workflow included — pushes a version bump straight to main. The release therefore splits at the merge boundary: # # PHASE 1 — mode=prepare (dispatch with a version): bump package.json on a `release/v` branch, open the # release PR, and enable auto-merge. The PR merges itself the moment `test` is green; no click required. This needs # "Allow auto-merge" in the repository settings and a ruleset on `main` that requires the `test` check — without the # ruleset, auto-merge has nothing to wait for and merges before the tests run. # # THE PR SHOULD BE HUMAN-AUTHORED. A PR opened with GITHUB_TOKEN gets its `pull_request` Test run HELD at # `action_required` under the repo's approval policy (github-actions[bot] counts as a first-time contributor), and a # held run is a pending required check — auto-merge waits forever. So the branch push and the PR ride # `RELEASE_PR_TOKEN`, a fine-grained PAT (this repo only; contents + pull-requests: write) that makes the PR belong to # a trusted human, whose `pull_request` run fires unheld. Without the secret the job falls back to GITHUB_TOKEN + an # explicit Test dispatch, and warns that the held run will need approving in the Actions UI. # # PHASE 2 — mode=publish (dispatch AFTER the release PR has merged): tag the merged commit, create the GitHub release, # and publish via OIDC. Idempotent by construction — the tag, the release, and the npm publish are each skipped when # they already exist — so re-dispatching after a partial failure is always safe. This same mode IS the recovery path. # # ORDER IN PHASE 2 IS TAG-FIRST, deliberately. A failed publish then leaves a pushed tag with nothing on npm, which is # visible and fixable by re-dispatching. The reverse would leave npm ahead of git, and an npm version is immutable — # there is no taking it back. # # AUTH: npm Trusted Publishing. No NPM_TOKEN anywhere. npmjs.com is configured to accept publishes from this exact # workflow file, matched LITERALLY by path — `.github/workflows/publish.yml`. Renaming or moving this file silently # breaks publishing: the registry never validates that a configured path names a real file, so a mismatch surfaces as a # bare E404 on publish rather than as a configuration error. If a publish 404s and the package exists, check that path # first, then check whether the trusted publisher is bound to a GitHub environment this job does not declare. name: NPM Publish on: workflow_dispatch: inputs: mode: description: "prepare = bump the version + open the auto-merging release PR. publish = tag + npm-publish the ALREADY-MERGED release commit on main (also the partial-failure recovery path)." required: true default: prepare type: choice options: - prepare - publish version: description: "prepare only: patch / minor / major / prerelease, or an exact semver like 1.2.0" required: false default: patch type: string dry_run: description: "Dry run — run everything up to the side effects, then push nothing, open nothing, publish nothing" required: false default: false type: boolean permissions: contents: read # Serialize releases. Never cancel an in-flight run — a prepare may be mid-push and a publish mid-tarball; killing # either leaves a partial state a human has to unwind. concurrency: group: publish cancel-in-progress: false jobs: # PHASE 1 — version bump on a release branch + auto-merging PR. No tag, no npm. prepare: if: ${{ inputs.mode == 'prepare' }} runs-on: ubuntu-latest timeout-minutes: 15 permissions: contents: write # push the release/v branch pull-requests: write # open the release PR + enable auto-merge actions: write # dispatch the Test workflow against the release branch steps: - name: Checkout uses: actions/checkout@v7 with: ref: main fetch-depth: 0 - name: Set up Node uses: actions/setup-node@v7 with: node-version-file: .nvmrc # `npm version` without git side effects: only package.json changes. The commit and branch are made below, under # the allowlist, so an unexpected dirty file is a failure here rather than a silent passenger in the release # commit. - name: Resolve and bump the version id: bump run: | set -euo pipefail NEW_VERSION="$(npm version "${{ inputs.version }}" --no-git-tag-version)" VERSION="${NEW_VERSION#v}" echo "Bumped package.json to ${VERSION}" # Collision guards — the version must be new to both git and npm. if git ls-remote --tags origin "v${VERSION}" | grep -q .; then echo "::error::tag v${VERSION} already exists — pick a different version"; exit 1 fi if npm view "async-init@${VERSION}" version >/dev/null 2>&1; then echo "::error::async-init@${VERSION} is already on npm — pick a different version"; exit 1 fi echo "version=${VERSION}" >> "${GITHUB_OUTPUT}" - name: "Dry run: show the bump and stop" if: ${{ inputs.dry_run }} run: | echo "::notice::DRY RUN — would open a release PR for v${{ steps.bump.outputs.version }} with:" git --no-pager diff - name: Open the auto-merging release PR if: ${{ !inputs.dry_run }} env: # The PAT when present (see the header), else the workflow token. GH_TOKEN: ${{ secrets.RELEASE_PR_TOKEN || secrets.GITHUB_TOKEN }} HAS_RELEASE_PR_TOKEN: ${{ secrets.RELEASE_PR_TOKEN != '' }} WORKFLOW_TOKEN: ${{ secrets.GITHUB_TOKEN }} VERSION: ${{ steps.bump.outputs.version }} run: | set -euo pipefail BRANCH="release/v${VERSION}" git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git checkout -b "${BRANCH}" # Only the manifest may change in a release commit. Anything else dirty is a fault to surface, never to ship. git add package.json UNEXPECTED="$(git status --porcelain --untracked-files=no | grep -vE '^M package\.json$' || true)" if [ -n "${UNEXPECTED}" ]; then echo "::error::changed files outside package.json — refusing to commit them silently:" echo "${UNEXPECTED}" exit 1 fi git commit -m "Release ${VERSION}" # Push with the same identity that opens the PR: the pusher is the triggering actor for `on: pull_request`, so # both operations must ride the same token. git push "https://x-access-token:${GH_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" "HEAD:refs/heads/${BRANCH}" PR_URL="$(gh pr create --base main --head "${BRANCH}" \ --title "Release ${VERSION}" \ --body "Automated release PR (publish.yml mode=prepare). Auto-merges when the required \`test\` check passes; then dispatch \`publish.yml\` with mode=publish to tag and npm-publish.")" echo "release PR: ${PR_URL}" if [ "${HAS_RELEASE_PR_TOKEN}" != "true" ]; then echo "::warning::RELEASE_PR_TOKEN is not set — the PR's pull_request Test run will be held at action_required; approve it in the Actions UI or auto-merge will wait forever." # A dispatched run at least lands a `test` check on the branch head. gh workflow run test.yml --ref "${BRANCH}" fi # Arming auto-merge is a GraphQL mutation (enablePullRequestAutoMerge) a fine-grained PAT is not permitted to # call — "Resource not accessible by personal access token" — so it rides the workflow token, which may. Only # the push and the PR need the human identity. GH_TOKEN="${WORKFLOW_TOKEN}" gh pr merge --auto --squash "${PR_URL}" { echo "## Release PR opened: v${VERSION}" echo "" echo "- ${PR_URL} (auto-merges when \`test\` is green)" echo "- Next: dispatch this workflow with **mode=publish** after the merge." } >> "${GITHUB_STEP_SUMMARY}" # PHASE 2 — tag + GitHub release + npm publish of the merged release commit on main. publish: if: ${{ inputs.mode == 'publish' }} # GITHUB-HOSTED ON PURPOSE — do not move this to a self-hosted runner. npm refuses provenance attestation from one # outright: E422 `Unsupported GitHub Actions runner environment: "self-hosted". Only "github-hosted" runners are # supported when publishing with provenance`. runs-on: ubuntu-latest timeout-minutes: 20 permissions: contents: write # push the version tag + create the GitHub release id-token: write # npm Trusted Publishing (OIDC) steps: - name: Checkout uses: actions/checkout@v7 with: ref: main fetch-depth: 0 - name: Set up Node uses: actions/setup-node@v7 with: node-version-file: .nvmrc registry-url: https://registry.npmjs.org - name: Enable Corepack (yarn 4) run: corepack enable - name: Install dependencies run: yarn install --immutable # Trusted Publishing needs npm >= 11.5.1 for OIDC. Pinning the requirement here means a future runner image change # fails loudly on this line rather than as an unexplained auth error three steps later. - name: Ensure npm supports OIDC run: | npm install -g npm@latest npm --version # The `test` check already gated the release PR; this re-run is cheap insurance that the tarball is built from # exactly what is on main now, and produces `out/` for the publish. - name: Lint run: yarn lint - name: Compile run: yarn compile # `--run` is required. Bare vitest watches and never exits. - name: Test run: yarn test --run - name: Type tests run: yarn test:types - name: Read the version on main id: version run: | VERSION="$(node -p "require('./package.json').version")" echo "version=${VERSION}" echo "version=${VERSION}" >> "${GITHUB_OUTPUT}" - name: Pack and inspect the tarball run: | npm pack --dry-run test -f out/index.js || { echo "::error::out/index.js missing — compile did not produce the published entry"; exit 1; } test -f out/index.d.ts || { echo "::error::out/index.d.ts missing — no types would ship"; exit 1; } - name: "Dry run: summary and stop" if: ${{ inputs.dry_run }} run: | echo "::notice::DRY RUN — would tag, release, and publish async-init@${{ steps.version.outputs.version }} from $(git rev-parse --short HEAD). Nothing pushed, nothing published." - name: Tag and GitHub release if: ${{ !inputs.dry_run }} env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} VERSION: ${{ steps.version.outputs.version }} run: | set -euo pipefail git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" if git ls-remote --tags origin "v${VERSION}" | grep -q .; then echo "tag v${VERSION} already on origin (recovery re-run) — skipping tag push" else git tag -a "v${VERSION}" -m "Release ${VERSION}" git push origin "v${VERSION}" fi if gh release view "v${VERSION}" >/dev/null 2>&1; then echo "GitHub release v${VERSION} already exists (recovery re-run) — skipping" else gh release create "v${VERSION}" --title "v${VERSION}" --generate-notes fi # `delete_branch_on_merge` does not fire for a merge performed by the Actions app, so the release branch # outlives the PR. Remove it here; a missing branch is not an error. git push origin --delete "release/v${VERSION}" 2>/dev/null || echo "release/v${VERSION} already gone" # --provenance signs a sigstore attestation against this workflow's OIDC identity. It needs a public repo (this one # is) AND a CI identity, so it cannot be reproduced by a local publish. - name: Publish to npm if: ${{ !inputs.dry_run }} env: VERSION: ${{ steps.version.outputs.version }} run: | if npm view "async-init@${VERSION}" version >/dev/null 2>&1; then echo "async-init@${VERSION} is already on npm (recovery re-run) — skipping publish" else npm publish --provenance --access public fi # The registry processes a publish asynchronously, and a version can take over a minute to appear in `npm view`. # Poll for up to 10 minutes rather than sample once. - name: Verify the published version if: ${{ !inputs.dry_run }} env: EXPECTED: ${{ steps.version.outputs.version }} run: | for attempt in $(seq 1 40); do PUBLISHED="$(npm view async-init version 2>/dev/null || true)" echo "attempt ${attempt}: npm reports '${PUBLISHED}', expected ${EXPECTED}" test "${PUBLISHED}" = "${EXPECTED}" && exit 0 sleep 15 done echo "::error::npm still reports '${PUBLISHED}' after 10 minutes; expected ${EXPECTED}" exit 1