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

Issue

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

Environment

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 whitelisted:

  • 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.

References

Have more questions?

0 Comments

Please sign in to leave a comment.