Extract value from XML file to Jenkinsfile pipeline, using XmlParser().parseText(xml_file)

3.8k Views Asked by At

The following situation.

The goal: from Jenkins pipeline (Jenkinsfile from SCM) to trigger some other build job X. But before that, for some purposes of further sophisticated configuration, I want to parse the config.xml of that build job X and to extract its value “remote”, which is the path to repository of the build job X.

I have constructed a correctly working path to the config.xml of the build job X and now I can do readFile for the config.xml.

This is what my config.xml looks like:

<?xml version='1.1' encoding='UTF-8'?>
<project>
  <description>Just a sample build job that should always be successful.</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.plugins.jira.JiraProjectProperty plugin="[email protected]"/>
  </properties>
  <scm class="hudson.scm.SubversionSCM" plugin="[email protected]">
    <locations>
      <hudson.scm.SubversionSCM_-ModuleLocation>
        <remote>http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC</remote>
        <credentialsId>someid</credentialsId>
        <local>.</local>
        <depthOption>infinity</depthOption>
        <ignoreExternalsOption>true</ignoreExternalsOption>
        <cancelProcessOnExternalsFail>true</cancelProcessOnExternalsFail>
      </hudson.scm.SubversionSCM_-ModuleLocation>
    </locations>
    <excludedRegions></excludedRegions>
    <includedRegions></includedRegions>
    <excludedUsers></excludedUsers>
    <excludedRevprop></excludedRevprop>
    <excludedCommitMessages></excludedCommitMessages>
    <workspaceUpdater class="hudson.scm.subversion.UpdateUpdater"/>
    <ignoreDirPropChanges>false</ignoreDirPropChanges>
    <filterChangelog>false</filterChangelog>
    <quietOperation>true</quietOperation>
  </scm>
  <canRoam>true</canRoam>
  <disabled>false</disabled>
  <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
  <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
  <triggers/>
  <concurrentBuild>false</concurrentBuild>
  <builders/>
  <publishers>
    <hudson.plugins.ws__cleanup.WsCleanup plugin="[email protected]">
      <patterns class="empty-list"/>
      <deleteDirs>false</deleteDirs>
      <skipWhenFailed>false</skipWhenFailed>
      <cleanWhenSuccess>true</cleanWhenSuccess>
      <cleanWhenUnstable>true</cleanWhenUnstable>
      <cleanWhenFailure>true</cleanWhenFailure>
      <cleanWhenNotBuilt>true</cleanWhenNotBuilt>
      <cleanWhenAborted>true</cleanWhenAborted>
      <notFailBuild>false</notFailBuild>
      <cleanupMatrixParent>false</cleanupMatrixParent>
      <externalDelete></externalDelete>
      <disableDeferredWipeout>false</disableDeferredWipeout>
    </hudson.plugins.ws__cleanup.WsCleanup>
  </publishers>
  <buildWrappers/>
</project>

This is console output of the Jenkins pipeline for echo:

project[attributes={}; value=[description[attributes={}; value=[Just a sample build job that should always be successful.]], keepDependencies[attributes={}; value=[false]], properties[attributes={}; value=[hudson.plugins.jira.JiraProjectProperty[attributes={[email protected]}; value=[]]]], scm[attributes={class=hudson.scm.SubversionSCM, [email protected]}; value=[locations[attributes={}; value=[hudson.scm.SubversionSCM_-ModuleLocation[attributes={}; value=[remote[attributes={}; value=[ http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC]], credentialsId[attributes={}; value=[someid]], local[attributes={}; value=[.]], depthOption[attributes={}; value=[infinity]], ignoreExternalsOption[attributes={}; value=[true]], cancelProcessOnExternalsFail[attributes={}; value=[true]]]]]], excludedRegions[attributes={}; value=[]], includedRegions[attributes={}; value=[]], excludedUsers[attributes={}; value=[]], excludedRevprop[attributes={}; value=[]], excludedCommitMessages[attributes={}; value=[]], workspaceUpdater[attributes={class=hudson.scm.subversion.UpdateUpdater}; value=[]], ignoreDirPropChanges[attributes={}; value=[false]], filterChangelog[attributes={}; value=[false]], quietOperation[attributes={}; value=[true]]]], canRoam[attributes={}; value=[true]], disabled[attributes={}; value=[false]], blockBuildWhenDownstreamBuilding[attributes={}; value=[false]], blockBuildWhenUpstreamBuilding[attributes={}; value=[false]], triggers[attributes={}; value=[]], concurrentBuild[attributes={}; value=[false]], builders[attributes={}; value=[]], publishers[attributes={}; value=[hudson.plugins.ws__cleanup.WsCleanup[attributes={[email protected]}; value=[patterns[attributes={class=empty-list}; value=[]], deleteDirs[attributes={}; value=[false]], skipWhenFailed[attributes={}; value=[false]], cleanWhenSuccess[attributes={}; value=[true]], cleanWhenUnstable[attributes={}; value=[true]], cleanWhenFailure[attributes={}; value=[true]], cleanWhenNotBuilt[attributes={}; value=[true]], cleanWhenAborted[attributes={}; value=[true]], notFailBuild[attributes={}; value=[false]], cleanupMatrixParent[attributes={}; value=[false]], externalDelete[attributes={}; value=[]], disableDeferredWipeout[attributes={}; value=[false]]]]]], buildWrappers[attributes={}; value=[]]]]

My code that works OK:

def triggered_job_config_file = JENKINS_HOME + '\\jobs\\' + RUNLIST[element] + '\\config.xml'
def xml_file = readFile triggered_job_config_file
def xml_file_contents = new XmlParser().parseText(xml_file)

My code that does not work OK:

Approach A code:

def remote_scm_path = xml_file_contents.project.locations.hudson.scm.SubversionSCM_-ModuleLocation.remote
echo remote_scm_path.toString()

Approach A exception:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node project

Approach B code:

def remote_scm_path = xml_file_contents.remote
echo remote_scm_path.toString()

Approach B exception:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote

Approach C code:

echo "${xml_file_contents.project.locations.hudson.scm.SubversionSCM_-ModuleLocation.remote.text()}"

Approach C exception:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node project

My question: does anybody have a good idea, how I can traverse to the value of "remote" in config.xml, which is this one: http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC

Thanks!

2

There are 2 best solutions below

5
On

If you can use a shell command with an expression :

$ xidel -se '//remote/text()' file.xml

Output

http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC
0
On

Here's something you can drop into your Groovy console:

def xml_file = """<?xml version='1.1' encoding='UTF-8'?>
<project>
  <description>Just a sample build job that should always be successful.</description>
  <keepDependencies>false</keepDependencies>
  <properties>
    <hudson.plugins.jira.JiraProjectProperty plugin="[email protected]"/>
  </properties>
  <scm class="hudson.scm.SubversionSCM" plugin="[email protected]">
    <locations>
      <hudson.scm.SubversionSCM_-ModuleLocation>
        <remote>http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC</remote>
        <credentialsId>someid</credentialsId>
      </hudson.scm.SubversionSCM_-ModuleLocation>
    </locations>
  </scm>
  <canRoam>true</canRoam>
  <buildWrappers/>
</project>"""

def xml_file_contents = new XmlParser().parseText(xml_file)
println xml_file_contents.scm.locations[0].children()[0].remote.text()

This prints this for me:

http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC