0

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.

nandilov
  • 699
  • 8
  • 18
Bob T.
  • 43
  • 1
  • 7
  • Voting to close as duplicate as there are already too many questions like this. I've moved my answer [here](https://stackoverflow.com/a/61837364/7571258). – zett42 May 16 '20 at 13:17

1 Answers1

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