Last active
August 29, 2015 14:06
-
-
Save samkirton/9ae8e15de756a006d74f to your computer and use it in GitHub Desktop.
Run a remote Jenkins job and wait for a response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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