节流詹金斯在管道中并行 [英] throttling jenkins parallel in pipeline

查看:76
本文介绍了节流詹金斯在管道中并行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中遇到了此消息 在 JENKINS-44085 .

I came across this message with the code below in JENKINS-44085.

如果我已经有一个包含50个项目的分支地图,但是我想一次将它们并行化为5个,那么我该如何修改此代码? 我的代码已经在名为branch的var中具有50个项目的映射.

If I already have a map of branches that contains 50 items, but I want to parallel them 5 at a time, how do I need to modify this code? My code already has a map of 50 items in a var named branches.

// 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
}

推荐答案

这个问题有点老了,但对我来说,昨天这个问题也很重要.在某些情况下,您的Jenkins工作在Jenkins上可能很少,但在某些其他系统上却很重要,因此您想限制该系统的工作.在我看来,每个构建代理使用max executors是不正确的方法,因为如果您的Jenkins集群可扩展,则必须进行调整.

This question is a bit old, but for me the problem was also relevant yesterday. In some cases your Jenkins jobs may be light on Jenkins but high on some other system, so you want to limit it for that system. In my opinion using max executors per build agent is not the right way to do that because if your Jenkins cluster scales you will have to adjust stuff.

要回答您的问题,您可能想要执行以下操作:

To answer your question, you probably want to do something like this:

  • 使用数字索引"0","1"等创建分支地图.
  • 您在该代码的try块中粘贴的内容如下:build(my_branches[name])

至少这是我以前使用相同解决方法的方式.但是后来有人在工作中指出了更好的解决方案.我还在您提到的Jira票证中评论了这个更简单的解决方案.它需要可锁定资源插件: https://wiki.jenkins.io/display /JENKINS/Lockable + Resources + Plugin

At least that's how I was using that same workaround before. But then someone at work pointed out a better solution. I also commented this simpler solution in the Jira ticket you refered to. It requires the Lockable Resources Plugin: https://wiki.jenkins.io/display/JENKINS/Lockable+Resources+Plugin

  • 转到:http://<your Jenkins URL>/configure并添加带有标签"XYZ"的X可锁定资源.
  • 按如下方式在您的代码中使用:
  • Go to: http://<your Jenkins URL>/configure and add X lockable resources with label "XYZ".
  • Use in your code as such:

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带来负担,因此拥有这些全局锁非常方便.

The nice thing about this is that you can use this across different jobs. In our case different jobs have a load on XYZ so having these global locks are very handy.

这篇关于节流詹金斯在管道中并行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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