How to read xml properties from a xml file in groovy Jenkinsfile

Viewed 2595

I have a XML file like the below :-

<?xml version="1.0" encoding="UTF-8"?>
<properties>
<Targets>
   <Target>
      <branch>develop</branch>
      <env>dev</env>
   </Target>
   <Target>
      <branch>master</branch>
      <env>prod</env>
   </Target>
</Targets>
</properties>

Now i would like to read the values for the first branch and second branch as well as the first env and second env from this file in a scripted Jenkinsfile (groovy). Is there a way to parse this XML and then extract the actual values in the groovy scripted Jenkinsfile for both branch and env?

1 Answers

I was able to solve this using the below in my Jenkinsfile :-

node {

checkout scm
def File = readFile "${env.WORKSPACE}/myproperties.xml"
echo "${File}"

def xml = new XmlSlurper().parseText(File)
def env = xml.Targets.Target[1].env[0]

echo "${env}"

}

Related