54

How do I check the Jenkins build status without switching to the browser?

If required, I can create a script using the JSON API, but I was wondering if there is already something like this built in.

3
  • You can also use specialized tools like CatLight Build Monitor that will show the build status in tray.
    – alex
    Mar 14, 2017 at 0:39
  • 2
    Every post here seems to point to "last build". Is there a similar query for checking status of job/build number X? Something you're checking in real time or after the fact.
    – David
    Mar 31, 2017 at 5:16
  • I am answering in the comment form, because I have no reputation to answer. Use xpath feature. The URL https://HUDSON_URL/job/JOBNAME/api/xml?depth=1&xpath=//build[number=N]/result returns the status of the build number N. For the last build the URL looks as follows: https://HUDSON_URL/job/JOBNAME/api/xml?depth=1&xpath=//lastBuild/result. Dec 8, 2022 at 19:00

10 Answers 10

46

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)
1
  • 2
    How you are authenticated Jenkins user in your script? May 17, 2019 at 13:11
21

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!

4
  • 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, 2012 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, 2012 at 22:07
  • You can add || and after that the condition for exit code 1, curl --silent $JOB_STATUS_URL | grep result\":null > /dev/null || if [ "$?" == "1" ];then GREP_RETURN_CODE=$? So you don't get exit code '1' in case you are running in Jenkins build and you don't want it to fail. Aug 22, 2017 at 8:05
  • There is a problem here. Suppose job "stevehhhbuild" is run very frequently, for example once a minute. "lastBuild" is exactly that, the "lastBuild". You may start by looking at lastBuild which is #5, but the next time you may be querying about lastBuild #6 or #7. Indeed a built may have been canceled and restarted between your checks. To do this properly, you need to check the results of a specific build number. Jun 16, 2023 at 14:04
6

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

6

I think I found an easier way. If I understood correctly, you want to check the result of the build - if it was a success or a failure, in other words.

Jenkins CLI's "build" command changes the exit code depending on the result of the build, as long as you use the -s or -f option at the end.

For example,

java -jar jenkins-cli.jar -s <url of Jenkins instance> build <project> -s

or

java -jar jenkins-cli.jar -s <url of Jenkins instance> build <project> -f

Notice that the option goes at the end; it's not the first -s, which is used to define the URL of the Jenkins instance.

And then, to get the result, you can use $?:

echo $?

If the result is 0, it was a success. If it's something other than 0, it was a failure.

Reference: I can't find a public Jenkins instance that gives access to this page, but it can be found in your local Jenkins instance: http://<url of Jenkins Instance>/cli/command/build. It also explains the difference between -s and -f:

-s  : Wait until the completion/abortion of the command. Interrupts are passed
      through to the build.

-f  : Follow the build progress. Like -s only interrupts are not passed
      through to the build.
1
  • there is no way to get info about job in progress
    – Psychozoic
    Apr 2, 2020 at 11:03
6

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()
1
  • thanks. I also found other API calls on job_instance to be useful (to check if it's running alone : is_running() , to check if its queued alone : is_queued() apart from the combined check you correctly showed. Mar 20, 2020 at 1:18
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
    
2
  • I have to use jenkins.getItemByFullName(). Unfortunately, lastBuild is always null. Also, the Groovy shell apparently doesn't recognize this statement: Unknown property: exit. May 19, 2020 at 18:07
  • ERROR: user is missing the Overall/Administer permission It seems this doesn't work for mere mortals
    – poleguy
    Mar 22, 2022 at 21:46
2

You can use the symbolic descriptor lastBuild:

http://localhost/jenkins/job/<jobName>/lastBuild/api/xml

The result element in the response contains a string describing the outcome of the build.

2

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--it's the only programmatic 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.

1

Another script for CMD (Windows):

:loop
ping 127.0.0.1 -n 6  1>nul
curl --silent http://localhost:8080/job/JOB_NAME/lastBuild/api/xml | FINDSTR "SUCCESS FAILURE" >nul & IF ERRORLEVEL 1 (goto :loop)
echo "BUILD FINISH!!"
2
  • Thanks. This proved to be very helpful.
    – user548005
    Nov 11, 2019 at 20:47
  • It looked promising but doesn't work, It just run the job multiple times. What am I missing here ?
    – Epligam
    Oct 31, 2022 at 14:21
0

You can try with this,

JOB_URL=http://localhost:8080/view/TestTab/job/JobWait
JOB_STATUS_URL=${JOB_URL}/lastBuild/api/json

GREP_RETURN_CODE=0

# Start the build
curl --user "username:password" $JOB_URL/build?delay=0sec

# Poll every 10 second  until the build is finished
while [ $GREP_RETURN_CODE -eq 0 ]
do
    sleep 10
    # Grep will return 0 while the build is running:
    curl --user "username:password" --silent $JOB_STATUS_URL | grep result\":null > /dev/null || if [ "$?" == "1" ]; then
      exit 0
    fi

    GREP_RETURN_CODE=$?
done
echo Build finished

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .