首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用XmlParser().parseText(xml_file)将值从XML文件提取到Jenkinsfile管道

使用XmlParser().parseText(xml_file)将值从XML文件提取到Jenkinsfile管道
EN

Stack Overflow用户
提问于 2020-01-09 04:12:39
回答 2查看 1.9K关注 0票数 0

以下情况。

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

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

这是我的config.xml的样子:

代码语言:javascript
复制
<?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>

这是echo的Jenkins管道的控制台输出:

代码语言:javascript
复制
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=[]]]]

我的代码运行正常:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
def remote_scm_path = xml_file_contents.project.locations.hudson.scm.SubversionSCM_-ModuleLocation.remote
echo remote_scm_path.toString()

方法A例外:

代码语言:javascript
复制
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node project

方法B代码:

代码语言:javascript
复制
def remote_scm_path = xml_file_contents.remote
echo remote_scm_path.toString()

方法B例外:

代码语言:javascript
复制
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote

方法C代码:

代码语言:javascript
复制
echo "${xml_file_contents.project.locations.hudson.scm.SubversionSCM_-ModuleLocation.remote.text()}"

方法C异常:

代码语言:javascript
复制
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

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2020-01-09 04:18:35

如果您可以将外壳命令与xpath表达式一起使用:

代码语言:javascript
复制
$ xidel -se '//remote/text()' file.xml

输出

代码语言:javascript
复制
http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC
票数 0
EN

Stack Overflow用户

发布于 2020-01-09 14:10:11

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

代码语言:javascript
复制
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()

这将为我打印以下内容:

代码语言:javascript
复制
http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59653182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档