Getting current build number in jenkins using python
<p>I have a python script from which I am trying to get the current build number of a job in jenkins. Below script gives the last build number and whether the build is success or failure. How can I get the current build number? I mean when I run this script, It will build with new build number and how can I get that current build number?</p>

<p>script.py</p>

<pre><code>import jenkins
import urllib2 
import urllib
import sys

j = jenkins.Jenkins('http://localhost:8080')
if not j.job_exists('sample1234'):
    j.create_job('sample1234', jenkins.EMPTY_CONFIG_XML)

j.reconfig_job('sample1234', jenkins.RECONFIG_XML)
j.build_job('sample1234')

last_build_number = j.get_job_info('sample123')['lastCompletedBuild'] ['number']
print last_build_number

build_info=j.get_build_info('sample123',last_build_number)
if build_info['result']=='SUCCESS':
    print "Build Success "
else:
    print " Build Failed "
</code></pre>

<p>I think the answer you're looking for is within the python-jenkins <a href="http://python-jenkins.readthedocs.io/en/latest/examples.html" rel="nofollow">documentation</a> specifically at Example 9</p>

<p>The code snippet below is from their documentation:</p>

<pre><code>next_bn = server.get_job_info('job_name')['nextBuildNumber']
server.set_next_build_number('job_name', next_bn + 50)
</code></pre>

<p>I think you really only need the first line for the current/next build number. The caveat is that you need this <a href="https://wiki.jenkins-ci.org/display/JENKINS/Next+Build+Number+Plugin" rel="nofollow">plugin</a> for your jenkins.</p>

python jenkins jobs jenkins-api