How to stop a build in Jenkins via the REST api ?
If the build has started, by POST
on:
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:
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.
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.
So, you can get <queueItem>
, by - http://jenkins:8081/queue/api/json
Sample Output will be of json type like this one -
[{"_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
.
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.
- Http Request Plugins is required by Jenkins ver2.17
- Uncheck the Prevent Cross Site Request Forgery exploits option. Manager Jenkins -> Configure Global Security -> Uncheck
- Setup Http Request Plugins's authorization. Manager Jenkins -> Configure System -> HTTP Request Basic/Digest Authentication -> add. Make sure the user has the job cancel permission
- Job A is running. 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.
Done