复制詹金斯Multijob工作和所有下游工作通过groovy工作 [英] Copy Jenkins Multijob job and all its downstream jobs via groovy job

查看:127
本文介绍了复制詹金斯Multijob工作和所有下游工作通过groovy工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write a Jenkins job (say CopyJob) that copies another job (in this job using the Multijob plugin) and also copies all its downstream jobs to new jobs. The idea is to have a Multijob that serves as a template so it can be copied to new Multijobs (e.g. for a specific branch or feature).

请参阅:

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

CopyJob (Parameters: NewSuffix)

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

When manually triggering the "CopyJob" it shall create a new MultiJob with new SubJobs:

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



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

So far I was successful with copiing the Multijob and copiing the Subjobs, but I couldn't find a way to make the new Multijob actually depend on the new Subjobs. My code (for the CopyJob groovy script) so far is:

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?
  // ????
}


推荐答案

我真的设法解决它我。也许还有其他方法,但最好逐步列出构建器列表,然后逐步列出MultiJob模板的PhaseJob列表。

I actually managed to solve it myself. Maybe there are other ways too, but it seems best to step through the list of builders and then the list of PhaseJobs of the MultiJob template.

MultiJob插件本身对此解决方案有所帮助。
也值得看看这个问题

The code of the MultiJob plugin itself helped for this solution. It is also worth having a look at this question if you are looking for similar things.

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()

这篇关于复制詹金斯Multijob工作和所有下游工作通过groovy工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆