简体   繁体   English

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

[英]Jenkins Pipeline - build same job multiple times in parallel

I am trying to create a pipeline in Jenkins which triggers same job multiple times in different node(agents).我正在尝试在 Jenkins 中创建一个管道,它在不同的节点(代理)中多次触发相同的作业。

I have "Create_Invoice" job Jenkins, configured: (Execute Concurrent builds if necessary) If I click on Build 10 times it will run 10 times in different (available) agents/nodes.我有“Create_Invoice”作业 Jenkins,已配置:(如有必要,执行并发构建)如果我单击构建 10 次,它将在不同(可用)代理/节点中运行 10 次。

Instead of me clicking 10 times, I want to create a parallel pipeline.我不想点击 10 次,而是想创建一个并行管道。

I created something like below - it triggers the job but only once.我创建了类似下面的东西 - 它触发了工作但只有一次。 What Am I missing or is it even possible to trigger same test more than once at the same time from pipeline?我错过了什么,或者是否有可能同时从管道中多次触发相同的测试?

Thank you in advance先感谢您

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

I had the same problem and solved it by passing different parameters to the same job.我有同样的问题并通过将不同的参数传递给同一个作业来解决它。 You should add parameters to your build steps, although you obviously don't need them.您应该在构建步骤中添加参数,尽管您显然不需要它们。 For example, I added a string parameter.例如,我添加了一个字符串参数。

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

As long as the same parameters or no parameters are passed to the same job, the job is only tirggered once.只要相同的参数或没有参数传递给同一个作业,该作业只会被触发一次。

See also this Jenkins issue, it describes the same problem:另请参阅此 Jenkins 问题,它描述了相同的问题:
https://issues.jenkins.io/browse/JENKINS-55748 https://issues.jenkins.io/browse/JENKINS-55748

I think you have to switch to Declarative pipeline instead of Scripted pipeline.我认为您必须切换到声明式管道而不是脚本式管道。

Declarative pipeline has parallel stages support which is your goal:声明式管道具有并行阶段支持,这是您的目标:

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

This example will grab the available agent from the Jenkins and iterate and run the pipeline in all the active agents.此示例将从 Jenkins 中获取可用代理,并在所有活动代理中迭代和运行管道。

with this approach, you no need to invoke this job from an upstream job many time to build on a different agent.使用这种方法,您无需多次从上游作业调用此作业来构建不同的代理。 This Job itself will manage everything and run all the stages define in all the online node.该作业本身将管理所有内容并运行所有在线节点中定义的所有阶段。


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在 jenkins 管道的并行阶段运行相同的 jenkins 构建作业 - Is it possible to run a same jenkins build job in parallel stages in jenkins pipeline 在声明性管道中并行在多个代理上运行相同的 Jenkins 作业 - Running same Jenkins job on multiple agents in parallel in declarative pipeline jenkins 是否可以并行多次运行同一个作业? - Does jenkins can run the same job multiple times in parallel? 如何与Jenkins并行多次运行相同的作业? - How to run the same job multiple times in parallel with Jenkins? jenkins 管道捕获失败并行构建的 build_job 信息 - jenkins pipeline catch build_job info for a failed parallel build 詹金斯为多个分支机构管道相同的工作 - Jenkins pipeline same job for multiple branches 詹金斯管道工作机会 - Jenkins Pipeline Job Build 使用声明性管道在不同的存储库上并行运行相同的 Jenkins 作业 - Run same Jenkins job on different repositories in parallel using Declarative Pipeline Jenkins Pipeline - 在多个远程主机上并行运行作业 - Jenkins Pipeline - Run job on parallel on multiple remote hosts 如何根据管道参数值并行运行作业多次 - How to run a job multiple times in parallel depending on the pipeline parameter value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM