Jenkins声明式管道获取构建失败原因

时间:2020-05-14 20:24:11

标签: jenkins jenkins-pipeline jenkins-plugins jenkins-declarative-pipeline

我最近将脚本化管道转换为声明性管道,但是在 post 部分中无法获取构建失败案例。

对于脚本化管道,我可以轻松地将管道包装在 try-catch 中,并可以访问异常对象。但不适用于这样的声明性管道:

pipeline {
    stages {
        ...
    }
    post{
        failure {
            script {
                //this is where i need the failure exception detail
                handleFailure()
            }
        }
    }
}

我不确定该怎么做,我正在尝试 getContext()方法,但它返回 null 。 感谢任何建议。

1 个答案:

答案 0 :(得分:0)

//proceed to next stage based on the currentBuild.result    
def currentBuild.result='';
    pipeline
    {
        stages
        {
           stage('example')
           {
               steps
               {
                   script
                   {
                     try
                     {
                       \\ render the code here
                         currentBuild.result='SUCCESS'
                     }
                     catch(Exception ex)
                     {
                         println(ex)
                         currentBuild.result='FAILURE'
                     }
                   }
               }
           }
        }
    }