通过groovy job复制Jenkins Multijob作业及其所有下游作业

时间:2014-09-11 13:18:10

标签: groovy jenkins

我正在尝试编写一个Jenkins工作(比如CopyJob)复制另一个工作(在这个工作中使用Multijob插件),并将所有下游工作复制到新工作。我们的想法是使用Multijob作为模板,以便将其复制到新的Multijobs(例如,用于特定的分支或功能)。

请参阅:

MultiJob_Template
   |
   +-- Sub1_Template
   +-- Sub2_Template
   +-- Sub3_Template

CopyJob (Parameters: NewSuffix)

手动触发" CopyJob"它将使用新的SubJobs创建一个新的MultiJob:

MultiJob_BranchXYZ
   |
   +-- Sub1_BranchXYZ
   +-- Sub2_BranchXYZ
   +-- Sub3_BranchXYZ

到目前为止,我成功地复制了Multijob并复制了Subjobs,但我无法找到一种方法让新的Multijob实际上依赖于新的Subjobs。 到目前为止,我的代码(用于CopyJob groovy脚本)是:

import jenkins.model.*
import com.tikal.jenkins.plugins.multijob.*

def templateJobName = "MultiJob_Template"

// Retrieve parameters 
def newSfx = build.buildVariableResolver.resolve("NewSuffix")
def templateJob = Jenkins.instance.getJob(templateJobName)

// copy Multijob
def newJob = Jenkins.instance.copy(templateJob, 'Multijob_' + newSfx)
newJob.save()

// copy all downstreamjobs
def subs = newJob.getDownstreamProjects()
for (s in subs) {
  def oldSubJob = Jenkins.instance.getJob(s.getDisplayName())
  def newSubJob = Jenkins.instance.copy(oldSubJob, s.getDisplayName().replaceFirst(/Template/, newSfx))
  newSubJob.save()

  // how to update the MultiJob_newSfx DownstreamJoblist to use the newSubJob?
  // ????
}

2 个答案:

答案 0 :(得分:2)

我实际上自己设法解决了这个问题。也许还有其他方法,但最好是逐步完成构建器列表,然后是MultiJob模板的PhaseJobs列表。

MultiJob plugin本身的代码有助于此解决方案。 如果你正在寻找类似的东西,也值得看看这个question

import jenkins.model.*
import com.tikal.jenkins.plugins.multijob.*

def jenkinsInstance = jenkins.model.Jenkins.instance 
def templateJobName = "Multijob_Template"

// Retrieve parameters
def newSfx = build.buildVariableResolver.resolve("NewSuffix")

// create new MultiJob
def templateJob = Jenkins.instance.getJob(templateJobName)
def newJob = Jenkins.instance.copy(templateJob, 'Multijob_' + newSfx)
newJob.save()

// get MultiJob BuildPhases and clone each PhaseJob
def builders = newJob.getBuilders()
builders.each { builder ->
    builder.getPhaseJobs().each() { pj ->
      println "cloning phasejob: " + pj.getJobName()

      def subTemplate = Jenkins.instance.getJob(pj.getJobName())
      def newSubJob = Jenkins.instance.copy(subTemplate, pj.getJobName().replaceFirst(/Template/, newSfx))
      newSubJob.save()
      pj.setJobName(newSubJob.getDisplayName())
    }
}

// update dependencies
jenkinsInstance.rebuildDependencyGraph()

答案 1 :(得分:2)

我在Jenkins中使用带有参数的Scriptler插件:

NewSuffix,TemplateStr和templateJobName。我调整了pitseeker的脚本以使用它们并解决Jenkins v1.580.3中的运行时问题:

import jenkins.model.*
import com.tikal.jenkins.plugins.multijob.*

def jenkinsInstance = jenkins.model.Jenkins.instance 

// Retrieve parameters
def newSfx = NewSuffix
println "using new suffix " + newSfx

// create new MultiJob
def templateJob = Jenkins.instance.getJob(templateJobName)
println "Found templateJob " + templateJobName

def newJobName = templateJobName.replaceFirst(/$TemplateStr/, newSfx)

def newJob = Jenkins.instance.copy(templateJob, templateJobName + newSfx)
newJob.save()
println "Copied template job to " + newJob.getName()

// get MultiJob BuildPhases and clone each PhaseJob
def builders = newJob.getBuilders()
builders.each { builder ->
    builder.getPhaseJobs().each() { pj ->
      def pjNameOrig = pj.getJobName()
      def pjNameNew = pjNameOrig.replaceFirst(/$TemplateStr/, newSfx)
      println "pjNameOrig = $pjNameOrig, pjNameNew=$pjNameNew"

      if (pjNameNew != pjNameOrig)
      {
          println "cloning phasejob: " + pjNameOrig

          def subTemplate = Jenkins.instance.getJob(pjNameOrig)

          def newSubJob = Jenkins.instance.copy(subTemplate, pjNameNew)
          newSubJob.save()
          pj.setJobName(newSubJob.getDisplayName())
      }
      else
      {
          println "Not cloning phasejob, keeping original job: " + pjNameOrig
      }
    }
}

// update dependencies
jenkinsInstance.rebuildDependencyGraph()