Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

In trying to make doing the right thing easy, I'm looking for and making command line tools for our developers.

I'm hoping to find a way that I can check the jenkins build status without switching to the browser.

If I need to I'll make a script using the json api but was wondering if there's already something like this built in.

Anyone know?

share|improve this question

4 Answers 4

up vote 12 down vote accepted

I couldn't find a built in tool so I made one:

#!/usr/bin/python
#
# author: ajs
# license: bsd
# copyright: re2


import json 
import sys
import urllib2

jenkinsUrl = "https://jenkins.example.com/job/"


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

try:
    jenkinsStream   = urllib2.urlopen( jenkinsUrl + jobName + "/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)
share|improve this answer

A former colleague of mine wrote this: https://github.com/txels/autojenkins which has a whole bunch of convenience features and API type stuff around working with a jenkins instance from Python..

share|improve this answer

Check to see if a build is running or not

I tried the Python script in the answer to this question, but couldn't get it to work. I don't know Python, and didn't want to invest any time in debugging, but was able to read enough of the script to gain inspiration from it.

All I need to do is check to see if a build is running or not. To do that I used curl and grep, like this:

curl http://myjenkins/job/myjob/lastBuild/api/json | grep --color result\":null

  • If a build is in progress, a grep for result\":null will return 0.
  • If a build is finished, a grep for result\":null will return 1.

Not especially elegant, but it works well enough for my needs.

For example, I have a Bash script that starts a build, then waits for it to finish:

JOB_URL=http://jenkins.local/job/stevehhhbuild
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json

GREP_RETURN_CODE=0

# Start the build
curl $JOB_URL/build?delay=0sec

# Poll every thirty seconds until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
    sleep 30
    # Grep will return 0 while the build is running:
    curl --silent $JOB_STATUS_URL | grep result\":null > /dev/null
    GREP_RETURN_CODE=$?
done

echo Build finished

Thanks for the inspiration, Catskul!

share|improve this answer
    
I went and checked my current implementation which is working, and it's a bit different than the version I put in the answer because of some password requirements. Do you know why it wasn't working for you? –  Catskul Aug 28 '12 at 17:01
    
The Python script works great if a job has already finished, but if a job is running, the Python script fails: TypeError: cannot concatenate 'str' and 'NoneType' objects. I don't know Python, so I switched to using shell and +1ed your answer for the inspiration. Thanks! –  Steve HHH Aug 28 '12 at 22:07

Fortunately, there is a jenkins-cli that you can use to get some information from jenkins. Unfortunately, you can't retrieve the status of a build using the CLI--which means your solution of using the JSON API is not only correct--its the only programatic way of doing so.

Also, while it looks like get-job might do what you want, it doesn't actually return the result--it only returns the job configuration.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.