问题
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