1

我有以下工作流程:

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 ...-line,而是整个作业失败并出现异常:

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-step 继续使用工作流脚本?

4

1 回答 1

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(例如,来自 的非零退出代码),通过打印“被...中止”消息并设置自定义结果(如),以及通过打印堆栈跟踪来完成所有其他操作。shFlowInterruptedExceptionABORTED

于 2015-08-28T12:19:54.660 回答