Jenkins Declarative pipeline get build failure cause

耗尽温柔 提交于 2020-05-17 05:51:10

问题


I recently converted a scripted pipeline into a declarative pipeline but having trouble to get the build failure case in the post section.

For a scripted pipeline, I can easily wrap the pipeline inside a try-catch and have access to the exception object. But not for declarative pipeline like this:

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

Im not sure how to do that, I'm trying getContext() method but it return null . Appreciate any suggestion.


回答1:


//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'
                     }
                   }
               }
           }
        }
    }


来源:https://stackoverflow.com/questions/61806764/jenkins-declarative-pipeline-get-build-failure-cause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!