需要从某些XML文件中提取一些值。从Jenkins文件触发提取。以下代码在我的Jenkinsfile中可以正常工作:
def xml_file_contents = new XmlParser().parseText(xml_file)
def value_i_need_to_extract = xml_file_contents.children()[4].children()[0].children()[0].children()[0].text().toString()
但是,解析的XML文件可能会在以后修改,因此通过XML节点名称而不是使用children()方法进行遍历将是一个更好的主意。但是,每次我应用这样的内容:
def value_i_need_to_extract = xml_file_contents.nodename.nodename.nodename.nodename.text().toString()
我收到以下错误:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote
我尝试使用不同的XML文件和不同的节点名称遍历节点名称,但是结果始终是错误。
尽管我已经批准了Jenkins进程内脚本批准所要求的所有安全功能,但仍然出现此错误。
问题说明示例:
代码A:
def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].children()[0]
echo 'Remote repository is:' + remote_repo.toString()
输出A:
Remote repository is:remote[attributes={}; value=[http://LAPTOP/svn/localrepo/app/component/RC]]
代码B:
def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].children()[0].text()
echo 'Remote repository is:' + remote_repo.toString()
输出B:
Remote repository is:http://LAPTOP/svn/localrepo/app/component/RC
代码C:
def remote_repo = xml_file_contents.children()[3].children()[0].children()[0].remote.text()
输出C:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field groovy.util.Node remote
您知道这种行为的可能原因吗?有没有一种方法可以通过节点名称遍历XML?
示例XML文件:
<?xml version='1.1' encoding='UTF-8'?>
<project>
<description>Just a sample build job</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/subfolder/componentname/RC</remote>
<credentialsId>justsomeid</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>
答案 0 :(得分:0)
XmlParser should not be used,因为它不会序列化(它只能在主节点内工作,该主节点不应有任何执行程序)。
不幸的是,Jenkins没有像JSON和YAML一样提供可序列化的步骤(请参见Pipeline Utility Steps)。
如果您可以将XML转换为JSON或YAML,则将可以适当地使用readJSON
/ readYaml
步骤,并且也可以更轻松地浏览它们。
如果不可能,则应考虑编写一个小型实用程序来帮助您处理XML文件:这样,您将能够在不是主节点的代理上运行管道脚本。
答案 1 :(得分:0)
以下内容可在我的Groovy控制台上使用:
def xml_file = """<?xml version='1.1' encoding='UTF-8'?>
<project>
<description>Just a sample build job</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/subfolder/componentname/RC</remote>
<credentialsId>justsomeid</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>"""
def xml_file_contents = new XmlParser().parseText(xml_file)
println xml_file_contents.scm.locations[0].children()[0].remote.text()
输出:
http://LAPTOP/svn/localrepo/subfolder/componentname/RC