发布更改(Promote Changes)
从当前工作目录状态运行完整的发布工作流,最终在 main 上创建一个带有标签的 GitHub Release。
当前分支: !git branch --show-current 2>/dev/null || echo "unknown"
Git 状态: !git status --short 2>/dev/null || echo "not a git repo"
最近标签: !git describe --tags --abbrev=0 2>/dev/null || echo "none"
自最近标签以来的最近提交:
!`git log $(git describe --tags --abbrev=0 2>/dev/null || echo "")..HEAD --oneline -10 2>/dev/null || git log --oneline -10`
下一个版本(根据上面的提交计算):
!`(last=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0"); IFS='.' read -r ma mi pa <<< "${last#v}"; log=$(git log "${last}..HEAD" --format="%s" 2>/dev/null); if echo "$log" | grep -qE "^[a-z]+(\([^)]+\))?!:"; then echo "v$((ma+1)).0.0"; elif echo "$log" | grep -qE "^feat"; then echo "v${ma}.$((mi+1)).0"; else echo "v${ma}.${mi}.$((pa+1))"; fi)`
步骤 0:前置检查
gh auth status 2>&1 || { echo "错误:gh 未认证。请运行:gh auth login"; exit 1; }
检查可能被意外暂存的未跟踪 .env 文件:
git status --short | grep -E '^\?\? .*\.env' && echo "警告:检测到未跟踪的 .env 文件 — 暂存前请检查"
cat .gitignore 2>/dev/null | grep -q '\.env' || echo "警告:.gitignore 未排除 .env 文件"
如果任何 .env 文件会被暂存:
if [ -t 0 ]; then
# 交互式 — 询问确认后再继续
read -p "警告:检测到 .env 文件。是否继续?(y/N) " confirm
[[ "$confirm" =~ ^[Yy]$ ]] || { echo "已中止。"; exit 1; }
else
# 非交互式 — 硬性中止;未经人工检查风险太高
echo "错误:在非交互模式下检测到未跟踪的 .env 文件。正在中止。"
exit 1
fi
步骤 1:提交所有未暂存的更改
如果上面的 Git 状态显示有未暂存或未跟踪的更改:
- 查看差异:
git diff和git diff --cached - 选择性暂存更改 — 优先暂存已跟踪的文件:
git add -u,然后检查并逐一添加任何 有意的新文件。除非用户明确确认,否则避免使用git add -A。 - 根据更改草拟一个约定式提交信息 — 以类型前缀开头
(
feat:、fix:、docs:、chore:等)后跟简洁摘要 - 使用 heredoc 提交,以便多行信息格式正确
如果没有未提交的内容,则跳至步骤 2。
步骤 2:推送
将当前分支推送到远程:
git push -u origin HEAD
步骤 3:合并到 main(仅限特性分支)
如果已在 main(或仓库的默认分支)上,则跳过此步骤。
如果位于特性分支上:
-
扫描从最近标签以来的提交,查找 Issue 引用:
git log $(git describe --tags --abbrev=0 2>/dev/null || echo "")..HEAD --format="%B"查找
Closes #N、Fixes #N、Resolves #N(不区分大小写)。收集所有找到的 Issue 编号。 如果未找到:if [ -t 0 ]; then # 交互式 — 询问用户 read -p "此更改解决了哪些 GitHub Issues?(例如 12 15 — 或按回车跳过) " issues else # 非交互式 — 静默跳过 issues="" fi -
创建一个目标为 main 的 PR,并在正文中包含关闭关键词,以便 GitHub 在合并时自动关闭 Issue:
gh pr create --title "<type>: <summary>" --body "$(cat <<'EOF' ## 摘要 <来自提交的要点> Closes #N, Closes #N 🤖 由 [claude-workflow-skills:promote](https://github.com/ali5ter/claude-workflow-skills) 代表 [Alister](https://github.com/ali5ter) 生成 EOF )"如果未识别到任何 Issue,则省略
Closes行。 -
启用自动合并(推荐 squash 合并):
gh pr merge --auto --squash -
轮询直至合并:
gh pr view --json state --jq '.state' -
切换到 main 并拉取:
git checkout main && git pull
步骤 4:确认下一个版本
下一个版本已在上方的上下文中根据以下语义版本规则预先计算:
- 提交主题包含
!:(例如feat!:``、fix!:``) → major 版本提升 - 提交主题以
feat开头 → minor 版本提升 - 所有其他提交 → patch 版本提升
根据提交列表确认计算出的版本正确。如果不存在之前的标签,则使用 v1.0.0。仅在计算版本明显错误时(例如注入命令返回了错误或空输出)才进行覆盖。
步骤 5:同步插件清单(如果存在)
如果 .claude-plugin/plugin.json 存在,将其 version 字段更新为 <next-version>(不带 v 前缀 — 插件清单使用裸的语义版本号,如 1.2.3):
node -e "
const fs = require('fs');
const f = '.claude-plugin/plugin.json';
const d = JSON.parse(fs.readFileSync(f, 'utf8'));
d.version = '<next-version>';
fs.writeFileSync(f, JSON.stringify(d, null, 2) + '\n');
"
然后提交并推送:
git add .claude-plugin/plugin.json
git commit -m "chore: sync plugin.json version to v<next-version>"
git push
如果 .claude-plugin/plugin.json 不存在,则完全跳过此步骤。
步骤 6:标签和发布
git tag -a v<next-version> -m "Release v<next-version>"
git push origin v<next-version>
gh release create v<next-version> \
--generate-notes \
--title "v<next-version>"
步骤 7:关闭已解决的 Issue(仅限 main 分支)
如果在步骤 3 中创建了 PR,则跳过此步骤 — GitHub 会通过 PR 正文中的 Closes #N 关键词在 PR 合并时自动关闭 Issue。
如果是在 main 上直接进行发布(没有 PR),则立即关闭任何识别到的 Issue:
gh issue close <N> --comment "已在 $(gh release view v<next-version> --json url --jq '.url') 中解决"
步骤 8:清理已合并的特性分支
如果在步骤 3 中合并了特性分支,请同时在本地和远程删除它:
git branch -d <feature-branch>
git push origin --delete <feature-branch>
步骤 9:确认干净状态
git status
git log --oneline -5
输出来自 gh release view v<next-version> --json url --jq '.url' 的 GitHub Release URL。