使用TeamCity在Maven项目中实现自动化HotFix

这篇文章将描述如何使用Teamcity在Maven项目中配置HotFix自动化。


要进行HotFix,通常需要执行许多手动操作:


  1. 为您要将HotFix发行到的发行版创建早午餐
  2. 修复发布错误
  3. 在发布早午餐中更改错误修正版本
  4. 推出标签错误修正版本

项目1,3,4可以自动化。


在继续讨论该主题之前,我想介绍一个重要而复杂的主题- 版本控制软件。 可以在此屏幕截图中简要了解有关Semver的信息。


您可以在链接上阅读更多内容: 1


这篇文章中描述的所有设置都基于Semver基于Trunk的开发


在基于主干的开发中,您需要为每个版本创建自己的早午餐。 此版本中的所有更改(修补程序)都已提交到此早午餐。


作为本文的一部分,我们将自动执行以下操作:


  • CI组装


  • 创建新版本


  • 创建发布早午餐


  • 修改错误修正版本




要求:



让我们在Teamcity中创建“ Automation Maven Hotfix”项目,并在其中创建4个任务。


  • CI构建(CI构建)


  • 创建要发布的分支


  • Maven增量错误修正(更改错误修正版本)


  • Maven版本(创建新版本)



项目截图:



常规设置


在所有任务中,都必须设置复选框“ 清理构建:在构建之前删除checkout目录中的所有文件 ”,因为缺少此数据时,我会遇到错误。


创建单个VCS。 VCS具有红色圆圈。



VCS通常使用HTTPS方案。 在Branch规范中:指示监视所有早午餐和所有标签:


+:refs/heads/* +:refs/tags/* 

必须创建4个配置参数。


  • BRANCH_FOR_INCREMENT
  • TAG_FROM_VERSION
  • TEAM_USER
  • TEAM_USER_EMAIL

BRANCH_FOR_INCREMENT和TAG_FROM_VERSION中的值字段必须保留为空白。



您必须上传/添加私钥。 除CI Build外,所有任务均需要私钥。



在除CI Build之外的所有任务中,都需要在Build Features部分中连接私钥。


Maven发布示例



CI版本**。


CI Build任务中,只需一步即可进行mvn clean测试



Maven发布


Maven发行版分两个步骤。 第一步是验证早午餐是否为主 。 如果早午餐不是master ,则任务失败。


 BRANCH=$(git branch | grep \* | cut -d ' ' -f2) echo "$BRANCH" if [[ "$BRANCH" != "master" ]]; then echo 'Branch is not master'; echo 'Aborting'; exit 1; fi 


第二步是标准的mvn版本:使用--batch-mode选项进行准备



创建要发布的分支


要创建发布的修补程序,您需要创建早午餐。 这是通过为发布任务创建分支来完成的。 她有2个步骤。


第一步检查早午餐不是master ,第二步检查pom.xml文件中的版本不包含单词SNAPSHOT


 BRANCH=$(git branch | grep \* | cut -d ' ' -f2) echo "$BRANCH" if [[ "$BRANCH" == "master" ]]; then echo 'Branch is master'; echo 'Aborting'; exit 1; fi echo "Get version package from pom.xml" version=`python -c "import xml.etree.ElementTree as ET; print(ET.parse(open('pom.xml')).getroot().find('{http://maven.apache.org/POM/4.0.0}version').text)"` echo "Check SNAPSHOT" if [[ $version == "*SNAPSHOT*" ]]; then echo "******************* WARNING *************************" echo "************ You are create branch for SNAPSHOTS ******************" echo "***********************************************************" exit 1 fi 


第二步将developerConnection的连接方案从HTTPS更改为GIT。


 #   developerConnection   pom.xml developerConnection=$(xmllint -xpath "/*[local-name() = 'project' ]//*[local-name() = 'developerConnection']/text()" pom.xml | sed 's|scm:git:ssh://||') echo developerConnection echo $developerConnection #   /  :  URL  git_remote_url git_remote_url=$(echo $developerConnection| sed 's/gitlab.com\//gitlab.com:/g') echo git_remote_url echo $git_remote_url git remote set-url origin $git_remote_url #       Teamcity  user  email  ~/.gitconfig,      echo 'git config user.name %TEAM_USER%' git config user.name %TEAM_USER% echo 'git config user.email %TEAM_USER_EMAIL%' git config user.email %TEAM_USER_EMAIL% #      pom.xml echo "Get version package from pom.xml" version=`python -c "import xml.etree.ElementTree as ET; print(ET.parse(open('pom.xml')).getroot().find('{http://maven.apache.org/POM/4.0.0}version').text)"` echo $version # -  fetch  . git fetch if [ `git branch -a | egrep "${version}$"` ] then echo "Branch exists" exit 1 fi #    ,     pom.xml echo "Create branch" git checkout -b $version #  git     . git config --global push.default simple #        pom.xml echo "Push release branch" git push --set-upstream origin $version 


Maven增量错误修正


任务包括6个部分。 可以重构它,但是它可以那样工作。


第一步是验证早午餐不是主要的 。 如果早午餐任务落下。


 BRANCH=$(git branch | grep \* | cut -d ' ' -f2) echo "$BRANCH" if [[ "$BRANCH" == "master" ]]; then echo 'Branch is master'; echo 'Aborting'; exit 1; fi #      pom.xml echo "Get version package from pom.xml" BRANCH=`python -c "import xml.etree.ElementTree as ET; print(ET.parse(open('pom.xml')).getroot().find('{http://maven.apache.org/POM/4.0.0}version').text)"` #   checkout   . #  git status  detached   . #   git status    git checkout $BRANCH #   bash   Teamcity   . echo "##teamcity[setParameter name='BRANCH_FOR_INCREMENT' value='$BRANCH']" 


Maven的第二个步骤是修改pom.xml文件中的错误修正版本。


目标: maven将所有内容整合在一起


 build-helper:parse-version versions:set -DnewVersion=${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.nextIncrementalVersion} versions:commit 


第三步是显示Git状态信息和其他信息:


 echo 'cat pom.xml' cat pom.xml echo 'git status' git status echo 'git remote -v' git remote -v echo 'git branch' git branch 


第四步,将developerConnection的连接方案从HTTPS更改为GIT。


并将更改推送到Teamcity变量%BRANCH_FOR_INCREMENT%中指定的分支


 #   developerConnection   pom.xml developerConnection=$(xmllint -xpath "/*[local-name() = 'project' ]//*[local-name() = 'developerConnection']/text()" pom.xml | sed 's|scm:git:ssh://||') echo developerConnection #   /  :  URL  git_remote_url git_remote_url=$(echo $developerConnection| sed 's/gitlab.com\//gitlab.com:/g') echo git_remote_url echo $git_remote_url git remote set-url origin $git_remote_url #       Teamcity  user  email  ~/.gitconfig,      echo 'git config user.name %TEAM_USER%' git config user.name %TEAM_USER% echo 'git config user.email %TEAM_USER_EMAIL%' git config user.email %TEAM_USER_EMAIL% echo 'git add .' git add . echo 'git commit -m "Increment bugfix"' git commit -m "Increment bugfix" git push --set-upstream origin %BRANCH_FOR_INCREMENT% 


第五步,从pom.xml文件中获取版本,并将TAG_FROM_VERSION变量设置为Teamcity 。 请注意, pom.xml文件中不带字母v的版本位于前面。 以及基于该版本的标签,开头已经带有字母v。


 echo "Get version package from pom.xml" VERSION_AFTER_CHANGE=`python -c "import xml.etree.ElementTree as ET; print(ET.parse(open('pom.xml')).getroot().find('{http://maven.apache.org/POM/4.0.0}version').text)"` echo $VERSION_AFTER_CHANGE echo "##teamcity[setParameter name='TAG_FROM_VERSION' value='v$VERSION_AFTER_CHANGE']" 


第六步是标记错误修正版本。 这是通过使用MavenGoal中所需的选项来完成的。


目标选项:


 -Dtag=%TAG_FROM_VERSION% scm:tag 

Source: https://habr.com/ru/post/zh-CN483242/


All Articles