gpt4 book ai didi

python - 使用 header 中的 "location"信息检索通过 API 启动的 Jenkins 构建的 ID(jenkins 1.529 的新功能)

转载 作者:太空狗 更新时间:2023-10-30 00:56:23 25 4
gpt4 key购买 nike

我需要获取开始使用 REST API(使用 python)的 jenkins 作业的 ID。自 Jenkins 1.529 以来,似乎可以使用 API abd 触发构建以返回指向队列中作业的 url。

Jenkins 的文档:

Perform a build

To programmatically schedule a new build, post to this URL. If thebuild has parameters, post to this URL and provide the parameters asform data. Either way, the successful queueing will result in 201status code with Location HTTP header pointing the URL of the item inthe queue. By polling the api/xml sub-URL of the queue item, you cantrack the status of the queued task. Generally, the task will gothrough some state transitions, then eventually it becomes eithercancelled (look for the "cancelled" boolean property), or getsexecuted (look for the "executable" property that typically points tothe AbstractBuild object.)

但我没有成功获得有用的 url。

我试过:

import requests
requestKwargs ={'headers': {'Content-Type': 'application/x-www-form-urlencoded'}, 'data': {'json': '{"parameter": []}'}, 'verify': True}
url=server+'job/test/build/'
req=requests.post(url, **requestKwargs)
print 'status',req.status_code
location=req.headers['location']
print 'location is:',location

这段代码的输出是:

status 201

location is: http://SERVER_PORT/job/test/build/

如何使用“位置”信息来跟踪队列任务的状态?

谢谢。

最佳答案

我通过为我希望以编程方式与之交互的作业提供“ident”参数来解决这个问题。

提交作业时,我生成一个 uuid 并将其作为 ident 参数提交给 /job/<job_name>/buildWithParameters .

然后我得到 /queue/api/json并遍历排队项目的列表,在其参数中搜索具有该 uuid 的项目。获取队列ID。

然后我使用这个队列 ID 来轮询 /queue/item/<queue_id>/api/json每隔 N 秒,等待 Jenkins 在响应中提供内部版本号(一旦构建开始就会提供)。您可以使用此内部版本号来构建您想要的 url,/job/<job_name>/<build_number> .

您实际上需要在您的 jenkins 作业中添加 ident 作为参数。这很痛苦,但它工作可靠。

关于python - 使用 header 中的 "location"信息检索通过 API 启动的 Jenkins 构建的 ID(jenkins 1.529 的新功能),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18699555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com