在 Jenkins 中对数组进行简单的并行执行 [英] Simple parallel execution in Jenkins for an array

查看:12
本文介绍了在 Jenkins 中对数组进行简单的并行执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Groovy 运行 Jenkins 作业时遇到问题,这看起来非常简单,但我是 1) Java/Groovy 的菜鸟和 2) 我能找到的所有示例都不是正是我需要做的.

I'm having trouble running a Jenkins job using Groovy, and it seems super simple but I'm 1) a noob at Java/Groovy and 2) all the examples I can find are not quite what I need to do.

我发现了以下类似的问题:Jenkins Groovy 并行变量不工作,但准确 代码导致错误:

I found the following similar question: Jenkins Groovy Parallel Variable not working, but that exact code results in an error:

java.lang.UnsupportedOperationException:调用公共静态java.util.Listorg.codehaus.groovy.runtime.DefaultGroovyMethods.collect(java.lang.Object,groovy.lang.Closure)尚不支持 CPS 转换的闭包 (JENKINS-26481);封装在@NonCPS 方法中,或者使用Java 风格的循环

java.lang.UnsupportedOperationException: Calling public static java.util.List org.codehaus.groovy.runtime.DefaultGroovyMethods.collect(java.lang.Object,groovy.lang.Closure) on a CPS-transformed closure is not yet supported (JENKINS-26481); encapsulate in a @NonCPS method, or use Java-style loops

这是我想要做的伪代码:

Here's my pseudo-code of what I want to do:

String[] arr = [ "one","two","three",'four','five' ]
echo "Running commands: ${arr}"
int top = arr.size()
echo "top is ${top}"
parallel arr.each {
    echo "${it}"
}

我试过闭包,但它们会抛出一个错误,老实说我几乎不知道如何使用它们.以下代码

I've tried closures, but they throw an error and honestly I barely know how to use them. The following code

String[] arr = [ "one","two","three",'four','five' ]
echo "Running commands: ${arr}"

parallel arr.collect { item ->
    { -> 
        echo "${item}"
    }
}

抛出与上述相关问题相同的java.lang.UnsupportedOperationException".

throws the same "java.lang.UnsupportedOperationException" as the related question above.

我正在尝试学习如何在 Jenkins 中正确使用 Groovy,但过程很艰难.只是在寻找我现在可以获得的任何帮助.

I'm trying to learn how to properly utilize Groovy with Jenkins, but it's been rough. Just looking for any help I can get at this point.

谢谢.

推荐答案

这是一个非常烦人的限制,但目前您不能在管道脚本中使用 .each(如此处所述:https://issues.jenkins-ci.org/browse/JENKINS-26481)

It's a pretty annoying limitation, but currently you can't use .each in a pipeline script (as documented here: https://issues.jenkins-ci.org/browse/JENKINS-26481)

你需要做一个实际的循环,比如

You need to do an actual loop, like

String[] arr = [ "one","two","three",'four','five' ]
echo "Running commands: ${arr}"
int top = arr.size()
echo "top is ${top}"
for (it in arr) {
    echo "${it}"
}

如果确实需要并行执行,代码看起来更像:

If actually need parallel execution the code will look more like:

String[] arr = [ "one","two","three",'four','five' ]
echo "Running commands: ${arr}"
int top = arr.size()
echo "top is ${top}"
def stepsForParallel = [:]

for (int i = 0; i < arr.size(); i++) {
    def it = arr[i]
    def stepName = "running ${it}"
    stepsForParallel[stepName] = { ->           
        echo "${it}"
    }
}

parallel stepsForParallel

您链接到的另一个 stackoverflow 使用 Build Flow 插件,它是您正在使用的 Pipeline 插件的前身.这就是为什么相同的代码不起作用.

The other stackoverflow you linked to uses the Build Flow plugin, which is the predecessor to the Pipeline plugin that you're using. That's why the same code doesn't work.

在现代 Jenkins(在 2.150.1 中测试)中,上面列出的原始错误已修复,并且 .each 有效.您可以为并行执行执行以下操作:

In modern Jenkins (tested in 2.150.1) the original bug listed above is fixed, and .each works. You can do the following for parallel execution:

String[] arr = [ "one","two","three",'four','five' ]

def stepsForParallel = [:]
arr.each {
    def stepName = "running ${it}"
    stepsForParallel[stepName] = { ->           
        echo "${it}"
    }
}
parallel stepsForParallel

这篇关于在 Jenkins 中对数组进行简单的并行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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