我想根据参数N从管道“ main_pipeline”并行运行作业“ main_job” N次。但是我无法解决jenkins错误,该错误允许在脚本块中进行“ for”循环,但不允许“平行”块。
我尝试使用脚本/并行/阶段块的所有组合。
pipeline {
agent any
stages {
stage("All jobs") {
parallel {
script {
def numJobs = "${N}" as Integer
for(def curJob=1; curJob<=numJobs; i++) {
def param = "JOB-" + curJob
script {
build (job: "main_job",
parameters: [string(name:"PARAM", value:param)])
}
}
}
}
}
}
}
使用脚本/阶段/并行的不同组合,我会得到关于预期哪个块的不同错误。错误的一个示例:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: xx: Expected a stage @ line xx, column xx.
script {
^
WorkflowScript: xx: Expected one of "steps", "stages", or "parallel" for stage "All jobs" @ line xx, column xx.
stage("All jobs") {
答案 0 :(得分:1)
好的,我终于找到了我需要做的事情,基本上是通过反复试验,并发现通过传递包含所有要运行作业的详细信息的字典,可以将parallel
称为函数。
最终代码如下:
pipeline {
agent any
parameters {
string(
name: 'N',
defaultValue:"2",
description: "The number of jobs to run"
)
}
stages {
stage("All jobs") {
steps {
script {
def numJobs = "${N}" as Integer
def allJobs = [:]
for(def curJob=1; curJob<=numJobs; curJob++) {
def jobName = "JOB-" + curJob
allJobs[jobName] = {
build (job: "main_job",
parameters:
[string(name:"PARAM", value:jobName)])
}
}
parallel(allJobs)
}
}
}
}
}