首页 > 解决方案 > 在 Groovy 中同时运行 FOR LOOP?

问题描述

我正在尝试找到可以在 Groovy 中同时运行 FOR 循环的选项?我有一个 Jenkins 文件,我希望 for 循环在那里并行运行

类似于以下代码。我希望所有这些 for 循环都可以并行运行。订单不是问题。只是for循环里面的步骤应该已经完成​​了。

vars=[*,*,*,*,*]

for i in vars:
        '''steps'''

标签: multithreadingfor-loopgroovyjenkins-pipelinejenkins-groovy

解决方案


您需要使用并行指令。这就是你可以为 for 循环做的事情。您应该能够使用此代码并使其适应您的

def testList = ["a", "b", "c", "d"]
def branches = [:] 

for (int i = 0; i < 4 ; i++) {
       int index=i, branch = i+1
            branches["branch_${branch}"] = { 
                    sh "echo 'node: ${NODE_NAME},  index: ${index}, i: ${i}, testListVal: " + testList[index] + "'"   
      }
}

parallel branches

推荐阅读