如何使用 Python 启动 hudson 作业?

如何使用 Python 启动 hudson 作业?

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

此页面建议使用 Python API,我在哪里可以找到有关此 API 的更多信息?

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

答案1

这是我在 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)

答案2

Python API 与 JSON API 相同。唯一的区别在于,您在返回代码上执行 eval() 即可获得 Python 对象,而不必调用 JSON 库。

用纯 Python 回答你最初的问题,这就是我为我们的工作所做的(这被称为提交后钩子)请注意,我们在 hudson 前面有 http 身份验证,这使得事情变得更加复杂。

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)

答案3

有一个项目叫JenkinsAPI它提供了一个高级 API,除其他功能外,还可用于触发 Jenkins 作业。语法如下:

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

相关内容