我正在尝试编写一个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和Copying 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? // ???? }
我实际上设法自己解决了。也许还有其他方法,但是最好逐步浏览构建器列表,然后逐步浏览MultiJob模板的PhaseJobs列表。
MultiJob插件本身的代码为该解决方案提供了帮助。
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()