Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

Script to check the build status of a Jenkins job

Using JSON & Curl

[code]BUILD_STATUS=$(curl –user USER:TOKEN_VALUE –silent $BUILD_URLapi/json | jq -r ‘.result’)
echo $BUILD_STATUS[/code]

Using jenkins Plugins
Plugins Name – Conditional Build Step

For Implementations, Please check here.
https://stackoverflow.com/questions/11125598/getting-the-build-status-in-post-build-script

Using Python & Json

[code]</pre>
#!/usr/bin/python
#
# author: ajs
# license: bsd
# copyright: re2

import json
import sys
import urllib
import urllib2

jenkinsUrl = “https://jenkins.example.com/job/”

if len( sys.argv ) > 1 :
jobName = sys.argv[1]
jobNameURL = urllib.quote(jobName)
else :
sys.exit(1)

try:
jenkinsStream = urllib2.urlopen( jenkinsUrl + jobNameURL + “/lastBuild/api/json” )
except urllib2.HTTPError, e:
print “URL Error: ” + str(e.code)
print ” (job name [” + jobName + “] probably wrong)”
sys.exit(2)

try:
buildStatusJson = json.load( jenkinsStream )
except:
print “Failed to parse json”
sys.exit(3)

if buildStatusJson.has_key( “result” ):
print “[” + jobName + “] build status: ” + buildStatusJson[“result”]
if buildStatusJson[“result”] != “SUCCESS” :
exit(4)
else:
sys.exit(5)

sys.exit(0)

Reference –
<a href=”http://serverfault.com/questions/309848/how-can…49988bb53ee820fe202a”>serverfault.com/questions/309848/how-can…49988bb53ee820fe202a</a>
<pre>[/code]

Rajesh Kumar
Follow me