1
votes

We use Git, jenkins and python at office.

We build a new SW version on a daily basis using jenkins.

How can I get the branch name to which the build belongs using Python from Jenkins?

Is there any API (function call) in Jenkins that can I give the build number as an argument, and it gives me the branch number?

Temporary solution

We also have a git tag for each build, in the following format:

<build>_<username>_<branch>

I currently use the following git command to get the tag:

git ls-remote --tags <git-repo> | grep BUILD-NUMBER

Once I have the tag, I extract the branch name from it using regex.

Thanks in advance.

1

1 Answers

3
votes

Check jenkinsapi https://pypi.python.org/pypi/jenkinsapi

With it you can do:

>>> from jenkinsapi.jenkins import Jenkins
>>> job_name = 'example_job'
>>> build_number = 123
>>> server = Jenkins('http://localhost:8080', username='user', password='passwd')
>>> build = server.get_job(job_name).get_build(build_number)
>>> rev = build.get_revision_branch()[0].get('name')
>>> print rev
refs/remotes/origin/master
>>> rev.rsplit('/', 1)[1]
'master'