Skip to content

Commit 9a73b9b

Browse files
committed
ci: 移除 registry-url —— 它会让 OIDC 静默失效
publish 步骤真实失败,根因是 setup-node 的 registry-url: 它会在 ~/.npmrc 写入 //registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN} 未提供 token 时该占位符展开为空字符串,但 npm 只要看到 _authToken 这一行存在,就认为「认证已配置」,从而跳过 OIDC 交换 —— trusted publishing 因此静默失效(表现为 ENEEDAUTH / 404 Not Found - PUT)。 registry.npmjs.org 本就是 npm 默认源,这个参数没有收益,只有风险。 改动: - setup-node 去掉 registry-url,只保留 node-version 与 cache - 新增步骤清洗 .npmrc 中的 _authToken 行,强制走 OIDC 路径 - npm publish 显式带 --registry=https://registry.npmjs.org - 仓库 .npmrc 去掉 registry 声明,并注明原因(避免后人加回 _authToken) - 发布失败时把 npm 输出写到 Step Summary,便于无日志权限时排查 参考:actions/setup-node#1551
1 parent e057935 commit 9a73b9b

2 files changed

Lines changed: 48 additions & 15 deletions

File tree

‎.github/workflows/release.yml‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,33 @@ jobs:
4545
- name: Checkout
4646
uses: actions/[email protected]
4747

48-
# 注意:使用 OIDC 时不要配置任何 registry auth token。
49-
# 这里只设置 registry 地址;发布时 npm 会自动用 GitHub 的 OIDC token 换取短期凭据。
48+
# 关键:这里**不能**设 registry-url。
5049
#
51-
# 用 Node 24:它自带 npm 11.x,满足 trusted publishing 的 npm >= 11.5.1 要求。
52-
# 之前用 Node 22.14.0 + `npm install -g npm@latest`,那一步在 runner 上
53-
# 会直接失败(权限/路径问题),导致后续全部跳过。直接用自带新版 npm 的
54-
# Node 更稳,也少一个失败点。
50+
# setup-node 的 registry-url 会在 ~/.npmrc 写一行
51+
# //registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}
52+
# 未提供 token 时它展开成空字符串,但 npm 只要看到 _authToken 这一行存在,
53+
# 就认为「认证已配置」,于是跳过 OIDC 交换,导致 trusted publishing 静默失效
54+
# (表现为 ENEEDAUTH 或 404 Not Found - PUT)。
55+
# registry.npmjs.org 本就是默认源,所以这个参数没有收益,只有风险。
5556
- name: Setup Node
5657
uses: actions/[email protected]
5758
with:
5859
node-version: '24'
59-
registry-url: 'https://registry.npmjs.org'
6060
cache: 'npm'
6161

62+
# 兜底:清掉可能存在的 _authToken 行,确保 npm 会走 OIDC 交换
63+
- name: Strip auth token from npmrc (force OIDC path)
64+
run: |
65+
for f in "$HOME/.npmrc" "$(pwd)/.npmrc"; do
66+
if [ -f "$f" ] && grep -q '_authToken' "$f"; then
67+
echo "清理 $f 中的 _authToken 行"
68+
grep -v '_authToken' "$f" > "$f.tmp" || true
69+
mv "$f.tmp" "$f"
70+
fi
71+
done
72+
echo "--- 当前 .npmrc(预期无 _authToken)---"
73+
cat "$HOME/.npmrc" 2>/dev/null || echo "(empty)"
74+
6275
# 兜底校验:npm 版本必须 >= 11.5.1,否则 OIDC 发布不可用,提前失败给出清晰原因
6376
- name: Verify npm supports trusted publishing
6477
run: |
@@ -120,14 +133,30 @@ jobs:
120133
# 后者确保首次发布会创建公开包。明确写出来,避免依赖隐式行为。
121134
- name: Publish to npmjs (trusted publishing)
122135
run: |
136+
set -o pipefail
123137
echo "=== 发布前诊断 ==="
124138
echo "npm: $(npm --version) / node: $(node --version)"
125139
echo "package: $(node -p "require('./package.json').name + '@' + require('./package.json').version")"
126-
echo "registry: $(npm config get registry)"
127-
echo "NODE_AUTH_TOKEN set: ${{ secrets.NPM_TOKEN != '' }}"
128-
echo "ACTIONS_ID_TOKEN_REQUEST_URL present: ${{ env.ACTIONS_ID_TOKEN_REQUEST_URL != '' }}"
129-
echo "=================="
130-
npm publish --verbose --access public
140+
echo "--- npmrc(预期无 _authToken)---"
141+
cat "$HOME/.npmrc" 2>/dev/null || echo "(none)"
142+
echo "=================================="
143+
# 显式指定 registry:因为没有设 setup-node 的 registry-url,
144+
# 这里确保发布目标就是 npmjs 官方源。
145+
# --provenance 会随 OIDC 自动附加来源证明。
146+
set +e
147+
npm publish --registry=https://registry.npmjs.org --access public 2>&1 | tee /tmp/npm-publish.log
148+
STATUS=$?
149+
set -e
150+
if [ "$STATUS" -ne 0 ]; then
151+
{
152+
echo "### npm publish 失败(退出码 $STATUS)"
153+
echo ""
154+
echo '```'
155+
tail -40 /tmp/npm-publish.log
156+
echo '```'
157+
} >> "$GITHUB_STEP_SUMMARY"
158+
exit "$STATUS"
159+
fi
131160
132161
# 发布后校验:确认 registry 上真的存在这个版本,避免「命令成功但没发出去」
133162
- name: Verify published to registry

‎.npmrc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# 公共包默认走 npmjs 官方源。
2-
# 若国内网络较慢,可换成镜像(但发布必须用官方源):
1+
# 本仓库不使用长期 token:发布走 GitHub Actions 的 Trusted Publishing (OIDC)。
2+
#
3+
# 注意:不要在 .npmrc 里写 _authToken。npm 只要看到该行(哪怕展开为空字符串)
4+
# 就会认为认证已配置,从而跳过 OIDC 交换,导致 trusted publishing 失效。
5+
#
6+
# registry 使用默认值 https://registry.npmjs.org(npm 内置),无需显式声明。
7+
# 如需国内镜像(仅本地开发加速,发布必须用官方源):
38
# registry=https://registry.npmmirror.com
4-
registry=https://registry.npmjs.org/

0 commit comments

Comments
 (0)