應用情境:希望能依據 Pull Request
標題來自動生成 Release Notes,標題為 semantic-release
的版本號,並將內容至少分類為 feature
和 fix
兩類。
目標
實作:在專案根目錄下,新增 release.config.js
(或 .releaserc
),以 release.config.js
為例:
{
"branches": ["main"],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "angular",
"releaseRules": [
{ "type": "feat", "release": "minor" },
{ "type": "fix", "release": "patch" },
{ "type": "docs", "release": false },
{ "type": "style", "release": false },
{ "type": "refactor", "release": "patch" },
{ "type": "perf", "release": "patch" },
{ "type": "test", "release": false },
{ "type": "*", "release": false } // 所有其他類型,歸到 "Others"
]
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "angular",
"presetConfig": {
"types": [
{ "type": "feat", "section": "Features" },
{ "type": "fix", "section": "Fixes" },
{ "type": "refactor", "section": "Refactors" },
{ "type": "perf", "section": "Performance Improvements" },
{ "type": "docs", "section": "Documentation" },
{ "type": "style", "section": "Code Style Updates" },
{ "type": "test", "section": "Tests" },
{ "type": "*", "section": "Other Changes" }
]
}
}
],
"@semantic-release/changelog",
"@semantic-release/git",
[
"@semantic-release/github",
{
"release": {
"name": "Release version ${nextRelease.version}"
}
}
]
]
}
說明
${nextRelease.version}
。注意事項
release.config.js
(或 .releaserc
):應該放在專案的根目錄下,當 GitHub Actions
觸發並執行 semantic-release
時,會自動在專案根目錄下尋找 release.config.js
或 .releaserc
文件來進行版本號處理與生成 Release Notes。透過使用 semantic-release 和 GitHub Actions,自動化發布版本號、生成 Release Notes 以及提交 CHANGELOG.md 能大大提升開發流程效率,確保程式碼變更後可被清楚記錄並發布到 GitHub。此外,還可以根據專案需求客製化 Release Notes 的規則,靈活定義變更類別及內容格式。