I need to start a hudson job from python, and then wait for it to complete.
This pages suggests a Python API, where can I find further information on this?
I need to start a hudson job from python, and then wait for it to complete.
This pages suggests a Python API, where can I find further information on this?
Here's my solution in jython:
from hudson.cli import CLI
class Hudson():
def StartJob(self, server, port, jobname, waitForCompletion = False):
args = ["-s", "http://%s:%s/hudson/" % (server, port), "build", jobname]
if waitForCompletion: args.append("-s")
CLI.main(args)
if __name__ == "__main__":
h = Hudson()
h.StartJob("myhudsonserver", "8080", "my job name", False)
The python API is the same as the json api. The only difference is that you you eval() on the returning code and you get python objects rather than having to call a json library.
Answering your original question in pure python this is what I did for our job (this is called as a post commit hook) Please note we have http auth in front of our hudson which makes things more complicated.
import httplib
import base64
TESTING = False
def notify_hudson(repository,revision):
username = 'XXX'
password = 'XXX'
server = "XXX"
cmd = 'svnlook uuid %(repository)s' % locals()
#we strip the \n at the end of the input
uuid = os.popen(cmd).read().strip()
cmd = 'svnlook changed --revision %(revision)s %(repository)s' % locals()
body = os.popen(cmd).read()
#we strip the \n at the end of the input
base64string = base64.encodestring('%s:%s' % (username, password)).strip()
headers = {"Content-Type":"text/plain",
"charset":"UTF-8",
"Authorization":"Basic %s" % base64string
}
path = "/subversion/%(uuid)s/notifyCommit?rev=%(revision)s" % locals()
if not TESTING:
conn = httplib.HTTPSConnection(server)
conn.request("POST",path,body,headers)
response = conn.getresponse()
conn.close()
if response.status != 200:
print >> sys.stderr, "The commit was successful!"
print >> sys.stderr, "But there was a problem with the hudson post-commit hook"
print >> sys.stderr, "Please report this to the devteam"
print >> sys.stderr, "Http Status code %i" % response.status
notify_hudson(repository,revision)
There is a project called JenkinsAPI which provides a high-level API which (amongst other things) can be used to trigger Jenkins jobs. The syntax is this:
api = jenkins.Jenkins(baseurl, username, password)
job = api.get_job(jobname)
job.invoke(securitytoken=token, block=block)