Interview Prep/Jenkins/Scripted pipeline, Groovy & CPS

How do you handle errors in scripted pipeline correctly?

IntermediateConceptScripted pipeline, Groovy & CPS
Answer

Use try/catch/finally, rethrow after handling, and never swallow FlowInterruptedException (user abort or timeout), or the build will keep running after someone clicked Abort.

try {
  sh './deploy.sh'
} catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
  throw e
} catch (err) {
  currentBuild.result = 'FAILURE'
  slackSend channel: '#deploys', message: "Deploy failed: ${err}"
  throw err
} finally {
  sh 'rm -f kubeconfig'
}
All Jenkins questions Roadmap: Jenkins

More on Scripted pipeline, Groovy & CPS