简体   繁体   中英

Groovy Script to add new phase job to multi-job in Jenkins

For the already available multi-job in jenkins, need to add new phase jobs using Groovy Scripting. I have written the following groovy code which adds up an already existing job p25_deploy-1.
This code is working to create the multi-job but the phase job is not showing as mapped in the Jenkins UI. Where as if I see it config.xml , its created properly as expected except a tag <killPhaseOnJobResultCondition> . Not sure why the phase job is not mapped properly?

import jenkins.model.*
import hudson.model.*
import com.tikal.jenkins.plugins.multijob.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.KillPhaseOnJobResultCondition.*
import java.lang.String.*
import hudson.model.Descriptor;
import hudson.tasks.Builder;

def jenkinsInstance = jenkins.model.Jenkins.instance

def templateJobName = 'profile_p25'

def templateJob = jenkinsInstance.getJob(templateJobName)


 // get MultiJob BuildPhases and clone each PhaseJob
 builders = templateJob.getBuilders();

builders.each { b ->
if (b instanceof MultiJobBuilder){
def pj = b.getPhaseJobs()

   hudson.model.Describable p1 = new PhaseJobsConfig("p25_deploy-1",null,
        true,PhaseJobsConfig.KillPhaseOnJobResultCondition NEVER,null,false,false,null,0,false,true,null,false,false)
   pj.add(p1)
   }  
 }

    templateJob.save()
   // update dependencies
    jenkinsInstance.rebuildDependencyGraph()

Any help will be really appreciated. Have tried many ways but was not able to figure out the problem with the script.

We can use DSL to create but I wanted it to be done in Groovy Scripting and moreover modify the existing job.

Blockquote

Yay! I am back with the answer for my question. Have tried this since very long time. Finally am able to make it though. I was aware that solution would be really simple but not able to figure out the hack of it.

import jenkins.model.*
import hudson.model.*
import com.tikal.jenkins.plugins.multijob.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.*
import com.tikal.jenkins.plugins.multijob.PhaseJobsConfig.KillPhaseOnJobResultCondition.*
import java.lang.String.* 
import hudson.model.Descriptor
import hudson.tasks.Builder

def jenkinsInstance = jenkins.model.Jenkins.instance

def templateJobName = 'profile_p25'

def templateJob = jenkinsInstance.getJob(templateJobName)

// get MultiJob BuildPhases and clone each PhaseJob
builders = templateJob.getBuilders();

builders.each { b -> if (b instanceof MultiJobBuilder)
    { def pj =
        b.getPhaseJobs()

        hudson.model.Describable newphase = new
        PhaseJobsConfig(deploys[i],null,
            true,null,null,false,false,null,0,false,false,"",false,false)
            newphase.killPhaseOnJobResultCondition = 'NEVER'
            pj.add(newphase)    
    }
}
templateJob.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM