How to send notifications in Jenkins pipeline when build recovers?

I've found another working solution, which doesn't require you to manually keep track of your build result. Although, it requires use of a script element :(

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

Interesting question. You can do it in Jenkins declarative pipeline using the 'changed' part of a post{} section. But you will need to set currentBuild.result to SUCCESS or FAILURE in the job and check it in the post section. There does not seem to be an easy way to get the current build status (fail, success etc) as far as Jenkins is concerned without tracking it in your pipeline - unless I have missed something subtle. Here is an example, you would send the notification in the changed section where it checks for SUCCESS:

pipeline {
    agent any

    parameters {
        string(name: 'FAIL',     defaultValue: 'false', description: 'Whether to fail')
    }

    stages {
        stage('Test') {

            steps {

                script {

                    if(params.FAIL == 'true') {
                        echo "This build will fail"
                        currentBuild.result = 'FAILURE'
                        error("Build has failed")
                    }
                    else {
                        echo "This build is a success"
                        currentBuild.result = 'SUCCESS'
                    }

                }
            }
        }
    }

    post {
        always  {
            echo "Build completed. currentBuild.result = ${currentBuild.result}"
        }

        changed {
            echo 'Build result changed'

            script {
                if(currentBuild.result == 'SUCCESS') {
                    echo 'Build has changed to SUCCESS status'
                }
            }
        }

        failure {
            echo 'Build failed'
        }

        success {
            echo 'Build was a success'
        }
        unstable {
            echo 'Build has gone unstable'
        }
    }
}

--Bill