使用Maven(SemVer GitFlow Maven)自动化语义版本控制

您是否正在使用语义方法进行版本控制?您在使用gitflow吗?您最可能熟悉调整版本,创建分支,从master / dev进行合并,重新调整版本,处理合并冲突等过程。



在本文中,我将简要说明我们实质上用于库的发布过程以及如何使其自动化。我们通常使用带有内部编号的CI / CD方法,但是对于我们的库,我们选择使用语义版本控制。我遇到了多家公司所伴随的繁琐的发布过程,现在终于找到了解决方案。



本文是关于Maven的,但是Gradle也有很多替代品



可以在我们的GitHub页面上找到一个示例项目



语义版本控制和Git



语义版本控制是您发布的分类系统。我确定您已经看过1.6.4、1.7.10、1.12.2等版本号。这些数字代表MAJOR.MINOR.PATCH(MAJOR.MINOR.PATCH)



另外,还有一些SNAPSHOT版本看起来相同,但最后添加了“ -SNAPSHOT”,例如1.14.4-SNAPSHOT。



典型的发布过程包括以下步骤:



  1. 从开发分支(以下称为开发分支)创建发行分支。
  2. 在发行分支中将所有pom.xml文件中的版本从SNAPSHOT(1.2.3-SNAPSHOT)更改为非SNAPSHOT(1.2.3)。
  3. 在开发分支(1.2.4-SNAPSHOT)上升级SNAPSHOT版本。
  4. 完成发布的所有工作后,将release分支合并到master分支中。这是当前版本。
  5. 合并主分支或发布分支回到开发分支。


/ , : , , .



, , . , merge .



?



  • , .
  • master SNAPSHOT.
  • CI, .
  • merge .
  • hotfixes ( master ).


gitflow-maven



, maven, pom.xml. . , .



, , . , . , : gitflow-maven-plugin.



, :



  • .
  • release .
  • hotfix.
  • (Merging) .


, . , CI/CD, , .



, (goals) maven. , .



:



, .



$ mvn gitflow:release-start -B


release (-B Batch Mode)



$ mvn gitflow:release


. master .



, , .



$ mvn gitflow:hotfix-start -B


$ mvn gitflow:hotfix-finish -B -DhotfixVersion=1.8.9b 


hotfix master , 1.8.9b . . - , .





, poms:



<build>
    <plugins>
        <plugin>
            <groupId>com.amashchenko.maven.plugin</groupId>
            <artifactId>gitflow-maven-plugin</artifactId>
            <version>1.13.0</version>
            <configuration>
                <!-- optional configuration -->
            </configuration>
        </plugin>
    </plugins>
</build>


GitHub maven central.



GitHub. :



<configuration>
    <!-- We use maven wrapper in all our projects instead of a local maven installation -->
    <mvnExecutable>./mvnw</mvnExecutable>

    <!-- Don’t push to the git remote. Very useful for testing locally -->
    <pushRemote>true</pushRemote>

    <!-- Set to true to immediately bump the development version when creating a release branch -->
    <commitDevelopmentVersionAtStart>false</commitDevelopmentVersionAtStart>

    <!-- Which digit to increas in major.minor.patch versioning, the values being 0.1.2 respectively.
         By default the rightmost number is increased.
         Pass in the number via parameter or profile to allow configuration,
         since everything set in the file can't be overwritten via command line -->
    <versionDigitToIncrement>${gitflowDigitToIncrement}</versionDigitToIncrement>

    <!-- Execute mvn verify before release -->
    <preReleaseGoals>verify</preReleaseGoals>
    <preHotfixGoals>verify</preHotfixGoals>

    <!-- Configure branches -->
    <gitFlowConfig>
        <productionBranch>master</productionBranch>
        <!-- default is develop, but we use development -->
        <developmentBranch>development</developmentBranch>
    </gitFlowConfig>
</configuration>


, , . , Gitlab CI.



Gitlab CI



CI/CD Gitlab CI , commit snapshot, merge master — release.



, — , master merge , hotfixes.



Gitlab CI, . :





release. , release, release, snapshot, (merge) master snapshot. - .



git Gitlab CI



git Gitlab CI, : , CI git.



write_repository. , , .



GITLAB_TOKEN, protected, development, release/* hotfix/* (protected). , .



git remote runner, CI . , Gitlab, :



$ git remote set-url --push origin "https://oauth2:${GITLAB_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"


git . Git «», git. , git , . :



$ git config user.name "Gitlab CI"
$ git config user.email gitlab-ci@viesure.io


git, CI. .





, git CI, gitflow. .



, , :



· MINOR



· PATCH hotfixes



.



(goals) -B, , .



Release



$ ./mvnw gitflow: release -B -DgitflowDigitToIncrement = $RELEASE_DIGIT


. master SNAPSHOT , . (goal ) maven , .





$ ./mvnw gitflow: release-start -B -DgitflowDigitToIncrement = $RELEASE_DIGIT


$ git push origin HEAD


. , , , (, , ). , , .



$ git symbolic-ref refs/heads/$CI_COMMIT_REF_NAME refs/remotes/origin/$CI_COMMIT_REF_NAME
$ ./mvnw gitflow:release-finish -B -DgitflowDigitToIncrement=$RELEASE_DIGIT


release . Git ( ref) HEAD . Gitlab CI . HEAD . , , HEAD.



master , .



(Hotfix)



, , , , .



$ ./mvnw gitflow:hotfix-start -B -DgitflowDigitToIncrement=$HOTFIX_DIGIT
$ git push origin HEAD


Hotfix-start hotfix, .



$ export CURRENT_VERSION=${CI_COMMIT_REF_NAME/hotfix\/}
$ git symbolic-ref refs/heads/$CI_COMMIT_REF_NAME refs/remotes/origin/$CI_COMMIT_REF_NAME
$ ./mvnw gitflow:hotfix-finish -B -DgitflowDigitToIncrement=$HOTFIX_DIGIT -DhotfixVersion=$CURRENT_VERSION


Hotfix-finish master . : . , , . .



, hotfix-start , . , .



. , .





. , - !



, git CI runners . , , .



, . CI .



Gitlab CI GitHub.








All Articles