如何并行执行从另一个 Groovy 文件导入的列表?

How to parelelly execute a list imported from another Groovy file?

我正在从另一个 groovy 文件导入一个列表,并利用它遍历内容:

First.groovy

def getContent() {
    def content = ["one", "two"]
    return content
}

return this;

并这样称呼它:

def first = load 'First.groovy'
def content = first.getContent()

stage("Build services") {
              parallel content { CONTENT_NAME ->
                 [CONTENT_NAME, {
                     //iterate through each job
                     try { 
                        some code;  
                       } 
                    catch (err) {
                         echo err.toString()
                         currentBuild.result = 'FAILURE'
                         throw err
                     }
                 }]
             }

但我收到错误 -

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.call() is applicable for argument types: (org.jenkinsci.plugins.workflow.cps.CpsClosure2) values: 
[org.jenkinsci.plugins.workflow.cps.CpsClosure2@23856748]
Possible solutions: tail(), tail(), wait(), last(), last(), any()

有解决这个问题的线索吗?

parallel: Execute in parallel Takes a map from branch names to closures

您需要先创建一个地图,然后根据 parallel 执行它。您的 content 只是一个列表。

我建议使用不同的方法构建任务:

def parallelTasks = [:]
for (element in content) {
    parallelTasks["${element}"] = {
      echo "${element}"
  }
}
parallel parallelTasks