Pipeline - How can I check previous build status in a Pipeline Script?

Article ID:230922188
1 minute readKnowledge base

Issue

I want to be able to check on the status of my previous build in a pipeline script.

Resolution

This would be what your pipeline script would look like:

if(!hudson.model.Result.SUCCESS.equals(currentBuild.rawBuild.getPreviousBuild()?.getResult())) {
  echo "last build failed"
}

This will check if the last build was a failure and then echo that the last build failed.

Several methods will need to be allowlisted:

  • method hudson.model.Run getPreviousBuild

  • method hudson.model.Run getResult

  • method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild

Tested product/plugin versions

This was last tested on Jenkins 2.204.1 with the Script Security Plugin version 1.68.

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.