How to get Build status of Jenkins Job using python script (Video)

Read Time: 3 mins

Most of us might have gone through situations where we need to check status of a Jenkins  Job and do some automations based on it. In this article, let me get you through steps on how you can check build status of last Jenkins job using Python script.

Step1: Use Jenkins API:

Yeah, Jenkins has an API which gives building status, Last Job build status(failed or Success) and some other useful information in JSON  format. This is the API /job/your_job_name_here/lastBuild/api/json .

Video Demonstration:

Please LIKE , COMMENT  and SUBSCRIBE .


Subscribe EasyAsLinux channel  

Let use curl  command to see the result of the API.

Here is the JSON result. I have hidden results which are not relevant for this article.

We can use keys building  and result  to achieve our goal. For your information, If value of key building  is True  (it is a boolean) which means requested Job is currently running and if it is False  means the Job is not running now. Like wise, if value of  result is SUCCESS , it means last build is success and if it is FAILURE , which means, last build is failed. A better picture is given below.

 

Step2: Let’s write Python script:

So we have all relevant information to write our Python script. Let’s create a logic. What if the Job is still running when we run the script? the Python script should  wait till the Job is completed and then only it should check the status of the Job(otherwise it gives you status of the last job). How do we do this? We need to write the Python script to check value of building  key, if it is True (which means Job is still running), we need to teach script check again after some time. Python’s WHILE loop  is our mate here. The script should exit the WHILE loop once the value of building  is False  and then check value of result  to understand the status of the Job.

Here is our Python script.

 

The Python script is very straight forward.  The core function  jenkins_job_status()  takes the job name as input and returns boolean output as per job status. Go ahead use this script and comment if you are facing any issues.

There could be use-cases like checking status of a remote Jenkins job from another Jenkins Job. There also you can use this script!. Enjoy

 

Subscribe to this blog so that you don’t miss out anything useful (Checkout Right Sidebar for the Subscription Form) . Please also put your thought on this article as a  comment .

A Site Reliability Engineer and an automation enthusiast.
Posts created 32

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
x