Git flow 自動化釋出
阿新 • • 發佈:2021-11-01
背景
公司使用 Git
做版本管理,使用 Jenkins
做自動化 Build 。但是在釋出的時候,還是有很多人工的活。
步驟如下:
- 在 Jenkins 上執行
release job
: build develop branch 。 - 手動填寫
release version
。 - (說明1)在背後,Jenkins 會做兩個 commit ,一個是更新 pom 檔案裡的 version 為指定的版本,把 SNAPSHOT 去掉,打一個 tag ,並把這個上傳到 Nexus 上。
- (說明2)然後,另一個 commit 是更新 pom 檔案裡的 version number +1 ,在把 SNAPSHOT 加回去,作為下一個開發週期的分支。
- 在釋出成功後,手動將 develop 分支 merge 到 master 分支。
一次釋出涉及十幾個專案,每個手動 merge 一遍,還是不小的工作量。
改進
使用 Maven gitflow
plugin ,可以實現自動化的釋出。
前提
使用這個 plugin 的前提,是遵循 git flow 的基本使用規則。
簡單概括如下:
origin/master
存放的是【生產環境】最新的程式碼。origin/develop
存放的是【開發環境】最新的程式碼。Feature
branches 從 develop 中來,到 develop 中去。Hotfix
branches 從 master 中來,到 master 和 develop 中去,命名以hotfix-
Release
branches 從 develop 中來,到 develop 和 master 中去,命名以release-
開頭。
詳情參考 -> https://nvie.com/posts/a-successful-git-branching-model/
實現
Project source: https://github.com/aleksandr-m/gitflow-maven-plugin
Maven依賴注入:
<build> <plugins> <plugin> <groupId>com.amashchenko.maven.plugin</groupId> <artifactId>gitflow-maven-plugin</artifactId> <version>1.16.0</version> <configuration> <!-- optional configuration --> <gitFlowConfig> <productionBranch>master</productionBranch> <developmentBranch>develop</developmentBranch> <releaseBranchPrefix>release-</releaseBranchPrefix> <versionTagPrefix></versionTagPrefix> <origin>origin</origin> </gitFlowConfig> </configuration> </plugin> </plugins> </build>
在 Jenkins 端使用 Maven 命令如下:
gitflow:release-start - Starts a release branch and updates version(s) to release version.
gitflow:release-finish - Merges a release branch and updates version(s) to next development version.
gitflow:feature-start - Starts a feature branch and optionally updates version(s).
gitflow:feature-finish - Merges a feature branch.
gitflow:hotfix-start - Starts a hotfix branch and updates version(s) to hotfix version.
gitflow:hotfix-finish - Merges a hotfix branch.
參考
- Git-Flow Maven Plugin https://aleksandr-m.github.io/gitflow-maven-plugin/plugin-info.html