TeamCity 9.x Documentation 的触发构建
部分中有一个 cURL 示例:
curl -v -u 用户:密码 http://teamcity.server.url:8111/app/rest/buildQueue --request POST --header "Content-Type:application/xml"--data -二进制@build.xml
我想知道如何将其转换为等效的 Python 脚本(使用 requests
模块中的 POST
请求)?
顺便说一句,我尝试了以下Python脚本,但得到了这样的响应代码400(错误请求)
:
url = "http://myteamcity.com:8111/httpAuth/app/rest/buildQueue/"
headers = {'Content-Type': 'application/json'}
data = json.dumps({'buildTypeId': 'MyTestBuild'})
r = requests.post(url, headers=headers, data=data, auth=("username", "password"), timeout=10)
print "r = ", r
>> r = <Response [400]>
如果将headers
中的Content-Type
更改为Accept
,则会得到另一个响应代码415(不支持的媒体类型)
:
headers = {'Accept': 'application/json'}
>> r = <Response [415]>
最佳答案
触发构建的文档显示您需要发送 XML,而不是 JSON:
<build>
<buildType id="buildConfID"/>
</build>
TeamCity REST API 有点混杂;有些方法同时接受 XML 和 JSON,有些方法只接受 XML。这是后一种方法。他们将根据您设置的 Accept
header 的内容,使用 XML 或 JSON 进行响应。
将上述内容与您所需的构建 ID 一起发送;对于 XML 文档,您可以简单地使用模板:
from xml.sax.saxutils import quoteattr
template = '<build><buildType id={id}/></build>'
url = "http://myteamcity.com:8111/httpAuth/app/rest/buildQueue/"
headers = {'Content-Type': 'application/xml'}
build_id = 'MyTestBuild'
data = template.format(id=quoteattr(build_id))
r = requests.post(url, headers=headers, data=data, auth=("username", "password"), timeout=10)
请注意,我使用了 xml.sax.saxutils.quotattr()
function确保 build_id
的值被正确引用以作为 XML 属性包含在内。
这将生成 XML;如果您想处理 JSON 响应,请将 'Accept': 'application/json'
添加到 headers
字典中。
关于python - 如何使用Python请求模块和TeamCity API来触发构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875345/