2017-08-28 99 views
0

我是詹金斯和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一個新的子元素的任何實例。

它是批量更新詹金斯作業配置正確的方法?感謝任何幫助/建議。

提前致謝!

回答

0

可以在NodeList

NodeList config = node.userRemoteConfigs."hudson.plugins.git.UserRemoteConfig" 
    config.add(0, new Node(config[0], "TIMEOUT", "theValue")) 
使用 add方法