jenkins - 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: 126
在childPipeline2: 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()}"}}
解决方案
您可以在并行执行的每个分支中捕获错误:
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()}"
}
}
推荐阅读
- javascript - 使用 jQuery 对包含组的表进行排序
- c - 这个问题的正确输出是什么?
- laravel - 是运行像 Laravel 调度程序这样的内部 cron 服务还是使用像 Easycron 这样的外部服务更好
- powershell - Outlook 和 Powershell
- python-3.x - 箱线图matplotlib的胡须大小
- java - 具有不同文件名的 springboot 配置外部化
- python - 将 Python 中的 Excel 文件读入内存并将工作表传递给 Pandas
- html - Wordpress 从视频中剥离 HTML5 Playsinline
- c++ - 比较嵌套 for 循环中的两个 wchar 数组?
- css - webpack encore 构建错误中的错误:模块构建失败:未关闭的块