首页 > 解决方案 > TeamCity - 如何通过 REST API 取消多个构建

问题描述

你好 DevOps 布道者!

首先,感谢这个答案,我能够使用以下 curl 成功取消单个 TeamCity 构建:

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"
    }
  }'

但是,我的想法是通过 TeamCity REST API 取消特定项目中的多个构建。我尝试了以下方法:

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"
    }
  }'

不幸的是,我失败得很惨,因为这个项目中只有一个构建被取消了。我知道我可以发送这个请求的次数与项目中的构建次数一样多,但是这个丑陋的解决方法!我想把它做好!有人可以告诉我如何使用 TeamCity REST API 取消项目中的所有构建吗?

标签: apiteamcitypipeline

解决方案


由于时间紧迫且没有给出答案,我被迫使用解决方法,因此基于此答案,我准备了以下请求:

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"
    }
  }'

您只需将n替换为您在所选项目中拥有的构建数量即可将它们全部取消。它基本上发送多个请求,这意味着停止所有排队的构建。

但是,如果您想停止已经运行的构建,则需要达到不同的端点:

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"
    }
  }'

如果您知道每个项目只会运行一个构建,那么您可以跳过count:[1-n]定位器,并且只会发送一个请求,这将停止当前在所选项目中运行的构建。


推荐阅读