How do I check the build status of a Jenkins build from the command line?

Solution 1:

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 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)

Solution 2:

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!


Solution 3:

A former colleague of mine wrote 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...


Solution 4:

Another Python solution:

from jenkinsapi.jenkins import Jenkins

jenkins_url = 'http://<server url>/'
server = Jenkins(jenkins_url, username = 'myUser', password = myPass)

job_instance = server.get_job('the job name')
running = job_instance.is_queued_or_running()
if not running:
   latestBuild = job_instance.get_last_build()
   print latestBuild.get_status()

Solution 5:

You can use a Groovy script:

  1. Via jenkins-cli

    echo 'println(jenkins.model.Jenkins.instance'\
    '.getItem("<JOB-NAME>").lastBuild.building)' \
        | java -jar jenkins-cli.jar -s <JENKINS-URL> groovy =
    

    , where = means standard in. You can authenticate with --username <USER> --password <PASS> or with -i <SSH-PRIVATE-KEY>.

  2. Via jenkins-cli over SSH

    echo -e 'println(jenkins.getItem("JOB-NAME").lastBuild.building)\nexit' \
    | ssh -p <JENKINS-SSH-PORT> <JENKINS-HOST> groovysh