Spawn multiple jobs

173 views
Skip to first unread message

Bryce Pepper

unread,
Dec 9, 2016, 9:54:45 AM12/9/16
to Jenkins Users
I would like to start a new jenkins job for each word passed in a parameter.

I could use a post

for eachWord in ${inputParameter//,/ }; do
{
  curl -X POST --silent --show-error --user user:token \
        $JENKINS_URL/job/testJob/buildWithParameters?inputParameter=${eachWord}
}
done

but I was hoping for a way to passthru the credentials of the user running the parent job.


Daniel Beck

unread,
Dec 9, 2016, 10:18:29 AM12/9/16
to jenkins...@googlegroups.com
Could probably be done with a Pipeline: Get the parameter, tokenize, then call build(…) a bunch of times in a (C-style) loop.
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/fc26224b-2936-4135-ae10-9326e77c01cf%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bryce Pepper

unread,
Dec 9, 2016, 11:19:19 AM12/9/16
to Jenkins Users, m...@beckweb.net
Thanks for the quick reply.  I am not familiar with the pipeline plugin but was wondering how that job gets parameters, is it in the advanced project options? The scripting does not look difficult and I would follow the jobs_in_parallel.groovy example.

Bryce Pepper

unread,
Dec 9, 2016, 3:21:55 PM12/9/16
to Jenkins Users
I tried the Groovy plugin and also the Build Flow Plugin...

I can get multiple jobs started in parallel but I can't get the parameter to pass to the child jobs.

import hudson.model.*
def inputParameter = build.buildVariableResolver.resolve("inputParameter")
def branches = [:]
def int i = 0
inputParameter.replaceAll("^[,\\s]+", "").split("[,\\s]+").each {
  branches["branch${i}"] = {
    build('aa', Param1: [[$class: 'StringParameterValue', name: 'inputParameter', value: "${it}"]])
  }
  i++
}
parallel branches

Bryce Pepper

unread,
Dec 12, 2016, 10:47:05 AM12/12/16
to Jenkins Users
"a b  c,d, e , f" parameters are passing now but I am not getting the "parallel" execution desired. All of the spawned jobs have the same build number and if I look at completed job it has the passed parameter "f". 

def inputParameter = build.buildVariableResolver.resolve("inputParameter").split(/[ ,]+/)
def branches = [:]
def int i = 0 
for (String deployment : inputParameter) {
  println("${i} ${deployment}")
  
  branches["deployment-${deployment}"] = {
    build( 'aa', inputParameter: "${deployment}" )
  }
  
  i++   
}

parallel branches

OUTPUT:
Started by user
0 a
1 b
2 c
3 d
4 e
5 f
parallel {
    Schedule job aa
    Schedule job aa
    Schedule job aa
    Schedule job aa
    Schedule job aa
    Schedule job aa
    Build aa #23 started
    Build aa #23 started
    Build aa #23 started
    Build aa #23 started
    Build aa #23 started
    Build aa #23 started
    aa #23 completed 
}
Aborted by
Build was aborted
Finished: ABORTED



Daniel Beck

unread,
Dec 12, 2016, 11:06:10 AM12/12/16
to jenkins...@googlegroups.com

> On 12.12.2016, at 16:47, Bryce Pepper <b.a.p...@gmail.com> wrote:
>
> "a b c,d, e , f" parameters are passing now but I am not getting the "parallel" execution desired. All of the spawned jobs have the same build number and if I look at completed job it has the passed parameter "f".

I'm pretty sure you're not passing different parameters -- just print `deployment` inside the actual block, not directly in the loop.

And if that's correct: Jenkins collapses identical queue items. Use different parameters if you want different builds.


Bryce Pepper

unread,
Dec 13, 2016, 9:32:41 AM12/13/16
to Jenkins Users, m...@beckweb.net
Thanks for the extra set of eyes and the tip (sorry my java skills are a bit rusty). The following works as a Build Flow, I tried as a Pipeline but didn't have any luck. As time frees up over the holidays I may revisit.  Thanks again.

def parm = build.buildVariableResolver.resolve("inputParameter").split(/[ ,]+/)
def jobs = [:]

for (String deployment : parm) {
  def inputParameter = deployment
  
  jobs[inputParameter] = {
    build( 'aa', inputParameter: "${inputParameter}")
  }
}

parallel jobs

Daniel Beck

unread,
Dec 13, 2016, 9:37:34 AM12/13/16
to Jenkins Users, Bryce Pepper

> On 13.12.2016, at 15:32, Bryce Pepper <b.a.p...@gmail.com> wrote:
>
> The following works as a Build Flow, I tried as a Pipeline but didn't have any luck.

In this case you're defining a new variable (inputParameter) inside each loop iteration that you pass as argument into build(…). I haven't tried it, but this may well be the relevant difference between the two scripts you showed.

Bryce Pepper

unread,
Dec 13, 2016, 10:18:55 AM12/13/16
to Jenkins Users, b.a.p...@gmail.com, m...@beckweb.net
Reply all
Reply to author
Forward
0 new messages