How to get build number for jenkins to work through python code

I am developing python code to work with jenkins using the jenkinsapi package. I am looking for an easy way to pass the name of the job and get the last build number for this 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 starts the assembly successfully, but I need to get the assembly number to continue. Any help would be greatly appreciated

+4
source share
3 answers

The Job object implements several methods for obtaining the assembly number of the last assembly, the last completed assembly, the last stable assembly, 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 learn the APIs for packages that do not have complete online documentation:

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

:

1: API -

URls builds. : http://(jenkins_url):8080/job/(jobname)/api/

Way2: jenkinsapi

import jenkinsapi
from jenkinsapi.jenkins import Jenkins
server = Jenkins(jenkins_url,username=<<>>,password=<<>>)
print(server.get_job("jobname").get_last_buildnumber())
+1

the problem is resolved. Removed slack-api and installed only python-jenkins. Documented methods now work

0
source

Source: https://habr.com/ru/post/1626117/


All Articles