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")

if __name__ == "__main__":
    h = Hudson()
    h.StartJob("myhudsonserver", "8080", "my job name", False)
| improve this answer | |

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


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",
           "Authorization":"Basic %s" % base64string
    path = "/subversion/%(uuid)s/notifyCommit?rev=%(revision)s" % locals()
    if not TESTING:
        conn = httplib.HTTPSConnection(server)
        response = conn.getresponse()
        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

| improve this answer | |

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)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.