使用REST在Jenkins中添加构建信息

时间:2014-02-11 12:04:34

标签: python post jenkins

有谁知道如何将构建信息添加到现有的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

任何想法我做错了什么?我对这个问题的解决方法完全错了吗?

4 个答案:

答案 0 :(得分:3)

对此进行逆向工程有点令人困惑。您只需要在POST中提交 json 参数:

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

在我的测试中,上面的工作是使用Jenkins 1.517设置构建名称和描述。

(另外,我不认为您应该设置内容类型标题,因为您应该提交表单编码数据。)

答案 1 :(得分:1)

来到这里(来自谷歌),我也有这个问题。除了Dave的回答,这可能会帮助那些尝试使用参数触发构建并获得此错误的人......

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

答案 2 :(得分:0)

尝试使用元组列表而不是字典,并对其进行urlencode。

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

答案 3 :(得分: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)