如何批量更新Jenkins作业配置xml文件?
问题描述:
我是詹金斯和Groovy的新手。我需要批量更新Jenkins作业配置文件。例如,更改大量作业的Git URL或添加超时行为。要做到这一点我使用这块Groovy代码生成詹金斯作业名称的列表:如何批量更新Jenkins作业配置xml文件?
import com.cloudbees.hudson.plugins.folder.*
void processFolder(Item folder) {
folder.getItems().each {
if(it instanceof Folder) {
processFolder(it)
} else {
processJob(it)
}
}
}
void processJob(Item job) {
println job.fullName
}
Jenkins.instance.getItems().each {
if(it instanceof Folder) {
processFolder(it)
} else {
processJob(it)
}
}
内processJob()方法,我打算使用作业名来获取作业的配置文件
void processJob(Item job) {
AbstractItem item = (AbstractItem)Jenkins.getInstance().getItem(job.name);
XmlFile configXml = item.getConfigFile();
File xmlfile = configXml.getFile();
def xml = new XmlParser().parse(xmlfile)
}
示例配置文件:
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<keepDependencies>false</keepDependencies>
<properties/>
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>https://github.com/gitboy/Backup.git</url>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
</project>
在这里,我需要的URL标记后添加一个新的元素TIMEOUT。
但XmlParser的给出了一个输出看起来是这样的:
project[attributes={}; value=[actions[attributes={}; value=[]],
description[attributes={}; value=[]], keepDependencies[attributes={}; value=
[false]], properties[attributes={}; value=[]], scm[attributes=
{class=hudson.plugins.git.GitSCM, [email protected]}; value=
[configVersion[attributes={}; value=[2]], userRemoteConfigs[attributes={};
value=[hudson.plugins.git.UserRemoteConfig[attributes={}; value=
[url[attributes={}; value=
[https://github.com/gitboy/Backup.git]]]]]],
branches[attributes={}; value=[hudson.plugins.git.BranchSpec[attributes={};
value=[name[attributes={}; value=[*/master]]]]]]]
我找不到如何添加使用XmlParser的/的XmlSlurper一个新的子元素的任何实例。
它是批量更新詹金斯作业配置正确的方法?感谢任何帮助/建议。
提前致谢!
答
可以在NodeList
NodeList config = node.userRemoteConfigs."hudson.plugins.git.UserRemoteConfig"
config.add(0, new Node(config[0], "TIMEOUT", "theValue"))
使用
add
方法