如何通过远程API在Jenkins触发构建并获取正确的构建编号

3
我正在使用Python请求在Jenkins中触发具有参数的构建,网址为http://jenkins:8080/view/projectAlpha/job/deploy_job_with_params/build,但我没有得到任何队列或构建号来标识我的构建。
根据Jenkins上面/api页面的说明:
"To programmatically schedule a new build, post to this URL (means the /build url - RaamEEIL). If the build has parameters, post to this URL (means the /buildWithParameters url - RaamEEIL) and provide the parameters as form data. Either way, the successful queueing will result in 201 status code with Location HTTP header pointing the URL of the item in the queue."
然而,在部署构建时返回的location header中,我没有得到队列url(例如http://jenkins:8080/queue/item/21038/api/json),而是得到了网址http://jenkins:8080/view/projectAlpha/job/deploy_job_with_params/,因此基本上我没有得到任何队列号或构建号。
我读到一个解决方法的建议,发送一个包含一些ID的附加参数,我可以用它来验证最新的构建是否是我的构建,或者用它来搜索构建列表,直到找到正确的构建。
谢谢。

我已经在此处发布了我的解决方案 https://dev59.com/D4jca4cB1Zd3GeqP3Ob6#74162441 - anandhu
1个回答

2
好的,原来是我把事情搞混了。这似乎也发生在许多其他人身上,所以这里提供一些信息。
/build url - 返回位置 = 作业的url,不包括有关您构建的信息
/buildWithParameters url - 返回位置 = 您构建的队列项目的url
根据Daniel Beck在https://issues.jenkins-ci.org/browse/JENKINS-30317的评论 -
(作者所说的"This"指的是/build url)
引用:
那就是问题所在。这实际上不是API。正如我之前所写的那样,文档中使用/buildWithParameters进行参数化构建的API。这只是假装成一个Web浏览器。
...因为Web浏览器会被重定向到一个空白的404错误页面。再次强调,您并没有实际使用API,而是假装成一个Web浏览器。
因此,尽管API页面说明您可以请求/build URL,但如果要获取构建的队列项URL作为“位置”标头,则必须请求/buildWithParameters URL。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接