user2988257 user2988257 - 1 year ago 342
Groovy Question

Groovy pipeline continue on fail

Is there's a way to continue execution of the script even if the previous stage was failed? I need to run specific commands (cleanup) in case the build was failed before it fails all the job.

Answer Source

The usual approach is to wrap your steps within a try block.

try {
  sh "..."
} catch (err) {
  echo "something failed"
// cleanup
sh "rm -rf *"

To ease the pain and make the pipeline code more readable, I've encapsulated this in another method here in my global library code.

Another approach, esp. created because of this very issue, are the declarative pipelines (blog, presentation).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download