Script to check the build status of a Jenkins job

Using JSON & Curl

BUILD_STATUS=$(curl --user USER:TOKEN_VALUE --silent $BUILD_URLapi/json | jq -r '.result')
echo $BUILD_STATUS

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

</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>
Tagged :
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x