我在下面的代码中看到了这条消息 在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
}
答案 0 :(得分:1)
这个问题有点老了,但是对我来说,这个问题昨天也很重要。在某些情况下,您的Jenkins工作在Jenkins上可能很少,但在某些其他系统上却很重要,因此您想限制该系统的工作。在我看来,每个构建代理使用max executors是不正确的方法,因为如果您的Jenkins集群可扩展,则必须进行调整。
要回答您的问题,您可能想要执行以下操作:
build(my_branches[name])
至少我以前就是这样使用这种解决方法的。但是后来有人在工作中指出了更好的解决方案。我还在您提到的Jira票证中评论了这个更简单的解决方案。它需要可锁定资源插件:https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin
http://<your Jenkins URL>/configure
并添加带有标签“ XYZ”的X个可锁定资源。def tests = [:] for (...) { def test_num="$i" tests["$test_num"] = { lock(label: "XYZ", quantity: 1, variable: "LOCKED") { println "Locked resource: ${env.LOCKED}" build(job: jobName, wait: true, parameters: parameters) } } } parallel tests
对此的好处是您可以在不同的作业中使用它。在我们的情况下,不同的作业会对XYZ产生负担,因此拥有这些全局锁非常方便。