简体   繁体   English

如何通过REST API停止Jenkins的构建?

[英]How to stop a build in Jenkins via the REST api ?

I have a job in Jenkins. 我在詹金斯有一份工作。 A website of our own triggers builds of this job via the REST api. 我们自己的网站通过REST api触发了这项工作的构建。 Sometimes we want to abort the build. 有时我们想要中止构建。 Sometimes, it can be before the build is even started. 有时,它可能在构建开始之前。 In such cases we have the queueItem # instead of the build #. 在这种情况下,我们有queueItem#而不是build#。

How to do so via the REST api ? 如何通过REST api这样做?

If the build has started, by POST on: 如果构建已经开始,请通过POST

http://<Jenkins_URL>/job/<Job_Name>/<Build_Number>/stop

Will stop/cancel the current build. 将停止/取消当前构建。

If the build has not started, you have the queueItem , then POST on: 如果构建尚未启动,则使用queueItem ,然后执行POST

http://<Jenkins_URL>/queue/cancelItem?id=<queueItem>

This is assuming your Jenkins Server has not been secured, otherwise you need to add BASIC authentication for a user with Cancel privileges. 这假设您的Jenkins服务器尚未受到保护,否则您需要为具有取消权限的用户添加BASIC身份验证

Actually this question is already answered. 实际上这个问题已经回答了。 So I will add, how to find id=<queueItem> , which I got stuck on finding this solution, which will helpful for others. 所以我要补充一下,如何找到id=<queueItem> ,我找不到这个解决方案,这对其他人有帮助。

So, you can get <queueItem> , by - http://jenkins:8081/queue/api/json 所以,您可以通过 - http:// jenkins:8081 / queue / api / json获取<queueItem>

Sample Output will be of json type like this one - 示例输出将是像这样的json类型 -

[{"_class":"hudson.model.Cause$RemoteCause","shortDescription":"Started by remote host 172.18.0.2","addr":"172.18.0.2","note":null}]}],"blocked":false,"buildable":false,"id":117,"inQueueSince":16767552,"params":"\nakey\t=AKIQ\nskey=1bP0RuNkr19vGze/bcb4ijDqVr8o\nnameofr=us\noutputtype=json\noid=284102\nadminname=admin","stuck":false,"task"

You have to take "id":117 , and parse it to - queueItem =117 . 您必须使用"id":117 ,并将其解析为 - queueItem =117

http://<Jenkins_URL>/queue/cancelItem?id=queueItem

Maybe you want to remotely send a post http request to stop a running build, there is a clue FYI, the jenkins job can stop another job(running build), like any jenkins admin click the X button when job is running. 也许你想远程发送一个post http请求来停止正在运行的构建,有一个线索FYI,jenkins作业可以停止另一个作业(运行构建),就像任何jenkins管理员在作业运行时单击X按钮。

  1. Http Request Plugins is required by Jenkins ver2.17 Jenkins ver2.17需要Http Request Plugins
  2. Uncheck the Prevent Cross Site Request Forgery exploits option. 取消选中“阻止跨站点请求伪造攻击”选项。 Manager Jenkins -> Configure Global Security -> Uncheck 经理詹金斯 - >配置全局安全 - >取消选中
  3. Setup Http Request Plugins's authorization. 设置Http Request Plugins的授权。 Manager Jenkins -> Configure System -> HTTP Request Basic/Digest Authentication -> add. 经理Jenkins - >配置系统 - > HTTP请求基本/摘要认证 - >添加。 Make sure the user has the job cancel permission 确保用户具有作业取消权限
  4. Job A is running. 作业A正在运行。 In job B, add build step as HTTP Request, URL: http://Jenkins_URL/job/Job_A_Name/lastBuild/stop , HTTP mode: POST, Authorization select the user you have just set, then build job B. 在作业B中,将构建步骤添加为HTTP请求,URL: http:// Jenkins_URL / job / Job_A_Name / lastBuild / stop ,HTTP模式:POST,授权选择刚刚设置的用户,然后构建作业B.

Done 完成

If you only need to cancel the active build from a certain job you can use this batch script (windows .bat syntax): 如果您只需要从某个作业中取消活动构建,则可以使用此批处理脚本(windows .bat语法):

REM @Echo off
CLS
REM CANCEL ACTIVE BUILD
REM PARAMETER 1 ACTIVE JOB NAME
if [%1] == [] GOTO NO_ARGUMENT
SET domain=https://my.jenkins.com/job/
SET path=/lastBuild/stop
SET url=%domain%%1%path%
"\Program Files\Git\mingw64\bin\curl.exe" -X POST %url% --user user:pass"
GOTO THEEND
:NO_ARGUMENT
Echo You need to pass the active jobname to cancel last build execution
:THEEND

Path to your local curl needs to set. 您需要设置本地curl路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM