user2988257 user2988257 - 1 month ago 52
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

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).