How do I dynamically trigger downstream builds in jenkins? How do I dynamically trigger downstream builds in jenkins? jenkins jenkins

How do I dynamically trigger downstream builds in jenkins?


def job = hudson.model.Hudson.instance.getJob("job")def params = new StringParameterValue('PARAMTEST', "somestring")  def paramsAction = new ParametersAction(params) def cause = new hudson.model.Cause.UpstreamCause(currentBuild)def causeAction = new hudson.model.CauseAction(cause) hudson.model.Hudson.instance.queue.schedule(job, 0, causeAction, paramsAction) 

This is what finally worked for me.


NOTE: The Pipeline Plugin should render this question moot, but I haven't had a chance to update our infrastructure.

To start a downstream job without parameters:

job = manager.hudson.getItem(name)cause = new hudson.model.Cause.UpstreamCause(manager.build)causeAction = new hudson.model.CauseAction(cause)manager.hudson.queue.schedule(job, 0, causeAction)

To start a downstream job with parameters, you have to add a ParametersAction. Suppose Job1 has parameters A and C which default to "B" and "D" respectively. I.e.:

A == "B"C == "D"

Suppose Job2 has the same A and B parameters, but also takes parameter E which defaults to "F". The following post build script in Job1 will copy its A and C parameters and set parameter E to the concatenation of A's and C's values:

params = []val = ''manager.build.properties.actions.each {    if (it instanceof hudson.model.ParametersAction) {        it.parameters.each {            value = it.createVariableResolver(manager.build).resolve(it.name)            params += it            val += value        }    }}params += new hudson.model.StringParameterValue('E', val)paramsAction = new hudson.model.ParametersAction(params)jobName = 'Job2'job = manager.hudson.getItem(jobName)cause = new hudson.model.Cause.UpstreamCause(manager.build)causeAction = new hudson.model.CauseAction(cause)def waitingItem = manager.hudson.queue.schedule(job, 0, causeAction, paramsAction)def childFuture = waitingItem.getFuture()def childBuild = childFuture.get()hudson.plugins.parameterizedtrigger.BuildInfoExporterAction.addBuildInfoExporterAction(    manager.build, childProjectName, childBuild.number, childBuild.result)

You have to add $JENKINS_HOME/plugins/parameterized-trigger/WEB-INF/classes to the Groovy Postbuild plugin's Additional groovy classpath.


Execute this Groovy script

import hudson.model.*import jenkins.model.*def build = Thread.currentThread().executabledef jobPattern = "PUTHEREYOURJOBNAME"     def matchedJobs = Jenkins.instance.items.findAll { job ->    job.name =~ /$jobPattern/}matchedJobs.each { job ->    println "Scheduling job name is: ${job.name}"    job.scheduleBuild(1, new Cause.UpstreamCause(build), new ParametersAction([ new StringParameterValue("PROPERTY1", "PROPERTY1VALUE"),new StringParameterValue("PROPERTY2", "PROPERTY2VALUE")]))}

If you don't need to pass in properties from one build to the other just take the ParametersAction out.

The build you scheduled will have the same "cause" as your initial build. That's a nice way to pass in the "Changes". If you don't need this just do not use new Cause.UpstreamCause(build) in the function call