ci: treat an already-published version as a skip, not a failure

The registry check through npm view did not see the existing version, so publish ran into E409. The registry itself is the authority here: a 409 now reports 'already exists, skipping' and exits zero, while every other error still fails the step.
This commit is contained in:
pyh
2026-09-30 11:31:42 +08:00
parent f4e107782a
commit 3a806f84ab
+19 -16
View File
@@ -4,7 +4,8 @@
# 1. 推送版本标签(推荐):git tag -a v1.0.1 -m "..." && git push origin v1.0.1
# 2. 仓库 → Actions → 选择本工作流 → Run workflow(手动,发布当前 ref 的版本)
#
# 已发布的版本会自动跳过:手动重跑同一个版本不会失败,只是什么都不发。
# 已发布的版本不会让工作流失败:注册表返回 409(version already exists)时这一步记为
# “已存在,本次跳过”,所以手动重跑同一个版本是安全的。
#
# 需要的权限:
# 默认使用 Gitea 内置的任务令牌 ${{ secrets.GITEA_TOKEN }} 发布。
@@ -66,21 +67,7 @@ jobs:
- name: Pack (preview the published contents)
run: npm pack --dry-run
- name: Skip when this version is already published
id: published
run: |
set -eu
spec="${{ steps.version.outputs.name }}@${{ steps.version.outputs.version }}"
# 注册表里查得到就说明这个版本发布过了(Gitea 不允许同版本重复发布)。
if npm view "${spec}" version --registry "${REGISTRY}" > /dev/null 2>&1; then
echo "already=true" >> "${GITHUB_OUTPUT}"
echo "${spec} 已经发布过,本次跳过"
else
echo "already=false" >> "${GITHUB_OUTPUT}"
fi
- name: Publish
if: steps.published.outputs.already == 'false'
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
JOB_TOKEN: ${{ secrets.GITEA_TOKEN }}
@@ -96,4 +83,20 @@ jobs:
npm config set @dsh-plugin:registry "${REGISTRY}"
npm config set "//gitea.iwake.top/api/packages/dsh-plugin/npm/:_authToken" "${token}"
echo "registry = $(npm config get registry)"
npm publish --registry "${REGISTRY}" --access public
# 已发布的版本由注册表自己判定:Gitea 对同版本返回 409(package version already
# exists)。把它当成"本次无需发布"而不是失败,手动重跑因此是安全的;
# 其他错误(E401/E403 等)照旧让这一步失败。
set +e
output="$(npm publish --registry "${REGISTRY}" --access public 2>&1)"
status=$?
set -e
printf '%s\n' "${output}"
if [ "${status}" -eq 0 ]; then
echo "已发布 ${{ steps.version.outputs.name }}@${{ steps.version.outputs.version }}"
elif printf '%s' "${output}" | grep -Eq 'E409|already exists'; then
echo "${{ steps.version.outputs.name }}@${{ steps.version.outputs.version }} 已存在,本次跳过(不算失败)"
else
exit "${status}"
fi