How to get the build number for jenkins work via Python code

advertisements

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


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)