How can I react to a build failure in pipeline?

98 views
Skip to first unread message

Scott Richmond

unread,
Apr 12, 2016, 6:25:44 PM4/12/16
to Jenkins Users
I need to be able to do a HTTP POST when the Jenkins Pipeline job finishes in failure due to some issue in the process of doing its tasks. Basically I need to surround the pipeline in a giant try-catch{}. Does pipeline provide functionality to perform final tasks in the event of a failure?

thomas....@teamaol.com

unread,
Apr 13, 2016, 7:57:05 AM4/13/16
to Jenkins Users
Hi,

It took me a while to find it but it looks like this is the approach
which is also documented ...

... you can find the relating documentation when you use the "Snippet Generator"
looking for the entry "catchError" and then clicking one the question mark.

The final implementation of course depends on your needs. If you wanna stop
the pipeline but doing the cleanup then next example is probably one way to go.
But you also could do a kind of cleanup and proceed with next stage when this
is possible in your case.

A bit unfortune - I say - that such things are not documented here: https://jenkins.io/doc/pipeline/

Regards,
Thomas

node {
    try {
        stage "One"
        sh "rm /tmp/file_does_not_exist"
    } catch (all) {
        // doing some cleanup for stage one
        throw new Exception("Stage One failed!")
    } 
    
    try {
        stage "Two"
        echo "hello world!"
    } catch (all) {
        // doing some cleanup for stage two
        throw new Exception("Stage Two failed!")
    } 
}


Reply all
Reply to author
Forward
0 new messages