2014-02-11 22 views
3

有誰知道如何將構建信息添加到現有的Jenkins構建中?使用REST在Jenkins中添加構建信息

我想要做的是用構建代表的實際完整版本號替換#1內部版本號。我可以通過轉到http:// MyJenkinsServer/job/[jobname]/[buildnumber]/configure

手動執行此操作我試圖使用chrome反向工程來查看它發送給服務器的內容,如下:

Request URL:http://<server>/job/test_job/1/configSubmit 
Request Method:POST 
Status Code:200 OK 

Request Headers view source 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Cache-Control:max-age=0 
Connection:keep-alive 
Content-Length:192 
Content-Type:application/x-www-form-urlencoded 
Cookie:hudson_auto_refresh=false; JSESSIONID=qbn3q22phkbc12f1ikk0ssijb; screenResolution=1920x1200 
Referer:http://<server>/job/test_job/1/configure 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4 

Form Data view URL encoded 
displayName:#1 
description:test4 
core:apply:true 
json:{"displayName": "#1", "description": "test4", "": "test4", "core:apply": "true"}** 

Response Headers view source 
Content-Length:155 
Content-Type:text/html;charset=UTF-8 
Server:Jetty(8.y.z-SNAPSHOT) 

這至少給我的表單參數,我需要POST。所以從這個,我想出了以下python3代碼:

import requests 
params={"displayName":"Hello World", 
    "description":"This is my description", 
    "":"This is my description", 
    "core:apply":"true"} 

a = requests.post("http://myjenkinsserver/job/test_jira_job_update/1/configSubmit", data=params, auth=(username, pwd), headers={"content-type":"text/html;charset=UTF-8"}) 
if a.raw.status != 200: 
    print("***ERROR***") 
    print(a.raw.status) 
    print(a.raw.reason) 

但遺憾的是這種失敗,出現以下錯誤:

***ERROR*** 
400 
Nothing is submitted 

任何想法我做錯了嗎?我對這個問題的方法完全錯了嗎?

回答

2

對此進行逆向工程有點令人困惑。你只需要提交JSON參數在您的文章:

p = {'json': '{"displayName":"New Name", "description":"New Description"}'} 
requests.post('http://jenkins:8080/job/jobname/5/configSubmit', data=p, auth=(user, token)) 

在我的測試中,上述工程設置構建的名稱和描述與詹金斯1.517。

(另外,我不認爲你應該設置Content-Type頭,因爲你要提交表單的編碼數據。)元組,而不是字典的

+0

謝謝戴夫,刪除所有其他代碼後,「噪音」和標題都工作。 – DelboyJay

0

嘗試列表中,並用urlencode它。

params=[("displayName","Hello World"), 
    ("description","This is my description")] 
dataParam = urllib.urlencode(params) 
1

來到這裏(谷歌),我也有這個問題。除了戴夫的答案,這可能會幫助那些試圖觸發構建參數,並得到這個錯誤...

import json 
import requests 

job_name = "my-jenkins-job" 
job_parameters = [ 
    { 
     "name": "ip_address", 
     "value": "192.168..." 
    }, 
    { 
     "name": "url", 
     "value": "http://..." 
    }, 
    { 
     "name": "architecture", 
     "value": "x86" 
    } 
] 

data = {"json": json.dumps({"parameter": build_parameters})} 
r = requests.post('http://<jenkins server>/job/{job_name}/build/api/json'.format(job_name=job_name), data=data) 
r.raise_for_status() 
0

參數列表已更改。另外請確保您擁有Jenkins UI中定義的相同數量的鍵。如果不是400或500!

param = {"parameter": [{"name": "CustomerName", "value": "AcmeCorp"}]} 
data = {"json": json.dumps(param)} 
headers["content-type"] = "application/x-www-form-urlencoded" 
. 
. 
. 
r = requests.post(jobURL, headers = headers, data=data) 
相關問題