Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
87 lines (70 sloc) 3 KB
#! /usr/bin/python
# Author: Alexander Sirotin /
# Copyright (c) 2017 Alexander Sirotin.
# Licensed under MIT license:
# Your use is on an "AS-IS" basis, and subject to the terms and conditions of this license.
import re
import sys
import jenkins
import logging
from optparse import OptionParser
logger = logging.getLogger()
hdlr = logging.StreamHandler(sys.stderr)
formatter = logging.Formatter('%(asctime)s %(levelname)s\t%(message)s')
def connect(server, port, username, password):"Connecting to Jenkins at %s:%d (user: %s)" % (server, port, username))
return jenkins.Jenkins("http://%s:%d" % (server, port), username=username, password=password)
except Exception as e:
logger.error("Connection failed: %s" % e.message)
return None
def check_build(conn, number):"Checking build number %d" % number)
info = conn.get_build_info(name=options.job, number=number)
if info["building"]:"Build %d is still in progress - skipping" % number)
return False
if info["result"] != "SUCCESS":"Build %d result is %s - skipping" % (number, info["result"]))
return False
branch = re.findall("\((.*)\)", info["displayName"])[0]
if branch != options.branch:"Build %d is on branch %s - skipping" % (number, branch))
return False"Found build %d" % number)
print("%d" % number)
return True
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("", "--server", dest="server", default="", help="Jenkins server ip address")
parser.add_option("", "--port", dest="port", default=8080, type="int", help="Jenkins server port")
parser.add_option("", "--username", dest="username", default="automation", help="Jenkins user name")
parser.add_option("", "--password", dest="password", default="password", help="Jenkins password")
parser.add_option("-j", "--job", dest="job", default="", type="string", help="Jenkins job name")
parser.add_option("-b", "--branch", dest="branch", default="", type="string", help="Git branch for the job")
parser.add_option("-n", "--number", dest="number", type="int", help="Build number as a hint")
(options, args) = parser.parse_args()
conn = connect(options.server, options.port, options.username, options.password)
if conn == None:
if options.number:"Checking given build number %d" % options.number)
found = check_build(conn, options.number)
if found:
logger.warn("The given build is not in the right branch - not using it.")"Looking for last successful job %s on branch %s" % (options.job, options.branch))"Getting builds for job %s" % options.job)
job = conn.get_job_info(options.job)
builds = job["builds"]
for b in builds:
found = check_build(conn, b["number"])
if found:
logger.error("Did not found any suitable build.")