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).
See:
MultiJob_Template
|
+-- Sub1_Template
+-- Sub2_Template
+-- Sub3_Template
CopyJob (Parameters: NewSuffix)
When manually triggering the "CopyJob" it shall create a new MultiJob with new SubJobs:
MultiJob_BranchXYZ
|
+-- Sub1_BranchXYZ
+-- Sub2_BranchXYZ
+-- Sub3_BranchXYZ
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?
// ????
}