Groovy 如何在并行任务失败时执行工作流
我有以下工作流程:Groovy 如何在并行任务失败时执行工作流,groovy,jenkins,continuous-integration,jenkins-workflow,Groovy,Jenkins,Continuous Integration,Jenkins Workflow,我有以下工作流程: def flow node('envinf1') { def buildTasks = [:] for(i = 0; i < 2; i++) { buildTasks[i] = { sh 'some command which fails in one of the tasks' } } parallel buildTasks echo 'Some message!' }
def flow
node('envinf1')
{
def buildTasks = [:]
for(i = 0; i < 2; i++) {
buildTasks[i] = {
sh 'some command which fails in one of the tasks'
}
}
parallel buildTasks
echo 'Some message!'
}
def流量
节点('envinf1')
{
def buildTasks=[:]
对于(i=0;i<2;i++){
构建任务[i]={
sh“某个命令在某个任务中失败”
}
}
并行构建任务
回音“一些信息!”
}
当其中一个任务失败时,工作流永远不会到达echo…
-行,而是整个作业失败,出现异常:
org.jenkinsci.plugins.workflow.cps.steps.ParallelStepException:并行步骤0失败
位于org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.checkAllDone(ParallelStep.java:153)
...
是否可以告诉并行-步骤继续执行工作流脚本
buildTasks[i] = {
try {
sh 'some command which fails in one of the tasks'
} catch (e) {
echo "got ${e} but continuing…"
}
}
如果希望构建最终失败,可以使用catchError
步骤
buildTasks[i] = {
catchError {
sh 'some command which fails in one of the tasks'
}
}
或者用手写出等价物
buildTasks[i] = {
try {
sh 'some command which fails in one of the tasks'
} catch (e) {
echo "failed with ${e}"
currentBuild.result = 'FAILURE'
}
}
catchError
确实比手工构建的同类产品有一个优势:它通过打印单行消息来处理AbortException
(例如,来自sh
的非零退出代码),FlowInterruptedException
(例如,用户单击停止按钮)通过打印“abortby…”来处理AbortException消息和设置自定义结果(如中止
),以及通过打印堆栈跟踪执行的所有其他操作