refactor(cli): improve publish-cli script reliability#441
Conversation
- Move version computation and pre-flight checks before build-and-test to fail fast on conflicts (existing branch/tag) instead of wasting minutes on lint/test/build - Add INT/TERM signal handlers to cleanup trap so Ctrl+C during build properly restores working tree state - Update Makefile help text to reflect PR-based workflow
Address code review feedback from gemini-code-assist bot: - Use `git checkout -f` in on-release and committed cleanup stages to ensure reliable branch switching even when files are staged but not committed (e.g., interrupted after `git add` but before `git commit`) - Remove redundant `git checkout -- <file>` in on-release stage since `-f` already discards all local changes This prevents cleanup failures when the script is interrupted between staging and committing.
- Fix ERR trap bypass: remove `if !` wrapper around `gh pr create` so set -e triggers the trap and prints pushed-stage recovery instructions - Fix command injection: all node -e/-p calls now use process.env instead of interpolating shell variables into JS string literals - Rewrite cli/RELEASE.md to document the new PR-based release flow - Rewrite scripts/tests/publish-cli-test.sh with 10 tests covering the new flow (stubs for bun/gh, pre-flight checks, happy path, cleanup state machine stages)
Review findings addressed in efcf0ee
Minor items 5/6/9 (EXIT trap, stderr suppression, trap cleanup inconsistency) are deferred — current code is safe because Tests run: |
|
我重新看了最新 head(
git pull origin main
git tag cli-vX.Y.Z
git push origin cli-vX.Y.Z
|
- Bind release tag to origin/main: PR body, end-of-run hint, and cli/RELEASE.md now use `git tag $TAG origin/main` so the tag is always placed on the merged commit, regardless of local branch state - Reject prerelease tags in version computation: if the latest cli-v* tag contains non-X.Y.Z characters (e.g., -rc.1), exit with a clear message instead of crashing in node parsing - Add pr-scripts.yml workflow: runs publish-cli-test.sh on scripts/** changes so the release script regression suite gates PRs - Add Test 11 covering prerelease tag rejection
|
All three addressed in 7847e0a:
|
|
这里还有一个发布流程的 blocker: 一个实际失败路径是:合并 release PR 后,按文档先执行 建议把版本基线改成只从远端 tag 计算,或者恢复/新增“本地 |
A failed `git push origin cli-vX.Y.Z` after a successful local tag leaves an orphan tag locally. The previous `git tag --list` baseline would then treat it as the latest release, causing skipped versions or publishes based on an unreleased tag. Switch to `git ls-remote --tags --refs origin 'cli-v*' | sort -V` so the baseline reflects only what is actually on origin. Local orphan tags can still collide with the computed target tag, which fails fast with a clear message as before. Adds test 12 covering the orphan-tag scenario.
|
已修复,commit 239cd2e: 把基线计算从 同时移除了之前的 补了 Test 12 覆盖 local-only orphan:origin 上 |
Summary
优化
publish-cli.sh脚本的可靠性和用户体验:INT/TERM信号(Ctrl+C),确保中断时正确清理工作树状态改进细节
1. 预检查前移(fail-fast)
之前:
git pull→bun install / lint / typecheck / test / build(几分钟)→ 计算版本 → 检查 tag/branch 是否存在现在:
git pull→ 计算版本 → 检查 tag/branch 是否存在 →bun install / lint / typecheck / test / build如果
release/cli-vX.Y.Z分支或 tag 已存在(比如上次 PR 已合并但还没打 tag),现在会在几秒内报错退出,而不是白跑几分钟构建。2. 信号处理增强
之前:
trap cleanup_on_error ERR— 只捕获命令失败现在:
trap cleanup_on_error ERR INT TERM— 同时捕获 Ctrl+C 和 kill 信号用户在
bun test或bun build阶段按 Ctrl+C 时,cleanup 函数会:cli/src/generated/pkg-info.tsTest plan
bash -n scripts/publish-cli.sh)release/cli-vX.Y.Z分支,验证脚本在 build 前就报错bun test阶段按 Ctrl+C,验证工作树被正确恢复make publish-cli patch(或在测试环境)相关 PR
这是 #422 的后续优化,基于 code review 反馈改进脚本健壮性。