使用REST在Jenkins中添加构建信息 [英] Add build information in Jenkins using REST

查看:145
本文介绍了使用REST在Jenkins中添加构建信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何将构建信息添加到现有的Jenkins构建中吗?

Does anyone know how to add build information to an existing Jenkins build?

我要执行的操作是将#1内部版本号替换为该内部版本所代表的实际完整版本号.我可以通过http://MyJenkinsServer/job/[jobname]/[buildnumber]/configure

What I'm trying to do is replace the #1 build number with the actual full version number that the build represents. I can do this manually by going to http://MyJenkinsServer/job/[jobname]/[buildnumber]/configure

我尝试通过使用chrome对标头进行反向工程,方法是查看它发送到服务器的内容,然后发现以下内容:

I have tried to reverse engineer the headers using chrome by seeing what it sends to the server and I found the following:

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代码:

This at least gives me the form parameters that I need to POST. So from this I came up with the following python3 code:

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)

但遗憾的是,此操作失败并显示以下错误:

but sadly this failed with the following error:

***ERROR***
400
Nothing is submitted

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

Any ideas what I am doing wrong? Is my approach to this problem completely wrong?

推荐答案

对它进行反向工程有点混乱.您只需要在POST中提交 json 参数:

It's a bit confusing to reverse engineer this. You just need to submit the json parameter in your POST:

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设置了构建名称和描述.

In my tests, the above works to set the build name and description with Jenkins 1.517.

(此外,我不认为您应该设置content-type标头,因为您应该提交表单编码的数据.)

(Also, I don't think you should set the content-type header, since you should be submitting form-encoded data.)

这篇关于使用REST在Jenkins中添加构建信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆