21

I have a job in Jenkins. A website of our own triggers builds of this job via the 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 #.

How to do so via the REST api ?

Barth
  • 15,135
  • 20
  • 70
  • 105

4 Answers4

36

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.

Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
Jifeng Zhang
  • 5,037
  • 4
  • 30
  • 43
  • Thank you very much for your reply. The first command works well. However, the second one returns a 404. Do you have an idea what could go wrong ? By the way, do you have a pointer to the doc for this part of the API because I couldn't find it. – Barth Jan 09 '14 at 15:33
  • I have just realized that the command for the queue does actually remove the item from the queue but still returns a 404. It is weird. – Barth Jan 09 '14 at 15:37
  • Agree. Unfortunately Jenkins does not have clear documentation on this part. The 404 is wired, though it did cancel the build from queue. But you can find some documentation using "http:///job///api", and you can add "/api" to a lot of urls and get a page showing its "REST API" – Jifeng Zhang Jan 09 '14 at 15:45
  • 3
    You must `POST` on all URLs which have an effect, including `stop`. – Jesse Glick Oct 09 '14 at 13:00
  • 2
    And `token` is only available for `build` (and `buildWithParameters` and `poll`). It cannot be used on any other REST APIs. In general you should use `BASIC` authentication with API tokens. – Jesse Glick Oct 09 '14 at 13:02
  • 5
    The 404 is [JENKINS-21311](https://issues.jenkins-ci.org/browse/JENKINS-21311) and can be ignored. – Jesse Glick Oct 09 '14 at 13:02
  • 1
    @JifengZhang I don't think your update reflects @JesseGlick's correction, which is that the token only applies to `build` and cannot be used for `stop` or `cancelItem`. I just tried `stop` with the `token` param, but that redirected to a login URL, indicating the token had no effect. –  Apr 25 '16 at 18:34
  • How do you fetch the build number of ongoing builds? – vs_lala Jun 13 '21 at 07:33
3

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
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
1

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.

  1. Http Request Plugins is required by Jenkins ver2.17
  2. Uncheck the Prevent Cross Site Request Forgery exploits option. Manager Jenkins -> Configure Global Security -> Uncheck
  3. 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
  4. 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

David Wang
  • 11
  • 1
1

If you only need to cancel the active build from a certain job you can use this batch script (windows .bat syntax):

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.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
rawdesk.be
  • 65
  • 3