bitblue
bitblue

Reputation: 21

Jenkins build flow plugin executing selected jobs

I'd like to execute parallel (child) jobs in jenkins based on parent job parameter configuration. Here's my current code with result:

CODE:
jobs2=[]
jobs2.add({build("ChildJob", param1:"nyan")})
jobs2.add({build("ChildJob", param1:"cat")})
out.println(jobs2)
parallel (jobs2)

jobs=[]
testnodes=params["testnodes"].split(' ')
for (testnode in testnodes) {
  out.println("Testnode:" + testnode)
  newjob={build("ChildJob", param1:testnode)}
  out.println("New job:" + newjob)
  jobs.add(newjob)
}
out.println(jobs)

parallel (jobs)

// OUTPUT:
Started by user anonymous
Building in workspace C:\Users\laszlo\.jenkins\workspace\Demo_Main
[Script1$_run_closure1@6b3f6d79, Script1$_run_closure2@112c59f1]
parallel {
    Schedule job ChildJob
    Schedule job ChildJob
    Build ChildJob #36 started
    ChildJob #36 completed 
    Build ChildJob #37 started
    ChildJob #37 completed 
}
Testnode:node1
New job:Script1$_run_closure3@47674a2b
Testnode:node2
New job:Script1$_run_closure3@4e3671ac
Testnode:node3
New job:Script1$_run_closure3@5860fb51
Testnode:node4
New job:Script1$_run_closure3@4713b02b
[Script1$_run_closure3@47674a2b, Script1$_run_closure3@4e3671ac, Script1$_run_closure3@5860fb51, Script1$_run_closure3@4713b02b]
parallel {
    Schedule job ChildJob
    Schedule job ChildJob
    Schedule job ChildJob
    Schedule job ChildJob
    Build ChildJob #38 started
    Build ChildJob #38 started
    Build ChildJob #38 started
    Build ChildJob #38 started
    ChildJob #38 completed 
    ChildJob #38 completed 
    ChildJob #38 completed 
    ChildJob #38 completed 
}
Finished: SUCCESS

Why is it starting only one job, how can I run "number of nodes" seperate jobs as in the first half?

Upvotes: 1

Views: 1009

Answers (1)

bitblue
bitblue

Reputation: 21

Actually i found a way to solve the problem and post it, maybe it will help somebody. Here's a dummy solution:

ArrayList<String> examplestrings = new ArrayList<String>()
examplestrings.add("A")
examplestrings.add("B")
examplestrings.add("C")
examplestrings.add("D")
examplestrings.add("E")
def jobs = []
for (int i = 0 ; i < 5; i++) {
    def closure = {
        def element = ""
        synchronized (examplestrings) {
            element = examplestrings.get(0)
            examplestrings.remove(element)
        }
        build("ChildJob", param1:element)
    }
    jobs.add(closure)
}

parallel (jobs)

Because of each job will be the same inside and behave the same way i created a list from where they can fetch the different values to work with.

Upvotes: 1

Related Questions