7
votes

I would like to ask is there any way to disable build triggers for all TeamCity projects by running a script?

I have a scenario that I need to disable all the build triggers to prevent the builds from running. This is because sometimes, I might need to perform some upgrading process on build agent machines which will take more than one day.

I do not wish to manually click on Disable buttons for every build triggers on every different TeamCity projects. Is there a way to automate this process?

Thanks in advance.

4

4 Answers

6
votes

Use Team City REST API.

Given your Team City is deployed at http://dummyhost.com and you enabled guest access with system admin role (otherwise just switch from guestAuth to httpAuth in URL and specify user with password in request, details are in documentation) you can do next:

  1. Get all build configurations GET http://dummyhost.com/guestAuth/app/rest/buildTypes/
  2. For each build configuration get all triggers GET http://dummyhost.com/guestAuth/app/rest/buildTypes/id:***YOUR_BUILD_CONFIGID***/triggers/
  3. For each trigger disable it PUT http://dummyhost.com/guestAuth/app/rest/buildTypes/id:***YOUR_BUILD_CONFIGID***/triggers/***YOUR_TRIGGER_ID***/disabled

See full documentation here

3
votes

You can pause the build queue. See this video. This way you needn't touch the build configurations at all; you're just bringing the TC to a halt.

For agent-specific upgrades, it's best to disable only the agent you're working on. See here.

Neither of these is "by running a script" as you asked, but I take it you were only asking for a scripted solution to avoid a lot of GUI clicking.

0
votes

Another solution might be to simply disable the agent, so no more builds will run.

0
votes

Here is a bash script to bulk pause all (still not paused) build configurations by project name pattern via TeamCity REST API:

TEAMCITY_HOST=http://teamcity.company.com
CREDS="-u domain\user:password"
curl $CREDS --request GET "$TEAMCITY_HOST/app/rest/buildTypes/" \
| sed -r "s/(<buildType)/\n\\1/g" | grep "Project Name Regex" \
| grep -v 'paused="true"' | grep -Po '(?<=buildType id=")[^"]*' \
| xargs -I {} curl -v $CREDS --request PUT "$TEAMCITY_HOST/app/rest/buildTypes/id:{}/paused" --header "Content-Type: text/plain" --data "true"