首页 > 解决方案 > 如何在詹金斯中自动增加 XML elt 值

问题描述

我的项目 repo 中有一个 xml 文件,在结帐后,我想为每个构建自动增加版本号的最后一个值。我已经在使用 xmlslurper 访问版本号并将其存储在一个变量中。我的管道脚本中有类似的内容,而我的 xml 文件只是一个带有 1.0.0.0 的单行脚本,我希望为每个构建自动增加最后一个数字。注意:我不想使用詹金斯的内部版本号。只需在我的项目存储库中将最后一位更改为 $BUILD_NUMBER 就很容易了。

@NonCPS

String getVersion(String path) 
{
  def version = new xmlSlurper().parse(new File(path))
  return version.toString()
}

node 
{


 stage ('checkout') {
 .........

 }

stage ('build) {
def pwd2=pwd()
def v=getVersion("${pwd2}/VersionNumber.xml")


sh ' dotnet publish path/to/project -p:Fileversion=$v '

 }
}

标签: jenkinsjenkins-pipeline

解决方案


没有插件,所以我想你可以使用 groovy。如果你不喜欢版本号,试试这个

def test = "1.1.1.1"
def splitted = test.split('\\.')
splitted[3] = (((splitted[3] as BigDecimal) + 1) as String)
def testInc = splitted.join('.')
print(testInc)

对于内部版本号方法,我建议将整个版本保留在 jenkinsfile 中,应该很简单:

def version = "1.0.0.${env.BUILD_NUMBER}"

推荐阅读