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

Jenkins scripted pipeline doesn't return all causes for parallel build job failures

我有一个简单的脚本管道如下 -

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 completed with status FAILURE (propagate: false to ignore)

childPipeline1 #1296 completed with status FAILURE (propagate: false to ignore)

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

请 help/guide 给我。

TIA。

动态平行图转轮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()}"}}

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

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