Jenkins流水线 - 并行构建同一个作业多次

3

我正在尝试在Jenkins中创建一个流水线(pipeline),可以在不同的节点(agents)上多次触发同一个作业(job)。

我已经在Jenkins中配置了名为“Create_Invoice”的作业,选择了“(Execute Concurrent builds if necessary)”选项。如果我点击10次“构建(Build)”按钮,它将在不同的(可用)代理程序/节点上运行10次。

与其手动点击10次,我想创建一个并行流水线(parallel pipeline)。

我创建了如下内容——它触发了该作业,但只运行了一次。 我错过了什么或者从流水线(pipeline)中是否有可能同时多次触发同一个测试(test)?

提前感谢你。

node {
    def notifyBuild = { String buildStatus ->
        // build status of null means successful
        buildStatus =  buildStatus ?: 'SUCCESSFUL'
        // Default values
 def tasks = [:]
    try { 
tasks["Test-1"] = {  
    stage ("Test-1") {
        b = build(job: "Create_Invoice",  propagate: false).result       
   }
 }  
tasks["Test-2"] = {    
   stage ("Test-2") {
        b = build(job: "Create_Invoice",  propagate: false).result
        }
}    
 parallel tasks   
       } catch (e) {
        // If there was an exception thrown, the build failed
        currentBuild.result = "FAILED"
        throw e
    }
      finally {
          notifyBuild(currentBuild.result)
    }   
}
}

你想在同一个代理程序中运行任务10次,还是在每个代理程序中运行10次? - Samit Kumar Patel
3个回答

3

我曾遇到同样的问题,通过向同一作业传递不同的参数来解决。您应该在构建步骤中添加参数,尽管显然您不需要它们。例如,我添加了一个字符串参数。

tasks["Test-1"] = {  
  stage ("Test-1") {
    b = build(job: "Create_Invoice",  parameters: [string(name: "PARAM", value: "1")], propagate: false).result       
  }
}  
tasks["Test-2"] = {    
  stage ("Test-2") {
    b = build(job: "Create_Invoice", parameters: [string(name: "PARAM", value: "2")], propagate: false).result
  }
}

只要向相同的作业传递相同的参数或不传递任何参数,该作业只会被触发一次。
另请参阅Jenkins的这个问题,它描述了相同的问题:
https://issues.jenkins.io/browse/JENKINS-55748

0

这个例子将从Jenkins中获取可用的代理,迭代并在所有活动代理中运行流水线。

通过这种方法,您无需多次从上游作业调用此作业以在不同代理上构建。该作业本身将管理一切,并在所有在线节点中运行定义的所有阶段。


jenkins.model.Jenkins.instance.computers.each { c ->
    
    if(c.node.toComputer().online) {
        node(c.node.labelString) {
            stage('steps-one') {
                echo "Hello from Steps One"
            }
            stage('stage-two') {
                echo "Hello from Steps Two"
            }
        }
    } else {
        println "SKIP ${c.node.labelString} Because the status is : ${c.node.toComputer().online} "
    }
    
}    


0

谢谢!我被其他项目分心了,还没有尝试过,但我会在几天内更新结果。 - Mochi79

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接