Python

如何使用 Python 開始 hudson 工作?

  • March 7, 2012

我需要從 python 開始一個 hudson 工作,然後等待它完成。

此頁面建議使用 Python API,我在哪裡可以找到有關此的更多資訊?

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

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

python API 與 json api 相同。唯一的區別是您在返回程式碼上執行 eval() 並獲得 python 對象,而不必呼叫 json 庫。

用純 python 回答你原來的問題,這就是我為我們的工作所做的(這被稱為送出後掛鉤)請注意,我們的 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)

引用自:https://serverfault.com/questions/192301