并行任务失败时如何继续工作流程

时间:2015-08-27 12:34:57

标签: 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!'
}

当其中一个任务失败时,工作流程永远不会到达echo ...行,而是整个作业失败并出现异常:

org.jenkinsci.plugins.workflow.cps.steps.ParallelStepException: Parallel step 0 failed at org.jenkinsci.plugins.workflow.cps.steps.ParallelStep$ResultHandler$Callback.checkAllDone(ParallelStep.java:153) ...

是否可以告诉parallel步骤继续使用工作流脚本?

1 个答案:

答案 0 :(得分:2)

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的非零退出代码){{1 (例如,用户单击停止按钮)通过打印“aborted by ...”消息并设置自定义结果(如FlowInterruptedException),以及通过打印堆栈跟踪来完成所有其他操作。

相关问题