0

Hello DevOps Evangelists!

First of all, thanks to this answer, I was able to successfully cancel single TeamCity build using following curl:

curl http://teamcity:8111/app/rest/buildQueue/buildType:<buildId> \
  -X POST -u *** \
  -H 'Content-Type: application/json' -d '{
    "buildCancelRequest": {
      "comment": "This build was cancelled.",
      "readdIntoQueue": "false"
    }
  }'

However, my idea was to cancel multiple builds within the particular project via TeamCity REST API. I have tried the following:

curl http://teamcity:8111/app/rest/buildQueue/project:<projectId>,count:<n> \
  -X POST -u *** \
  -H 'Content-Type: application/json' -d '{
    "buildCancelRequest": {
      "comment": "Only one build was cancelled.",
      "readdIntoQueue": "false"
    }
  }'

Unfortunately, I have failed miserably, because only single build from this project was cancelled. I know I can send this request as many times as there are builds in the project, but this ugly workaround! I want to do it right! Could some tell me please how to cancel all the builds within the project by using TeamCity REST API?

Gucu112
  • 877
  • 10
  • 12

1 Answers1

0

As there were pressure of time and no answer given I was forced to use the workaround, so based on this answer I have prepared following request(s):

curl http://teamcity:8111/app/rest/buildQueue/project:<projectId>,count:[1-n] \
  -X POST -u *** \
  -H 'Content-Type: application/json' -d '{
    "buildCancelRequest": {
      "comment": "Multiple builds will be cancelled.",
      "readdIntoQueue": "false"
    }
  }'

You only need to replace n with the number of builds you have in the selected project to cancel them all. It basically sending multiple request which implies in stopping all the queued builds.

However, if you want to stop already running builds you need to hit different end-point:

curl http://teamcity:8111/app/rest/builds/project:<projectId>,running:true,count:[1-n] \
  -X POST -u *** \
  -H 'Content-Type: application/json' -d '{
    "buildCancelRequest": {
      "comment": "Already running builds will be stopped.",
      "readdIntoQueue": "false"
    }
  }'

If you know that there will be only one build running per project then you can skip count:[1-n] locator and only one request will be sent which will stop currently running build within the selected project.

Gucu112
  • 877
  • 10
  • 12
  • Actually, "count" in the URLs mentioned makes no difference. If the build is canceled, then the same URL starts to return the top-most queued/running build. If the build is still being canceled and is still int he queue "start:[k]" can be used to get the k-th build (0-based) – Yaegor Oct 25 '19 at 12:58
  • @Yaegor The count here makes `curl` hitting the target URL that many times you specify as _n_ variable (substituting the place `[1-n]` with numbers from 1 to _n_). If you remove `count` from the URL then `curl` will only make one request and only one build from the project will be canceled. The purpose of it is to **cancel all the builds within the project**. – Gucu112 Jan 04 '20 at 15:15