How to Auto-increment XML elt value in jenkins

545 views Asked by At

I have an xml file in my project repo which after checkout, I will like to auto increment the last value of the version number for every build. I am already accessing the version number using xmlslurper and storing this in a variable. I have something like this in my pipeline script and my xml file is just a single line script with 1.0.0.0 and I will like auto increment the last digit for every build. NB: I do not want to use jenkins's build number. That would have been easy by just changing the last digit to $BUILD_NUMBER in my project repo.

@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 '

 }
}
1

There are 1 answers

8
hakamairi On BEST ANSWER

There's no plugin for this, so I guess you can just go with groovy. If you don't like the build number, try this

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)

For the build number approach I would suggest to keep the whole version in jenkinsfile, should be as simple as:

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