Git Flow 自动化发布

背景

公司使用 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>${gitflow-maven-plugin.version}</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.

一个具体的例子是:

start release: mvn -B -DallowSnapshots=true gitflow:release-start -X
for some internal dependency, drop snapshot: mvn -DprocessParent=true -Dincludes=<groupid>:<artifactid>* versions:use-releases -X
for some internal dependency, update version: mvn -DprocessParent=true -Dincludes=<groupid>:<artifactid>* versions:use-latest-releases -X
commit pom changes: git commit -a --amend --no-edit
build and deploy: mvn -DskipTests clean deploy -U
finish release: mvn -B -DskipTestProject=true gitflow:release-finish -X

Git 插件

除了 Maven 插件外,还有支持 git flow 的 Git 插件。它做的事和上面类似,把多个命令的功能合并为一个命令。(但是,有微小区别,它不会更新 pom 文件里的 version ,只会更新 git tag )

下面简单介绍用法:

使用 git flow release start

$ git flow release start 0.1.0

相当于执行了以下两条语句:切换到 release branch ,打上 version 的 tag 。

git checkout develop
git checkout -b release/0.1.0

使用 git flow release finish

git flow release finish '0.1.0'

相当于执行了以下两条语句:把 release merge 回 master/main branch 。

git checkout main
git merge release/0.1.0

具体参考这里 -> https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow

参考

posted @ 2021-11-01 17:30  MaxStack  阅读(214)  评论(0编辑  收藏  举报