Set Hudson Build Description via Web API
Asked Answered
P

4

5

I have a Python script that operates on Hudson builds and would love to be able to set the description of a build programmatically.

I can click "Add Description" on a build's page and fill in the form, how can I POST some data to the same URL that form does?

Plenum answered 2/2, 2011 at 2:23 Comment(0)
P
7

Figured it out, need to POST the following as form data (content type application/x-www-form-urlencoded) to

http://myserver/hudson/job/thebuild/10/submitDescription

{"description": "Some Description for the build"}

In code:

def set_description(build_url, desc):
    req_data = urllib.urlencode({'description': desc})
    req = urllib2.Request(build_url + '/submitDescription', req_data)
    req.add_header('Content-Type', 'application/x-www-form-urlencoded')
    urllib2.urlopen(req)
Plenum answered 2/2, 2011 at 3:10 Comment(1)
I'm using Jenkins and I'm getting a 403 when I try this. Anyone know if this still works in Jenkins?Module
S
3

(would comment, but not enough rep)

Thanks to jtb for the bulk of the approach. If security is enabled on the server, I found that I could authenticate using this code (adapted from here)

def set_description(build_url, desc, user, token):
    import base64, urllib, urllib2
    req_data = urllib.urlencode( {'description': desc } )
    req = urllib2.Request(build_url + '/submitDescription', req_data)
    req.add_header('Content-Type', 'application/x-www-form-urlencoded')
    auth = 'Basic {}'.format(base64.b64encode("{}:{}".format( user, token )))
    req.add_header( 'Authorization', auth )
    response = urllib2.urlopen(req)

The values for user and token can be found under API Token in: http://<myserver>/me/configure

Stomy answered 3/10, 2014 at 14:2 Comment(0)
M
2

Using an 'Execute system Groovy script' Build task:

import hudson.model.Cause
import hudson.model.Job
import jenkins.model.Jenkins

final JOB_NAME = 'my-job-name'

final jenkins = Jenkins.instance
final job = jenkins.getItemByFullName(JOB_NAME, Job.class)
final currentBuild = Thread.currentThread().executable
final buildNumber = currentBuild.getNumber()

job.builds
    .findAll { build ->
        build.number == buildNumber
    }
    .each { build ->
        build.setDescription("Some Description for the build")
    }
Module answered 1/6, 2012 at 22:58 Comment(0)
B
0

Here is the curl command that worked fine from shell. Replace the text in between and including {}.

curl -X POST -u {user:password} -H 'Content-Type: application/x-www-form-urlencoded' --data-urlencode description={descriptionstring} {hudsonurl}/job/{jobname}/{buildnumber}/submitDescription

Batter answered 4/9, 2015 at 2:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.