0
votes

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?

1

1 Answers

0
votes

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.