Jenkins构建流插件执行选定的作业

时间:2014-01-26 15:43:26

标签: jenkins jenkins-plugins

我想基于父作业参数配置在jenkins中执行并行(子)作业。 这是我当前的结果代码:

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

为什么它只启动一项工作,如何像上半年那样运行“节点数”单独的工作?

1 个答案:

答案 0 :(得分:1)

实际上我找到了一种方法来解决问题并发布它,也许它会帮助某人。这是一个虚拟解决方案:

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)

因为每个作业内部都是相同的,并且行为方式相同,我创建了一个列表,从中可以获取要使用的不同值。