简体   繁体   中英

Getting current build number in jenkins using python

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?

script.py

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 "

I think the answer you're looking for is within the python-jenkins documentation specifically at Example 9

The code snippet below is from their documentation:

next_bn = server.get_job_info('job_name')['nextBuildNumber']
server.set_next_build_number('job_name', next_bn + 50)

I think you really only need the first line for the current/next build number. The caveat is that you need this plugin for your jenkins.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM