jenkinsjenkins-api

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. 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 ?


Solution

  • 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.