name: Publish # Surface the inputs everywhere the run is shown, including the environment # approval notification, since the approval UI does not display # workflow_dispatch inputs. run-name: "Publish ${{ github.ref_name }} to npm dist-tag ${{ inputs['dist-tag'] }}${{ inputs.dry-run && ' (dry run)' || '' }}" on: workflow_dispatch: inputs: dist-tag: description: The npm dist-tag (distribution channel) to publish to. options: - latest - rc required: true type: choice dry-run: default: false description: Run npm publish --dry-run instead of publishing. type: boolean permissions: # Reading the repository is the only default permission either job needs. contents: read # Never run two publishes at once (interleaved dependency levels could # publish a package before its dependencies); queue instead of cancelling # so an in-flight publish always completes. concurrency: group: publish cancel-in-progress: false jobs: # Runs without approval so the npm-publish environment reviewer can open # the run, read what is being published (and whether it is valid) in the # step summary, then approve or reject the gated publish job. preflight: name: Preflight runs-on: ubuntu-latest permissions: # Reads the repository and the latest release for the summary. contents: read steps: - name: Checkout repository uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: persist-credentials: false - name: Setup Node.js uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: node-version: 24 - name: Pin npm uses: ./.github/actions/pin-npm - name: Assert valid workflow inputs env: DIST_TAG: ${{ inputs['dist-tag'] }} DRY_RUN: ${{ inputs.dry-run }} run: | # The web form enforces the choice list, but API dispatches do not: # a missing input arrives as an empty string, and an empty dist-tag # would publish `--tag ""`. Validating both inputs here also means # the later steps (including the approval-facing summary) only ever # see known values. case "$DIST_TAG" in latest|rc) ;; *) echo "::error::Unknown npm dist-tag '$DIST_TAG'; expected 'latest' or 'rc'." exit 1 ;; esac case "$DRY_RUN" in true|false) ;; *) echo "::error::Invalid dry-run value; expected 'true' or 'false'." exit 1 ;; esac - name: Assert the workflow was dispatched on a release tag env: REF_TYPE: ${{ github.ref_type }} REF_NAME: ${{ github.ref_name }} run: | # Publishing from a moving branch makes it ambiguous what was # released; require a release tag (vX.Y.Z, with an optional # pre-release suffix such as v1.7.0-rc.1). if [ "$REF_TYPE" != "tag" ]; then echo "::error::Dispatch this workflow on a release tag (vX.Y.Z or vX.Y.Z-), not on branch '$REF_NAME'." exit 1 fi if ! printf '%s' "$REF_NAME" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$'; then echo "::error::Tag '$REF_NAME' does not look like a release tag (vX.Y.Z or vX.Y.Z-)." exit 1 fi - name: Assert the package versions match the tag env: EXPECTED: ${{ github.ref_name }} run: | # Every package version must equal the tag being published (the # workspace releases in lockstep via linked-versions). expected="${EXPECTED#v}" # `npm pkg get` prints a plain-text table unless asked for `--json`, # and under `--workspaces` it keys each workspace to an object of the # requested fields, so the value is read as `.value.version` rather # than as the version string itself. # The jq program is single-quoted on purpose. # shellcheck disable=SC2016 mismatched="$(npm pkg get version --workspaces --json | jq -r --arg v "$expected" 'to_entries[] | select(.value.version != $v) | "\(.key)@\(.value.version)"')" if [ -n "$mismatched" ]; then echo "$mismatched" echo "::error::These package versions do not match tag '$EXPECTED'; dispatch the workflow on the tag that matches the versions being published." exit 1 fi - name: Assert stable versions when publishing to the latest dist-tag if: inputs['dist-tag'] == 'latest' run: | # The jq program is single-quoted on purpose. # shellcheck disable=SC2016 # Publishing to `latest` requires stable semver versions (x.y.z); # anything else (1.7.0-rc.1, 2.0.0-beta.0, ...) must go to a # pre-release channel such as `rc`. Runs before the approval gate # so an invalid request fails without asking a human. invalid="$(npm pkg get version --workspaces --json | jq -r 'to_entries[] | select(.value.version | test("^[0-9]+\\.[0-9]+\\.[0-9]+$") | not) | "\(.key)@\(.value.version)"')" if [ -n "$invalid" ]; then echo "$invalid" echo "::error::These versions do not look like stable releases; re-run with a pre-release dist-tag (e.g. 'rc') instead of 'latest'." exit 1 fi - name: Summarize the publish request env: DIST_TAG: ${{ inputs['dist-tag'] }} DRY_RUN: ${{ inputs.dry-run }} run: | # The jq program is single-quoted on purpose. # shellcheck disable=SC2016 versions="$(npm pkg get version --workspaces --json | jq -r 'to_entries[] | "\(.key)@\(.value.version)"')" { echo "## Publish request" echo "- npm dist-tag: \`$DIST_TAG\`" echo "- ref: \`$GITHUB_REF_NAME\` (\`$GITHUB_SHA\`)" echo "- dry run: \`$DRY_RUN\`" echo echo "
Versions" echo echo '```' echo "$versions" echo '```' echo "
" } >> "$GITHUB_STEP_SUMMARY" publish: name: Publish needs: preflight environment: npm-publish runs-on: ubuntu-latest permissions: # Reads the repository to build and publish the packages. contents: read id-token: write # Mints the OIDC token for npm provenance and trusted publishing. steps: - name: Checkout repository uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: persist-credentials: false - name: Setup Node.js uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: node-version: 24 registry-url: https://registry.npmjs.org - name: Pin npm uses: ./.github/actions/pin-npm - name: Install dependencies run: npm ci - name: Run tests run: npm test - name: Publish packages env: DIST_TAG: ${{ inputs['dist-tag'] }} # dry-run is a boolean with no publishConfig equivalent, so passing # it via the environment is safe and applies to every publish below. npm_config_dry_run: ${{ inputs.dry-run }} run: | # Pass the dist-tag with --tag on each publish rather than the # npm_config_tag environment variable. A command-line --tag outranks # a package's publishConfig.tag; the environment variable does not, # so publishConfig.tag would silently win and route the publish to # its tag (e.g. latest) instead of the requested one. # npm publish --workspaces publishes in alphabetical directory order, # NOT dependency order. This means packages like @arcjet/astro get # published before their dependencies (e.g. @arcjet/body, @arcjet/env) # exist on the registry, causing 409 Conflict errors. # # To fix this, we publish in levels based on the dependency graph. # Each level is a single command. Packages within a level have no # internal dependencies on each other. Each level's dependencies # are fully published before the next level starts. # If you add a new package, insert it in the correct level. # Level 0: no internal dependencies npm publish --tag "$DIST_TAG" \ --workspace @arcjet/analyze-wasm \ --workspace @arcjet/body \ --workspace @arcjet/cache \ --workspace @arcjet/duration \ --workspace @arcjet/env \ --workspace @arcjet/headers \ --workspace @arcjet/ip \ --workspace @arcjet/redact-wasm \ --workspace @arcjet/runtime \ --workspace @arcjet/skills \ --workspace @arcjet/sprintf \ --workspace @arcjet/stable-hash \ --workspace @arcjet/transport \ --workspace nosecone # Level 1: depend on level 0 npm publish --tag "$DIST_TAG" \ --workspace @arcjet/logger \ --workspace @arcjet/protocol \ --workspace @arcjet/redact \ --workspace @nosecone/next \ --workspace @nosecone/sveltekit # Level 2: depend on levels 0-1 npm publish --tag "$DIST_TAG" \ --workspace @arcjet/analyze \ --workspace @arcjet/decorate \ --workspace @arcjet/inspect # Level 3: core SDK + guard (depend on @arcjet/analyze from Level 2) npm publish --tag "$DIST_TAG" \ --workspace arcjet \ --workspace @arcjet/guard # Level 4: public framework integrations npm publish --tag "$DIST_TAG" \ --workspace @arcjet/astro \ --workspace @arcjet/bun \ --workspace @arcjet/deno \ --workspace @arcjet/fastify \ --workspace @arcjet/nest \ --workspace @arcjet/next \ --workspace @arcjet/node \ --workspace @arcjet/nuxt \ --workspace @arcjet/react-router \ --workspace @arcjet/remix \ --workspace @arcjet/sensitive-info-rampart \ --workspace @arcjet/sveltekit - name: Verify dist-tags # Nothing was published on a dry run, so there is nothing to verify. if: ${{ !inputs.dry-run }} timeout-minutes: 25 env: DIST_TAG: ${{ inputs['dist-tag'] }} EXPECTED: ${{ github.ref_name }} run: | # `npm publish` reports the requested tag but can silently write a # different dist-tag while still exiting 0 - a package's # publishConfig.tag once won over the requested tag, publishing a # pre-release to `latest` and leaving `rc` unmoved, with a green # run. Confirm every published package's requested dist-tag now # resolves to this version so that failure mode fails the job. version="${EXPECTED#v}" # `.[].name` yields the name value regardless of how npm keys the # object. As in preflight, `--json` is required for JSON output and # each workspace's value is an object of the requested fields. read -r -a pending <<< "$(npm pkg get name --workspaces --json | jq -r '.[].name' | tr '\n' ' ')" # npm holds a new version while it processes it ("Your package is # being processed and may take a few minutes to become available"), # so a dist-tag can read the previous version for many minutes after # a successful publish. Poll every unconfirmed package together # against one deadline rather than giving each a few seconds. deadline=$((SECONDS + 20 * 60)) declare -A actual while :; do remaining=() for pkg in "${pending[@]}"; do actual[$pkg]="$(npm view --prefer-online "$pkg" "dist-tags.$DIST_TAG" 2>/dev/null || true)" if [ "${actual[$pkg]}" = "$version" ]; then echo "ok: $pkg $DIST_TAG -> $version" else remaining+=("$pkg") fi done pending=("${remaining[@]}") if [ "${#pending[@]}" -eq 0 ] || [ "$SECONDS" -ge "$deadline" ]; then break fi echo "Waiting for npm to show $DIST_TAG -> $version for: ${pending[*]}" sleep 30 done if [ "${#pending[@]}" -gt 0 ]; then for pkg in "${pending[@]}"; do echo "::error::$pkg dist-tag '$DIST_TAG' is '${actual[$pkg]:-}', expected '$version'." done echo "::error::The publish step completed, but after 20 minutes npm still does not show $DIST_TAG -> $version for: ${pending[*]}. Check the registry before re-running: these versions may already be published." exit 1 fi echo "Verified $DIST_TAG -> $version for all published packages."