首页 > 解决方案 > Jenkins 脚本化管道不会返回导致并行构建作业失败的所有原因

问题描述

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

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 Web 控制台日志中即,所有原因;在本例中为 2)到我的脚本管道中的变量中。

请帮助/指导我。

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

标签: jenkinsjenkins-pipelinejenkins-groovy

解决方案


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

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

推荐阅读