Shell 是否可以将groovy中的失败阶段与管道中的try-catch结合起来?

Shell 是否可以将groovy中的失败阶段与管道中的try-catch结合起来?,shell,groovy,jenkins-pipeline,try-catch,Shell,Groovy,Jenkins Pipeline,Try Catch,我有一个带有一些阶段和逻辑的管道。唯一可能的调试器是向每个阶段添加try-catch: stage ("distribution"){ steps{ script{ try{ amd_distribution_distribute_bundle } catch (Exception e) {

我有一个带有一些阶段和逻辑的管道。唯一可能的调试器是向每个阶段添加try-catch:

stage ("distribution"){
            steps{
                script{
                    try{
    amd_distribution_distribute_bundle
                    }
                   catch (Exception e) {
                    echo e.toString()
                    }
            }
        }
    }
但是,如果一个阶段失败,我需要停止管道:

status = sh script: '''
                set +x
                python3.4 main.py --command create_bundle
                ''',  returnStatus:true
             }
        }
     }

    if (status == 1) {
    error("returning 1 from func")
    }

但是管道中的try-catch实际上会取消它(它会继续到其他阶段。是否有方法将它们组合起来?

可以使用
catchError
步骤来代替try/catch。如果该步骤引发错误,可以使用
buildResult
stagerResult
参数来设置生成结果:

catchError (stageResult: "FAILURE") {
    sh script: # Your script here
}

请参见

而不是
错误
,从函数返回一个值。检查try/catch之外的值,然后根据需要检查
错误