ris*_*ney 5 groovy jenkins jenkins-pipeline
我在JENKINS-44085中发现了这条带有以下代码的消息。
如果我已经有一个包含 50 个项目的分支图,但我想一次并行 5 个项目,我需要如何修改此代码?我的代码已经在名为branches 的 var 中包含了 50 个项目的映射。
// put a number of items into the queue to allow that number of branches to run
for (int i=0;i<MAX_CONCURRENT;i++) {
latch.offer("$i")
}
for (int i=0; i < 500; i++) {
def name = "$i"
branches[name] = {
def thing = null
// this will not allow proceeding until there is something in the queue.
waitUntil {
thing = latch.pollFirst();
return thing != null;
}
try {
echo "Hello from $name"
sleep time: 5, unit: 'SECONDS'
echo "Goodbye from $name"
}
finally {
// put something back into the queue to allow others to proceed
latch.offer(thing)
}
}
}
timestamps {
parallel branches
}
Run Code Online (Sandbox Code Playgroud)
这个问题有点老了,但对我来说这个问题昨天也相关。在某些情况下,您的 Jenkins 工作可能在 Jenkins 上较少,但在某些其他系统上较多,因此您希望限制该系统的工作量。在我看来,为每个构建代理使用最大执行器并不是正确的方法,因为如果你的 Jenkins 集群扩展,你将不得不调整内容。
为了回答你的问题,你可能想做这样的事情:
build(my_branches[name])至少这就是我之前使用相同解决方法的方式。但后来工作中有人指出了更好的解决方案。我还在您提到的 Jira 票证中评论了这个更简单的解决方案。它需要可锁定资源插件:https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin
http://<your Jenkins URL>/configure并添加带有标签“XYZ”的 X 个可锁定资源。def 测试 = [:]
为了 (...) {
def test_num=“$i”
测试[“$test_num”] = {
锁(标签:“XYZ”,数量:1,变量:“锁定”){
println "锁定资源:${env.LOCKED}"
构建(作业:作业名称,等待:true,参数:参数)
}
}
}
并行测试
这样做的好处是您可以在不同的工作中使用它。在我们的例子中,不同的作业在 XYZ 上有负载,因此拥有这些全局锁非常方便。
| 归档时间: |
|
| 查看次数: |
3629 次 |
| 最近记录: |