从一些XML文件中提取一些值是必需的。从Jenkinsfile中触发提取。下面的代码在我的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>发布于 2020-01-13 20:28:15
XmlParser should not be used,因为它不序列化(它只在主节点内工作,主节点不应该有任何执行器)。
不幸的是,Jenkins没有提供像JSON和YAML那样的可序列化步骤(参见Pipeline Utility Steps)。
如果您能够将XML转换为JSON或YAML,那么您将能够适当地使用readJSON/readYaml步骤,并且您也可以更轻松地导航它们。如果这是不可能的,您应该考虑编写一个小实用程序来帮助您处理XML文件:这样,您就可以在不是主节点的代理上运行管道脚本。
发布于 2020-01-14 15:04:56
下面的代码可以在我的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/RChttps://stackoverflow.com/questions/59713983
复制相似问题