Jenkins 管道 - 多次并行构建相同的作业

时间:2021-02-23 04:22:10

标签: jenkins jenkins-pipeline

我正在尝试在 Jenkins 中创建一个管道,该管道在不同的节点(代理)中多次触发相同的作业。

我有“Create_Invoice”作业 Jenkins,已配置:(如有必要,执行并发构建) 如果我点击 Build 10 次,它将在不同的(可用)代理/节点中运行 10 次。

我想创建一个并行管道,而不是点击 10 次。

我创建了类似下面的东西 - 它触发了工作,但只触发一次。 我错过了什么,或者甚至可以从管道同时多次触发相同的测试?

提前致谢

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)
    }   
}
}

2 个答案:

答案 0 :(得分:0)

我认为您必须切换到声明式管道而不是脚本式管道。

声明式管道具有并行阶段支持,这是您的目标:

https://www.jenkins.io/blog/2017/09/25/declarative-1/

答案 1 :(得分:0)

此示例将从 Jenkins 中获取可用代理,并在所有活动代理中迭代和运行管道。

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


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} "
    }
    
}