我如何使用Python启动哈德森作业?

我需要从python启动一个hudson作业,然后等待它完成。

本页面提供了一个Python API,在哪里可以find关于此的更多信息?

http://wiki.hudson-ci.org/display/HUDSON/Remote+access+API

这里是我的jython解决scheme:

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) 

python API与json api相同。 唯一的区别就是你在返回的代码中使用了eval(),并且你得到了python对象,而不必调用一个json库。

在纯python中回答你的原始问题这就是我为我们的工作所做的(这被称为post commit hook)请注意,我们在hudson前面有http auth,这使得事情变得更加复杂。

 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) 

有一个名为JenkinsAPI的项目,它提供了一个高级API(可以用来触发Jenkins作业)。 语法是这样的:

 api = jenkins.Jenkins(baseurl, username, password) job = api.get_job(jobname) job.invoke(securitytoken=token, block=block)