使用XmlParser()。parseText(xml_file)从XML文件中提取值到Jenkinsfile管道

时间:2020-01-08 20:12:38

标签: xml parsing jenkins jenkins-pipeline traversal

以下情况。

目标:从Jenkins管道(来自SCM的Jenkinsfile)触发其他构建作业X。但是在此之前,出于进一步复杂配置的目的,我想解析该构建作业X的config.xml并提取其值“ remote”,这是构建作业X的存储库的路径。

我已经构建了构建作业X的config.xml的正确工作路径,现在我可以为config.xml做readFile了。

这是我的config.xml的样子:

<?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="jira@3.0.11"/>
  </properties>
  <scm class="hudson.scm.SubversionSCM" plugin="subversion@2.12.2">
    <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="ws-cleanup@0.37">
      <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>

这是Jenkins管道的控制台输出,用于回显:

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={plugin=jira@3.0.11}; value=[]]]], scm[attributes={class=hudson.scm.SubversionSCM, plugin=subversion@2.12.2}; 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={plugin=ws-cleanup@0.37}; 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=[]]]]

我的代码正常运行:

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)

我的代码无法正常工作:

方法A代码:

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

方法异常:

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

方法B代码:

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

方法B例外:

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

方法C代码:

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

方法C例外:

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

我的问题:是否有人有一个好主意,如何遍历config.xml中的“ remote”值,即:http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC

谢谢!

2 个答案:

答案 0 :(得分:0)

如果可以使用带有表达式的shell命令:

const arr1 = [
  { name: 'A', code: 'D' },
  { name: 'B', code: 'A' },
  { name: 'A', code: 'Z' },
  { name: 'A', code: 'A' },
  { name: 'B', code: 'D' },
  { name: 'B', code: 'B' },
  { name: 'B', code: 'B' }
];
sortByFieldsInPlace(arr1, ['name', 'code'])
console.log(arr1)
// [
//   { name: 'A', code: 'A' },
//   { name: 'A', code: 'D' },
//   { name: 'A', code: 'Z' },
//   { name: 'B', code: 'A' },
//   { name: 'B', code: 'B' },
//   { name: 'B', code: 'B' },
//   { name: 'B', code: 'D' }
// ]

const arr2 = [
  { name: 'A', code: 'D' },
  { name: 'B', code: 'A' },
  { name: 'A', code: 'Z' },
  { name: 'A', code: 'A' },
  { name: 'B', code: 'D' },
  { name: 'B', code: 'B', status: 10 },
  { name: 'B', code: 'B', status: 9 }
];
sortByFieldsInPlace(arr2, ['name', 'code', 'status'])
console.log(arr2)
// [
//   { name: 'A', code: 'A' },
//   { name: 'A', code: 'D' },
//   { name: 'A', code: 'Z' },
//   { name: 'B', code: 'A' },
//   { name: 'B', code: 'B', status: 9 },
//   { name: 'B', code: 'B', status: 10 },
//   { name: 'B', code: 'D' }
// ]

输出

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

答案 1 :(得分:0)

以下是您可以放入Groovy控制台的内容:

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="jira@3.0.11"/>
  </properties>
  <scm class="hudson.scm.SubversionSCM" plugin="subversion@2.12.2">
    <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()

这为我打印了此

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