Dave Taylor Dave Taylor -3 years ago 252
Groovy Question

Get Jenkins Pipeline lastBuild result in Jenkinsfile

I want to use a Jenkinsfile (scripted pipeline) to send a slack message on a recovered build. I've tried using

lastBuild
but that isn't available. I was hoping there would be something along these lines.

if(lastBuild.result == "FAILURE") {
slackSend(channel: '#ci', color: '#00ee00', message: "RECOVERED: ${env.JOB_NAME} - [${env.BUILD_NUMBER}] (${env.BUILD_URL})")
}

Answer Source

to get from current pipeline build the previous one:

node {
  def lastBuild = currentBuild.rawBuild.getPreviousBuild()
  if( lastBuild && lastBuild.getResult().toString()=="FAILURE" ){
    println "the previous build ${lastBuild.getId()} failed"
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download