以下情况。
目标:从Jenkins管道(来自SCM的Jenkinsfile)触发其他构建作业X。但在此之前,出于进一步复杂配置的目的,我想解析该构建作业X的远程,并提取它的值“config.xml”,这是构建作业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>这是echo的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()方法A例外:
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
谢谢!
发布于 2020-01-09 04:18:35
如果您可以将外壳命令与xpath表达式一起使用:
$ xidel -se '//remote/text()' file.xml输出
http://LAPTOP/svn/localrepo/CCC/COMPONENT/RC发布于 2020-01-09 14:10:11
下面是您可以放入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/RChttps://stackoverflow.com/questions/59653182
复制相似问题