Is there a way to obtain the status of the Jenkins job in a variable during a Post-Build shell script?
I want to print out the message Build Status is $BUILD_URL :: $BUILD_STATUS
, where $BUILD_STATUS
is the status of the current completed build (e.g. ABORTED
, SUCCESS
, or FAILURE
).
If you can invoke a python script as a post-build step, you can try something like this:
import os, sys, json, codecs, urllib2
def main():
url = "http://localhost:8080/job/jobName/lastBuild/api/json"
try:
fRead = urllib2.urlopen(url, None, 30); # 30 second timeout
except:
raise
jsonResponse = json.loads(fRead.read());
fRead.close();
jobStatus = jsonResponse["result"]
main();
I have tested the url on my Jenkins and it works, but I haven't tested the script itself, so be wary. Obviously, substitute the port number and jobName as appropriate.
In my case, I had to include the API TOKEN here is what worked for me:
BUILD_STATUS=$(curl --user USER:TOKEN_VALUE --silent $BUILD_URLapi/json | jq -r '.result')
which for me was:
BUILD_STATUS=$(curl --user robert:valueofmysecrettoken --silent $BUILD_URLapi/json | jq -r '.result')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With