4

I am developing a python code to deal with jenkins using the jenkinsapi package. I am looking for a simple way to pass the job name and get the latest build number for that job. Example

from jenkinsapi import jenkins
ci_jenkins_url = "job url"
username = None
token = None
job = "Test 3"
j = jenkins.Jenkins(ci_jenkins_url, username=username, password=token)

if __name__ == "__main__":
    j.build_job(job)

This is triggering builds successfully, but I need to get the build number for proceeding further. Any help would be highly appreciated

4 Answers 4

5

There are 2 ways :

Way 1: Using following APIs -

From above URls, you can get latest build number from builds block. For more details : check http://(jenkins_url):8080/job/(jobname)/api/

Way2 : Using jenkinsapi module

import jenkinsapi
from jenkinsapi.jenkins import Jenkins
server = Jenkins(jenkins_url,username=<<>>,password=<<>>)
print(server.get_job("jobname").get_last_buildnumber())
Sign up to request clarification or add additional context in comments.

Comments

2

The Job object implements several methods for getting the build number of the last build, last completed build, last stable build, etc.

jenkins_server = jenkins.Jenkins(ci_jenkins_url, username=username, password=token)
my_job = jenkins_server.get_job('My Job Name')
last_build = my_job.get_last_buildnumber()

You can use Python interactively to explore the API for packages that don't have complete online documentation:

>>> jenkins_server = jenkins.Jenkins(...)
>>> job = jenkins_server.get_job('My Job Name')
>>> help(job)

2 Comments

Thank you very much, its working. I did also try this..... last_build_number = j.get_job_info('test_api')['lastCompletedBuild'] ['number'], this threw Traceback (most recent call last): File "/home/trishal/PycharmProjects/python-jenkins/get_build_number.py", line 8, in <module> last_build_number = j.get_job_info('Test 3')['lastCompletedBuild'] ['number'] AttributeError: 'Jenkins' object has no attribute 'get_job_info' Process finished with exit code 1, not sure if I am using get_job_info correctly
@Trishal the Jenkins object does not have a get_job_info method.
0

the problem is solved. Had uninstalled slack-api and installed python-jenkins only. Now the documented methods are working

Comments

0

When you submit a job using build_job() api it sends it to the queue where the job roughly waits around 5secs before being executed. During this time if you execute get_queue_item(queue_id) you will see why: In the quiet period. Expires in 3.7 sec in the response. To get the build id of the job you just submitted you need to wait till the job gets out of the queue and that when the response for get_queue_item(queue_id) is updated with job details.

Sample code:

new_job = server.build_job('test-job') # returns queue id of the new build

while True:
    queue_info = server.get_queue_item(new_job)
    if queue_info['why'] is None:
        print(f"The job url is {queue_info['executable']['url']}")
        break

Sample response:

{
    '_class': 'hudson.model.Queue$LeftItem',
    'actions': [
        {
            '_class': 'hudson.model.CauseAction',
            'causes': [
                {
                    '_class': 'hudson.model.Cause$UserIdCause',
                    'shortDescription': 'Started by user ****',
                    'userId': '*****',
                    'userName': '****'
                }
            ]
        }
    ],
    'blocked': False,
    'buildable': False,
    'id': 30,
    'inQueueSince': 1708121493104,
    'params': '',
    'stuck': False,
    'task': {
        '_class': 'hudson.model.FreeStyleProject',
        'name': 'test-job',
        'url': 'http: //opense-jenki-*********/job/test-job/',
        'color': 'blue_anime'
    },
    'url': 'queue/item/30/',
    'why': None,
    'cancelled': False,
    'executable': {
        '_class': 'hudson.model.FreeStyleBuild',
        'number': 28,
        'url': 'http://opense-jenki-**************/job/test-job/28/'
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.