fix(ci): keep the npm publish token out of install and build steps (#4690)

## Description

In `.github/workflows/release.yml`, job `publish-npm`, each package's
publish step set `NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}` for the
whole step, and the same step ran `npm ci` and `npm run build` before
`npm publish`. `npm ci` runs the install lifecycle scripts of every
dependency, so those scripts could read the publish token. This PR
splits each of the three packages (SDK, openclaw, opencode) into a
Prepare step with no token and a Publish step that carries the token and
runs only `npm publish`.

Fixes #4688

## Type of Change

- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)

## Changes Made

- `release.yml`: new `Prepare ... for npmjs.org` step per package (no
`env`): the "already published" `npm view` check, `cd`, `npm ci`, `npm
run build`, `npm version`, and for openclaw/opencode the dependency
rewrite and (openclaw) `node prepare-dist.mjs`. Same commands, same
order as before.
- `release.yml`: the existing `Publish ... to npmjs.org` step (same name
and id) now has `working-directory`, keeps `NODE_AUTH_TOKEN`, and runs
only `npm publish --access public`.
- The skip for an already-published version is passed between the two
steps with a step output (`skip=true` in `$GITHUB_OUTPUT`), and each
Publish step has `if: steps.<id>-prepare.outputs.skip != 'true'`.
Re-running after a partial release behaves as before.
- `tests/test_release_workflows.py`: new
`test_publish_npm_token_is_only_in_whoami_and_publish_steps`; the two
existing order tests now start at the Prepare step, where `npm version`
and the dependency rewrite now live.
- No `package.json` of the three packages has a `prepublishOnly`,
`prepack` or `prepare` script, so nothing needed at publish time was
moved out of the token step. I did not add `--ignore-scripts` to `npm
publish`.

## Testing

- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed

### Test Output

Fail before (new test only, on the unmodified workflow):

```text
FAILED tests/test_release_workflows.py::test_publish_npm_token_is_only_in_whoami_and_publish_steps
E   assert ('npm ci' not in 'version="${...ess public\n'
E     'npm ci' is contained here:
E       npm ci
E       npm run build
E       npm version "$version" --no-git-tag-version --allow-same-version
E       npm publish --access public)
1 failed, 4 passed, 54 deselected
```

Pass after (whole file):

```text
tests/test_release_workflows.py .................ss..................... [ 67%]
...................                                                      [100%]
57 passed, 2 skipped in 277.72s (0:04:37)
```

Lint (touched test file): `ruff check` "All checks passed!", `ruff
format --check` "1 file already formatted". `mypy` is not relevant (no
`headroom` code changed).

Step-by-step env keys of the `publish-npm` job, parsed with PyYAML,
before:

```text
 2 Verify npm publishing authentication       env=['NODE_AUTH_TOKEN'] cmds=['npm whoami']
 3 Publish ... (TypeScript SDK) to npmjs.org  env=['NODE_AUTH_TOKEN'] cmds=['npm ci', 'npm run build', 'npm publish']
 4 Publish ... openclaw to npmjs.org          env=['NODE_AUTH_TOKEN'] cmds=['npm ci', 'npm run build', 'npm publish']
 5 Publish ... opencode to npmjs.org          env=['NODE_AUTH_TOKEN'] cmds=['npm ci', 'npm run build', 'npm publish']
```

after:

```text
 2 Verify npm publishing authentication       env=['NODE_AUTH_TOKEN'] cmds=['npm whoami']
 3 Prepare ... (TypeScript SDK)               env=[] cmds=['npm ci', 'npm run build']
 4 Publish ... (TypeScript SDK)               env=['NODE_AUTH_TOKEN'] wd=sdk/typescript cmds=['npm publish']
 5 Prepare ... openclaw                       env=[] cmds=['npm ci', 'npm run build']
 6 Publish ... openclaw                       env=['NODE_AUTH_TOKEN'] wd=plugins/openclaw cmds=['npm publish']
 7 Prepare ... opencode                       env=[] cmds=['npm ci', 'npm run build']
 8 Publish ... opencode                       env=['NODE_AUTH_TOKEN'] wd=plugins/opencode cmds=['npm publish']
```

## Real Behavior Proof

- Environment: Windows 11 host, workflow parsed in WSL Ubuntu with
PyYAML and pytest from the repo's test venv, against a clone at
`12864fc8` plus this change.
- Exact command / steps: parsed `.github/workflows/release.yml` with
`yaml.safe_load` and printed every `publish-npm` step's env keys,
working directory, `if` and npm commands before and after (output
above); ran `pytest tests/test_release_workflows.py`.
- Observed result: before, all three install+build+publish steps carry
`NODE_AUTH_TOKEN`; after, the token is only on `npm whoami` and the
three `npm publish` steps, and `npm ci`/`npm run build` steps have no
env. The whole workflow test file passes.
- Not tested: an actual GitHub Actions run (the workflow only publishes
on a release and needs `NPM_TOKEN`; I did not run it or publish
anything). `actionlint` is not installed here, so
`scripts/validate-workflows.sh` was not run. I did not check that
setup-node's `.npmrc` placeholder `NODE_AUTH_TOKEN` lets the token-less
`npm view` and `npm ci` run as before; the `npm view` of a public
package should not need auth, but a maintainer should confirm on the
first release.

## Runtime Rollout Safety

- Rollout-managed feature(s): none (release workflow only).
- Minimum rollout channel: n/a.
- Stable/default behavior changed: No product behavior. The release
workflow publishes the same packages with the same commands; only the
step boundaries and which steps see the token changed.
- Kill switch / disable path: n/a; the existing `publish-npm` gating of
the workflow is unchanged.
- Unsafe override required: No.
- Qualification impact: None.
- Rollback path: revert this commit.

## Review Readiness

- [x] I have performed a self-review
- [x] This PR is ready for human review

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)

## Additional Notes

- Not changed, left for a maintainer: npm trusted publishing (OIDC,
`--provenance`) as suggested in the issue, which would remove the stored
`NPM_TOKEN`; and the same split in the `publish-github-packages` job,
which has the same shape with `GITHUB_TOKEN`.
- Documentation checkbox is N/A (no user-facing docs describe the
workflow steps).

Produced with AI assistance (Claude Code) operated by me (HetCreep); the
tests and proof above were run on my machine against a clone of the
repository.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- devin-review-badge-begin -->

---

<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4690"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HetCreep
2026-11-07 20:38:05 -06:00
committed by GitHub
co-authored by Claude Opus 5.5
parent adc6724966
commit 8f6366cf66
2 changed files with 54 additions and 13 deletions
+34 -10
View File
@@ -827,30 +827,39 @@ jobs:
# "Artifact not found" because publish-npm is not gated on
# collect-dist. Removing the dead step is the right fix.
- name: Publish ${{ env.NPM_SDK_PACKAGE }} (TypeScript SDK) to npmjs.org
id: npm-sdk-publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# Install and build run in their own steps WITHOUT the publish token:
# `npm ci` executes dependency lifecycle scripts, which must not be able
# to read NPM_TOKEN. Only the `npm publish` steps below carry it.
- name: Prepare ${{ env.NPM_SDK_PACKAGE }} (TypeScript SDK) for npmjs.org
id: npm-sdk-prepare
run: |
version="${{ needs.detect-version.outputs.npm_version }}"
if npm view "${NPM_SDK_PACKAGE}@${version}" version --registry "${NPM_REGISTRY_URL}" >/dev/null 2>&1; then
echo "${NPM_SDK_PACKAGE}@${version} is already published; continuing."
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi
cd sdk/typescript
npm ci
npm run build
npm version "$version" --no-git-tag-version --allow-same-version
npm publish --access public
- name: Publish ${{ env.NPM_OPENCLAW_PACKAGE }} to npmjs.org
id: npm-openclaw-publish
- name: Publish ${{ env.NPM_SDK_PACKAGE }} (TypeScript SDK) to npmjs.org
id: npm-sdk-publish
if: steps.npm-sdk-prepare.outputs.skip != 'true'
working-directory: sdk/typescript
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm publish --access public
- name: Prepare ${{ env.NPM_OPENCLAW_PACKAGE }} for npmjs.org
id: npm-openclaw-prepare
run: |
version="${{ needs.detect-version.outputs.npm_version }}"
if npm view "${NPM_OPENCLAW_PACKAGE}@${version}" version --registry "${NPM_REGISTRY_URL}" >/dev/null 2>&1; then
echo "${NPM_OPENCLAW_PACKAGE}@${version} is already published; continuing."
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi
cd plugins/openclaw
@@ -865,16 +874,23 @@ jobs:
fs.writeFileSync("package.json", `${JSON.stringify(pkg, null, 2)}\n`);
EOF
node prepare-dist.mjs
npm publish --access public
- name: Publish ${{ env.NPM_OPENCODE_PACKAGE }} to npmjs.org
id: npm-opencode-publish
- name: Publish ${{ env.NPM_OPENCLAW_PACKAGE }} to npmjs.org
id: npm-openclaw-publish
if: steps.npm-openclaw-prepare.outputs.skip != 'true'
working-directory: plugins/openclaw
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm publish --access public
- name: Prepare ${{ env.NPM_OPENCODE_PACKAGE }} for npmjs.org
id: npm-opencode-prepare
run: |
version="${{ needs.detect-version.outputs.npm_version }}"
if npm view "${NPM_OPENCODE_PACKAGE}@${version}" version --registry "${NPM_REGISTRY_URL}" >/dev/null 2>&1; then
echo "${NPM_OPENCODE_PACKAGE}@${version} is already published; continuing."
echo "skip=true" >> "$GITHUB_OUTPUT"
exit 0
fi
cd plugins/opencode
@@ -888,6 +904,14 @@ jobs:
pkg.dependencies["headroom-ai"] = `^${process.env.HEADROOM_NPM_VERSION}`;
fs.writeFileSync("package.json", `${JSON.stringify(pkg, null, 2)}\n`);
EOF
- name: Publish ${{ env.NPM_OPENCODE_PACKAGE }} to npmjs.org
id: npm-opencode-publish
if: steps.npm-opencode-prepare.outputs.skip != 'true'
working-directory: plugins/opencode
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
npm publish --access public
publish-github-packages:
+20 -3
View File
@@ -1114,8 +1114,8 @@ def test_publish_npm_regenerates_openclaw_dist_metadata_after_version_and_depend
"""The direct npm publish path must not ship stale OpenClaw dist metadata."""
content = (ROOT / ".github" / "workflows" / "release.yml").read_text(encoding="utf-8")
start = content.index("name: Publish ${{ env.NPM_OPENCLAW_PACKAGE }} to npmjs.org")
end = content.index("name: Publish ${{ env.NPM_OPENCODE_PACKAGE }}", start)
start = content.index("name: Prepare ${{ env.NPM_OPENCLAW_PACKAGE }} for npmjs.org")
end = content.index("name: Prepare ${{ env.NPM_OPENCODE_PACKAGE }}", start)
block = content[start:end]
version = block.index('npm version "$version"')
@@ -1130,7 +1130,7 @@ def test_publish_npm_rewrites_opencode_dependency_after_version_and_before_publi
"""The direct npm publish path must version and retarget the Opencode dependency."""
content = (ROOT / ".github" / "workflows" / "release.yml").read_text(encoding="utf-8")
start = content.index("name: Publish ${{ env.NPM_OPENCODE_PACKAGE }} to npmjs.org")
start = content.index("name: Prepare ${{ env.NPM_OPENCODE_PACKAGE }} for npmjs.org")
end = content.index("\n publish-github-packages:", start)
block = content[start:end]
@@ -1174,6 +1174,23 @@ def test_publish_npm_is_rerunnable_after_a_partial_release() -> None:
assert lookup_index < publish_index
def test_publish_npm_token_is_only_in_whoami_and_publish_steps() -> None:
"""Install and build scripts must never see the npm publish token."""
workflow = yaml.safe_load((ROOT / ".github" / "workflows" / "release.yml").read_text())
job = workflow["jobs"]["publish-npm"]
assert "NODE_AUTH_TOKEN" not in job.get("env", {})
with_token = [step for step in job["steps"] if "NODE_AUTH_TOKEN" in step.get("env", {})]
assert len(with_token) == 4 # whoami + one publish per package
for step in with_token:
run = step["run"]
assert "npm whoami" in run or run.strip().endswith("npm publish --access public"), step
assert "npm ci" not in run and "npm run build" not in run, step
for step in job["steps"]:
if "npm ci" in step.get("run", ""):
assert "env" not in step or "NODE_AUTH_TOKEN" not in step["env"], step
def test_sdist_license_is_packaged_and_verified_before_upload() -> None:
"""STRUCTURAL INVARIANT: the sdist tarball must physically contain
every license file PEP 639 declares in PKG-INFO, and the release