Skip to content

Instantly share code, notes, and snippets.

@samkirton
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samkirton/9ae8e15de756a006d74f to your computer and use it in GitHub Desktop.
Save samkirton/9ae8e15de756a006d74f to your computer and use it in GitHub Desktop.
Run a remote Jenkins job and wait for a response
import jenkins
import time
JOB_NAME = 'job_name'
URL = 'http://jenkins_url'
TIMEOUT = 120
SLEEP = 5
def get_job_info(j):
return j.get_job_info(JOB_NAME)
def exit_if_job_running(job_info):
print job_info
if "queueItem" in job_info and job_info['queueItem'] != None:
print "A job is already running"
exit(-1)
def get_last_build_number(j):
info = get_job_info(j)
if not info:
return 0
else:
return info['lastBuild']['number'] + 1
def wait_for_job(j, build_number, timeout):
timeout += 1
if timeout > TIMEOUT:
return None
try:
build_info = j.get_build_info(JOB_NAME,build_number)
if build_info['building']:
time.sleep(SLEEP)
return wait_for_job(j,build_number,timeout)
return build_info
except jenkins.JenkinsException:
time.sleep(SLEEP)
return wait_for_job(j,build_number,timeout)
j = jenkins.Jenkins(URL, '', '')
build_number = get_last_build_number(j)
print "> RUNNING BUILD #%s ON %s%s" % (build_number,URL,JOB_NAME)
j.build_job(JOB_NAME)
build_info = wait_for_job(j,build_number,0)
if build_info and build_info['result'] == 'FAILURE':
print "Build failed"
print build_info
exit(-1)
elif not build_info:
print "Build timeout"
exit(-1)
else:
print "Build success!"
print build_info
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment