2011-02-02 46 views
5

我有一個Python腳本,可以在Hudson構建上運行,並且希望能夠以編程方式設置構建的描述。通過Web API設置Hudson構建描述

我可以點擊構建頁面上的「添加描述」並填寫表單,我怎樣才能將一些數據發佈到與表單相同的URL?

回答

7

想通了,需要發佈以下作爲表格數據(內容類型application/x-www-form-urlencoded)至

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

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

在代碼:

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) 
+2

我正在使用Jenkins,當我嘗試這個時,我得到了403。任何人都知道這是否仍然在詹金斯工作? – 2012-06-01 01:41:56

2

使用一個「執行系統的Groovy腳本'構建任務:

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

(會評論,但沒有足夠的代表)

感謝jtb的大部分方法。如果安全服務器上啓用,我發現我可以使用此代碼(改編自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) 

和令牌可以在API令牌中找到的用戶值驗證:http://<myserver>/me/configure

0

這裏從shell可以正常工作的curl命令。替換之間和包括{}的文本。

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