本文档详细说明 Commit 规范校验和自动发布流水线的配置方法。
一、Commit 规范校验
1. 校验脚本
// scripts/verify-commit.js
const fs = require('fs')
const path = require('path')
const msgPath = path.resolve('.git/COMMIT_EDITMSG')
const msg = fs.readFileSync(msgPath, 'utf-8').trim()
// 格式:type(scope): subject
const commitRE = /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip)(\(.+\))?: .{1,50}/
if (!commitRE.test(msg)) {
console.error('invalid commit message format.')
console.error('Proper format: type(scope): subject')
console.error('Example: feat(compiler): add template optimization')
process.exit(1)
}
2. Husky Hook 配置
# 安装 husky
npm install -D husky
npx husky install
# 添加 commit-msg hook
npx husky add .husky/commit-msg 'node scripts/verify-commit.js'
3. Commit 格式说明
| type | 说明 | 出现在 CHANGELOG |
|---|---|---|
| feat | 新功能 | 是 |
| fix | 修复 bug | 是 |
| perf | 性能优化 | 是 |
| docs | 文档变更 | 否 |
| style | 代码格式 | 否 |
| refactor | 重构 | 否 |
| test | 测试 | 否 |
| chore | 构建/工具 | 否 |
示例:
feat(compiler): add template optimization
fix(v-model): handle events on blur
docs: update README
二、版本更新脚本
1. release.js 核心流程
// scripts/release.js
const { prompt } = require('enquirer')
const semver = require('semver')
const { execSync } = require('child_process')
async function main() {
const currentVersion = require('./package.json').version
const versionIncrements = ['patch', 'minor', 'major']
// 1. 选择版本
const { release } = await prompt({
type: 'select',
name: 'release',
message: 'Select release type',
choices: versionIncrements.map(i => `${i} (${semver.inc(currentVersion, i)})`)
})
const targetVersion = semver.inc(currentVersion, release)
// 2. 更新版本号
updatePackageVersions(targetVersion)
// 3. 生成 CHANGELOG
execSync('pnpm run changelog', { stdio: 'inherit' })
// 4. Git 提交
execSync(`git add -A && git commit -m "release: v${targetVersion}"`, { stdio: 'inherit' })
// 5. 打标签
execSync(`git tag v${targetVersion}`, { stdio: 'inherit' })
// 6. 推送(触发 CI)
execSync('git push && git push --tags', { stdio: 'inherit' })
}
function updatePackageVersions(version) {
// 更新所有 packages/*/package.json 中的版本号
// 更新相互引用包的版本依赖
}
main()
2. package.json 配置
{
"scripts": {
"release": "node scripts/release.js",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s"
}
}
三、GitHub Actions 流水线
1. CI 工作流
# .github/workflows/ci.yml
name: CI
on:
push:
branches: ['**']
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v4
with:
node-version-file: '.node-version'
cache: 'pnpm'
- run: pnpm install
- run: pnpm build
- run: pnpm test
2. Release 工作流
# .github/workflows/release.yml
name: Release
on:
push:
tags:
- 'v*' # 打标签自动触发
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v4
with:
node-version-file: '.node-version'
cache: 'pnpm'
- run: pnpm install
- run: pnpm build
- run: pnpm test
release:
needs: [test]
runs-on: ubuntu-latest
environment: Release
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v4
with:
node-version-file: '.node-version'
registry-url: 'https://registry.npmjs.org'
cache: 'pnpm'
- run: pnpm install
- run: pnpm build --withTypes
- run: pnpm release --publishOnly
- name: Create GitHub Release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: v${{ github.ref_name }}
body: |
See [CHANGELOG.md](https://github.com/${{ github.repository }}/blob/main/CHANGELOG.md) for details.
四、完整流程
开发者 本地 GitHub
│ │ │
│ git commit -m "feat(xxx): add new feature" │
│ ──────────────────────> │ │
│ pre-commit hook 校验 │
│ (verify-commit.js) │
│ ✓ │
│ git push │
│ ──────────────────────────────────────────────> │
│ CI workflow 触发 │
│ (test → build) │
│ ✓ │
│ pnpm release │
│ 1. 选择版本 │
│ 2. 更新版本号 │
│ 3. 生成 CHANGELOG │
│ 4. git commit │
│ 5. git tag v1.2.0 │
│ 6. git push --tags │
│ ──────────────────────────────────────────────> │
│ Release workflow 触发 │
│ (test → publish → release)│
│ ✓ │
│ GitHub Release 创建 │
五、快速上手
# 1. 安装依赖
pnpm add -D husky conventional-changelog enquirer semver
# 2. 初始化 husky
npx husky install
# 3. 添加 commit 校验 hook
npx husky add .husky/commit-msg 'node scripts/verify-commit.js'
# 4. 创建 release 脚本
# 参考上方 scripts/release.js
# 5. 发布
pnpm release
六、语义化版本规则
| 提交类型 | 版本变化 | 示例 |
|---|---|---|
| feat | minor +1 | 1.2.0 → 1.3.0 |
| fix | patch +1 | 1.2.0 → 1.2.1 |
| feat (breaking) | major +1 | 1.2.0 → 2.0.0 |
| 手动指定 | 自定义 | 1.2.0 → 2.0.0 |
BREAKING CHANGE 在 commit footer 中声明:
fix(api): remove deprecated endpoint
BREAKING CHANGE: The /old/endpoint has been removed.