Jenkins 脚本化管道不会返回并行构建作业失败的所有原因

时间:2021-03-28 03:12:41

标签: jenkins jenkins-pipeline jenkins-groovy

我有一个简单的脚本化管道,如下所示 -

import hudson.model.*
import groovy.json.*

def map = [:]
map['second'] = {
    build job: "childPipeline1", propagate: true, wait: true
}

map['third'] = {
    build job: "childPipeline2", propagate: true, wait: true
}

try {    
    parallel map   
}catch(Exception e) {
    e.getCauses().each {
        echo "${it.getShortDescription()}"
     }
}

实际的地图创建代码是动态的,看起来像这样 -

map["${substage}_${subdir}"] = {build job: "ci_pipeline_${substage}${jobSuffix}", parameters: downstreamParams[subdir], propagate: true}

当我执行此操作时,我预计第 2 阶段和第 3 阶段会失败(模拟),当我捕获异常时,我会得到一个 org.jenkinsci.plugins.workflow.steps.FlowInterruptedException只有一个 原因列表中的原因元素 -

[Pipeline] Start of Pipeline
[Pipeline] parallel
[Pipeline] { (Branch: second)
[Pipeline] { (Branch: third)
[Pipeline] build (Building childPipeline1)
Scheduling project: childPipeline1
[Pipeline] build (Building childPipeline2)
Scheduling project: childPipeline2
Starting building: childPipeline1 #1333
Starting building: childPipeline2 #148
[Pipeline] }
Failed in branch third
[Pipeline] }
Failed in branch second
[Pipeline] // parallel
[Pipeline] echo
childPipeline2 #148 completed with status FAILURE (propagate: false to ignore)
[Pipeline] End of Pipeline
Finished: SUCCESS

但是,当我抛出异常,即 throw e 时,我可以在 Jenkins Web 控制台日志中看到所有失败的并行作业,如下所示 -

[Pipeline] Start of Pipeline
[Pipeline] parallel
[Pipeline] { (Branch: second)
[Pipeline] { (Branch: third)
[Pipeline] build (Building childPipeline1)
Scheduling project: childPipeline1
[Pipeline] build (Building childPipeline2)
Scheduling project: childPipeline2
Starting building: childPipeline1 #1296
Starting building: childPipeline2 #126
[Pipeline] }
Failed in branch third
[Pipeline] }
Failed in branch second
[Pipeline] // parallel
[Pipeline] End of Pipeline
childPipeline2 #126 completed with status FAILURE (propagate: false to ignore)
childPipeline1 #1296 completed with status FAILURE (propagate: false to ignore)
Finished: FAILURE

我正在尝试获得结果 -

<块引用>

childPipeline2 #126 完成,状态为 FAILURE(传播:false 忽略)

childPipeline1 #1296 完成,状态为 FAILURE(传播:false 忽略)

并仅提取失败的管道名称和相应的内部版本号,例如 childPipeline1: 126childPipeline2: 1296 在 Jenkins 网络控制台日志中即,所有原因;本例中为 2)到我的脚本管道中的一个变量中。 >

请帮助/指导我。

TIA。

Dynamic parallel map runner impl(基于@Yuri G 的回答)如下 -

map["${substage}_${subdir}"] = { try { build job: "ci_pipeline_${substage}${jobSuffix}", parameters: downstreamParams[subdir], propagate: true }catch(Exception e) { e.getCauses().each {echo "${it.getShortDescription()}"}}

1 个答案:

答案 0 :(得分:0)

您可以在并行执行的每个分支中捕获错误:

import hudson.model.*
import groovy.json.*

def map = [:]
map['second'] = {
  try {
    build job: "childPipeline1", propagate: true, wait: true    
  }catch(Exception e) {
    e.getCauses().each {
      echo "${it.getShortDescription()}"
    }
    throw e
  }

}

map['third'] = {
  try{
    build job: "childPipeline2", propagate: true, wait: true
  }catch(Exception e) {
     e.getCauses().each {
       echo "${it.getShortDescription()}"
     }
  throw e
  }
}

try {    
  parallel map   
}catch(Exception e) {
  e.getCauses().each {
    echo "${it.getShortDescription()}"
  }
}