使用jenkins中的循环构建流程插件的并行作业 [英] Parallel jobs using build flow plugin with loop in jenkins
问题描述
我使用构建流程插件构建jenkins流程,该流程插件将抓取所有作业,将该名称与正则表达式进行比较,如果匹配,则会触发作业构建。
I完美的工作:
import jenkins.model.Jenkins
import java.util.regex。*
模式myRegex =〜/ release_status。* /
for(item in jenkins.model.Jenkins.instance.items)
{
if(item.name ==〜myRegex){
build($ item.name)
}
}
然而,这需要很长时间来构建所有匹配的作业(现在有20个,但可能还有更多)。
我试图让这个并行运行每个作业,但是我可以' t找出groovy语法。
我已经尝试了3种不同的方式:
import jenkins.model。 Jenkins
import java.util.regex。*
Pattern myRegex =〜/ release_status。* /
parallel(
{
for(item in jenkins.model.Jenkins .insta (item.name ==〜myRegex){
build($ item.name)
}
} $ b $
{
if b}
)
^^这仍然有效,但它与以前一样。它一次只能完成一项工作,在前一项完成之前不会构建下一项工作。
import jenkins.model.Jenkins
import java.util.regex。*
Pattern myRegex =〜 /release_status.*/
parallel(
{
for(item in jenkins.model.Jenkins.instance.items)
{
if(item.name == 〜myRegex){
{build($ item.name)},
}
}
}
)
^^与
Script1错误。 groovy:9:不明确的表达式可能是无参数闭包表达式或孤立的开放代码块;
解决方案:添加一个显式的闭包参数列表,例如{it - > ...},或者通过给它一个标签来强制它被当作一个空白块。 L:{...} @第9行第9列。
{build($ item.name)},
break
import jenkins.model.Jenkins
import java.util.regex 。*
模式myRegex =〜/ release_status。* /
parallel(
[
for(item in jenkins.model.Jenkins.instance.items)
{
if(item.name ==〜myRegex){
无用:{build($ item.name)},
}
}
]
)
休息
import jenkins.model.Jenkins
import java.util.regex。*
Pattern myRegex =〜/ release_status。* /
parallel(
for(item在jenkins.model.Jenkins.instance.items)
{
if(item.name ==〜myRegex){
{build($ item.name)},
}
}
)
:
org.codehaus.groovy.control.MultipleCompilationErrorsE xception:启动失败:
Script1.groovy:5:意外标记:for @ line 5,column 5.
for(item in jenkins.model.Jenkins.instance.items)
这里有很多代码,但相当简单。环顾四周,我无法在groovy上找到很多优秀的资源。
collect
返回一个列表: import jenkins.model。 Jenkins
import java.util.regex。*
模式myRegex =〜/ release_status。* /
并行jenkins.model.Jenkins.instance.items.collect {item - > ;
{ - >
if(item.name ==〜myRegex){
build($ item.name)
}
}
}
如果名称通过,只返回一个Closure(而不是每个项目的闭包,其中很多项目会提前结束)是:
import jenkins.model.Jenkins
import java.util.regex。*
Pattern myRegex =〜/ release_status。* /
parallel Jenkins.instance.items.findAll {item - > item.name ==〜myRegex}
.collect {item - > { - > build($ item.name)}}
I am building a jenkins flow using the build flow plugin that will grab all of the jobs, compare the name to a regex, and if it matches it will trigger a build of the job.
I have this working perfectly:
import jenkins.model.Jenkins
import java.util.regex.*
Pattern myRegex = ~/release_status.*/
for (item in jenkins.model.Jenkins.instance.items)
{
if (item.name ==~ myRegex) {
build( "$item.name" )
}
}
However this takes a very long time to build all of the matching jobs (right now there are 20 but there could be many more).
I am trying to make this run each job in parallel but I can't figure out the groovy syntax.
I have tried 3 different ways:
import jenkins.model.Jenkins
import java.util.regex.*
Pattern myRegex = ~/release_status.*/
parallel (
{
for (item in jenkins.model.Jenkins.instance.items)
{
if (item.name ==~ myRegex) {
build( "$item.name" )
}
}
}
)
^^This still works, however it works the same way as before. It goes one job at a time and does not build the next until the previous one finishes.
import jenkins.model.Jenkins
import java.util.regex.*
Pattern myRegex = ~/release_status.*/
parallel (
{
for (item in jenkins.model.Jenkins.instance.items)
{
if (item.name ==~ myRegex) {
{ build( "$item.name" ) },
}
}
}
)
^^This errors with
Script1.groovy: 9: Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
solution: Add an explicit closure parameter list, e.g. {it -> ...}, or force it to be treated as an open block by giving it a label, e.g. L:{...} @ line 9, column 9.
{ build( "$item.name" ) },
break
import jenkins.model.Jenkins
import java.util.regex.*
Pattern myRegex = ~/release_status.*/
parallel (
[
for (item in jenkins.model.Jenkins.instance.items)
{
if (item.name ==~ myRegex) {
useless: { build( "$item.name" ) },
}
}
]
)
break
import jenkins.model.Jenkins
import java.util.regex.*
Pattern myRegex = ~/release_status.*/
parallel (
for (item in jenkins.model.Jenkins.instance.items)
{
if (item.name ==~ myRegex) {
{ build( "$item.name" ) },
}
}
)
Both of the blocks above error with the following:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 5: unexpected token: for @ line 5, column 5.
for (item in jenkins.model.Jenkins.instance.items)
A lot of code here but its fairly simple. Looking around I can't find many good resources on groovy.
parallel
takes a list of Closures, so you should be able to use collect
to return a list:
import jenkins.model.Jenkins
import java.util.regex.*
Pattern myRegex = ~/release_status.*/
parallel jenkins.model.Jenkins.instance.items.collect { item ->
{ ->
if (item.name ==~ myRegex) {
build( "$item.name" )
}
}
}
An alternative that only returns a Closure if the name passes (rather than a Closure for every item, a lot of which will finish early) is:
import jenkins.model.Jenkins
import java.util.regex.*
Pattern myRegex = ~/release_status.*/
parallel Jenkins.instance.items.findAll { item -> item.name ==~ myRegex}
.collect { item -> { -> build("$item.name") } }
这篇关于使用jenkins中的循环构建流程插件的并行作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!