Jenkins ‘Build back to normal’

Yesterday, I finished porting our Jenkinsfile to use the new Declarative syntax. It makes the flow of processing a lot more straightforward and it’s great for handling errors and post actions. However getting everything to work again was tricky!

I was looking to send an e-mail and Office365 notification when a build returns to normal. Others updated the status of the current build during their steps, as seen on stackoverflow and here. I managed to do it slightly different without having to manage the current state;

pipeline {
  agent any
  post {
    success {
      script {
        if (currentBuild.getPreviousBuild().getResult().toString() != "SUCCESS") {
          echo 'Build is back to normal!'
        }
      }
    }
  }
}

For more details on the syntax of declarative pipelines, I’d recommend this site.