使用工作流插件同时启动作业多次 [英] Start a job multiple times concurrently with workflow plugin

查看:23
本文介绍了使用工作流插件同时启动作业多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用工作流插件同时运行一项作业 5 次.这是片段:

I am trying to run one job 5 times concurrently using the workflow plugin. here is the snippet:

def concurrent=[:]
for (int i = 0; i < 5; i++) {
concurrent["concurrent${i}"] = {
build job: 'test_job', parameters: [[$class: 'StringParameterValue', name:'queue', value:     
'automation_prod.q'],[$class: 'StringParameterValue', name:'dummy', value: "${i}"]]
    }
}
parallel concurrent

这个片段导致 test_job 只运行一次.我需要它同时运行 5 次.

this snippet causes the the test_job to run only once. I need it running 5 times concurrently.

谢谢!

推荐答案

除了缺乏对脚本错误的诊断之外,Workflow 中没有其他错误.在 Groovy 中,循环计数器 i 被定义在一个封闭的作用域中并发生了变化,所以当每个闭包运行时,它具有相同的值:5.您可以看到这一点,以及背后的概念修复,在 Jenkins 之外:

There is no bug in Workflow here beyond a lack of diagnosis for the mistake in the script. In Groovy, the loop counter i is defined in an enclosing scope and mutated, so by the time each closure runs, it has the same value: 5. You can see this, as well as the concept behind the fix, outside Jenkins:

$ groovy -e 'def cs = []; for (int i = 0; i < 5; i++) {def j = i; cs += {println "${i} vs. ${j}"}}; for (c in cs) {c()}'
5 vs. 0
5 vs. 1
5 vs. 2
5 vs. 3
5 vs. 4

在您的情况下,Jenkins 看到了五次尝试使用相同的参数来安排同一个下游项目,并将它们全部合并到一个队列项中,从而合并到一个构建中.(根据时间,它可能在其他 build 步骤甚至运行之前就开始了一个下游构建,在这种情况下,它会运行第二个下游构建,但通常总共少于五个构建.)

In your case, Jenkins saw five attempts to schedule the same downstream project with the same parameters, and coalesced them all into one queue item and thus one build. (Depending on timing, it might have started one downstream build before other build steps even ran, in which case it would run a second downstream build, but generally it would be fewer than five total builds.)

这个新测试表明您想要做的确实是可能的;你只需要在闭包外的一个新的词法范围变量中捕获循环变量的当前值.

This new test demonstrates that what you wanted to do is indeed possible; you just need to capture the current value of the loop variable in a new lexically-scoped variable outside the closure.

顺便说一下

def cs = []; (0..4).each {i -> cs += {println i}}; cs*.call()

确实按命令行 Groovy 的预期工作,因为没有变异的循环变量.不幸的是,此语法在 Workflow 中尚不可用:JENKINS-26481

does work as expected from command-line Groovy, since there is no mutated loop variable. Unfortunately this syntax is not yet available in Workflow: JENKINS-26481

请注意,您不应该不要使用虚拟"参数值来区分队列条目.虽然今天可能会发生这种情况,但希望该行为得到修复,以便 build 指定的参数值与下游项目中实际定义的任何参数不对应,或者会被跳过并显示警告,或导致错误.如果您认为您需要一个虚拟参数,那么您可能做错了其他一些事情,但是如果没有任何解释为什么您希望下游项目在其输入中没有区分构建的情况下多次运行,则不可能提出建议.

Beware that you should not be using a "dummy" parameter value to differentiate queue entries. While this may happen to work today, expect that behavior to be fixed, so that parameter values specified by build which do not correspond to any parameter actually defined in the downstream project will either be skipped with a warning, or result in an error. If you think you need a dummy parameter, you are probably doing something else wrong, but it is impossible to advise what that is without any explanation of why you would want a downstream project to run multiple times with nothing in its input differentiating the builds.

这篇关于使用工作流插件同时启动作业多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆