10

I want to emulate the original Jenkins behaviour of doing stuff (like sending out notifications) when builds become unstable / fail or become successful again.

For that I need to query the previous build status, which can be done like this:

currentBuild.rawBuild.getPreviousBuild()?.getResult().toString()

However, rawBuild is supposed to be a "dangerous" object and as thus blacklisted and cannot be executed inside the Groovy sandbox.

Now, since I'm loading my Jenkins scripts from SCM, there is no way for me to deactivate the Groovy sandbox on a per-project level, but only for the whole Jenkins instance (i.e. through this), and this is certainly not something I want.

Is there any other way of determining the last build status of a job that conforms with the sandbox principles (and does not include hacks like querying the status via Jenkins' REST API) that I have missed?

Rob
  • 14,746
  • 28
  • 47
  • 65
Thomas Keller
  • 5,933
  • 6
  • 48
  • 80

2 Answers2

10

No need to access rawBuild, instead you can do the following without any special permissions:

currentBuild.getPreviousBuild().result

This will allow you to see the result of the previous build without needing to have any special privileges set by the Jenkins administrator. It should be able to be run anywhere.

Global Variable Reference: https://www.jenkins.io/doc/book/pipeline/getting-started/#global-variable-reference

And more specifically, in your own Jenkins: ${YOUR_JENKINS_URL}/pipeline-syntax/globals#currentBuild

More information: https://support.cloudbees.com/hc/en-us/articles/217591038-How-to-Iterate-Through-the-Last-Successful-Builds-in-Pipeline-Job

Yair Segal
  • 108
  • 1
  • 7
Nick
  • 423
  • 7
  • 17
  • What does that do? How does it answer the question? Don't just blurt out code. When your link goes dead, your answer becomes useless. Explain yourself! https://stackoverflow.com/help/how-to-answer – Rob Jan 12 '18 at 14:18
  • @Rob edited to add some context. Not sure why the downvote? – Nick Jan 12 '18 at 18:28
  • 1
    I did not downvote and prefer to comment. There are 8000 to 10,000 questions asked every day. It could have been anyone but my problem with your answer was that there was no explanation for what it did. As stated in [The Tour](https://stackoverflow.com/tour), Stack Overflow wants to be "a library of **detailed answers** to every question about programming." – Rob Jan 12 '18 at 18:55
  • @Rob no comment on the current state? Still not up to standards? – Nick Jan 12 '18 at 19:37
  • Don't know what you mean. If it's about Jenkins, I wouldn't know. – Rob Jan 12 '18 at 20:27
5

If you approve the method getRawBuild from Manage Jenkins > In-process Script Approval after you get RejectedAccessException on a build, you can use the method from a next build even in the groovy sandbox.

Besides getRawBuild, you need to approve getPreviousBuild and getResult one by one after you get the exception against each method for your script.

arasio
  • 1,260
  • 10
  • 14