2017-08-28 24 views
0

私はJenkinsとGroovyの初心者です。 Jenkinsのジョブ設定ファイルを一括更新する必要があります。たとえば、大量のジョブのGit URLを変更するか、タイムアウト動作を追加します。私はGroovyのコードのこの部分を使用したジェンキンスジョブ名のリストを生成してることを行うには:Jenkinsジョブのconfig 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/XmlSlurperを使って新しい子要素を追加する方法上の任意の例を見つけることができませんでした

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は次のようになり、出力を提供します。

Jenkinsジョブの設定を一括更新するのは正しいアプローチですか?助けや提案を感謝します。

ありがとうございます!

答えて

0

あなたはNodeList

NodeList config = node.userRemoteConfigs."hudson.plugins.git.UserRemoteConfig" 
    config.add(0, new Node(config[0], "TIMEOUT", "theValue")) 
add方法を使用することができます