# Publish agent-hands to npm on a GitHub release, using Trusted Publishing. # # No npm token is stored anywhere. GitHub mints a short-lived OIDC identity for this specific # workflow, and npm accepts it because the package is registered with this repo + workflow as a # trusted publisher (npmjs.com -> package settings -> Trusted Publisher). That is why # `id-token: write` below is mandatory: without it there is no identity to present. # # Mirrors the agent-win -> PyPI workflow (same repo owner, same pattern). # # Requires npm CLI >= 11.5.1 for trusted publishing — Node 24 bundles that by default (Node 22 # bundles npm 10, too old). # # Two things had to be right before this actually worked, neither of which produced a clear error: # 1. On npmjs.com, the Trusted Publisher "Repository" field must be the bare repo name # ("agent-hands"), not a full URL. The npm UI happily pre-fills and SAVES a full GitHub URL # there (derived from package.json's repository field) with no validation error, and every # OIDC token exchange then fails with "package not found" — npm's deliberately vague message # for a claims mismatch, per https://github.com/npm/cli/issues/9088. Those identity fields are # locked once saved (npmjs.com says so), so a wrong value means delete and recreate the # connection, not edit it. # 2. See the "Strip the empty _authToken" step below. name: publish on: release: types: [published] workflow_dispatch: # lets you publish by hand from the Actions tab permissions: contents: read jobs: publish: runs-on: ubuntu-latest environment: npm # must match the environment named on npmjs.com, if you set one permissions: id-token: write # required: this is what mints the OIDC token contents: read steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: "24" registry-url: "https://registry.npmjs.org" - name: Verify package version matches the release tag if: github.event_name == 'release' run: | PKG_VERSION=$(node -p "require('./package.json').version") TAG_VERSION="${GITHUB_REF_NAME#v}" if [ "$PKG_VERSION" != "$TAG_VERSION" ]; then echo "package.json version ($PKG_VERSION) does not match release tag ($GITHUB_REF_NAME)" exit 1 fi - name: Strip the empty _authToken setup-node writes to .npmrc # setup-node's registry-url writes //registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}. # With no NODE_AUTH_TOKEN (the whole point of trusted publishing: no token to set), that # line is present but empty — and npm treats "an _authToken line exists" as "classic auth # is configured", skipping the OIDC exchange entirely and failing with a 404 that has # nothing to do with the trusted publisher config. Dropping registry-url instead was tried # and made npm skip OIDC altogether (ENEEDAUTH) — registry-url is what makes npm attempt # OIDC in the first place, so keep it and just remove the empty line it leaves behind. # https://github.com/npm/documentation/issues/1960 run: sed -i '/_authToken/d' "$NPM_CONFIG_USERCONFIG" - run: npm publish