How to do mass refactoring of TeamCity Build Configurations?

Answered

We've got a lot of TeamCity build configurations where two templates are attached. When you detach a template, all variables from a template are not removed but stays with this build configuration.

Is there any elegant way to detach build configurations AND remove all system/env variables defined in those template?

More specifically, is there any way to find all builds in a specific project and replace one env. variable by another one? I know that TeamCity has REST API but it doesn't seem to fulfill my needs.

I found a rest-client library and asked something similar in https://github.com/JetBrains/teamcity-rest-client/issues/88 but plans for this lib are pretty unclear.

0
2 comments

Hi Artem,

The easiest solution would be to use versioned settings in Kotlin DSL. I understand that right now you are not using versioned settings, but maybe this is a great opportunity to start using them.

Unfortunately, I cannot advise you on the rest-client you found, because it is not maintained by the TeamCity team. So it is best to wait for the answer to your question in the GitHub issue you created. But the project is alive as far as I know.

Other than that, you can try writing a script that will iterate through all build configurations via the REST API, find configurations in question and modify them. You can find all build configurations in a specific project using a GET request like this:

/app/rest/buildTypes?locator=project:PROJECT_ID

From this request, you can get a list of href values which you can iterate through, they will look like this:

"href": "/app/rest/buildTypes/id:TestProject_ReadTests",

Attaching or detaching a template should be easy, this section describes it. As for parameters, look for the parameters section, an example output in JSON is below, note that "inherited": true indicates that a particular parameter is inherited from a template. You can parse this section to find the desired parameters.

  "parameters": {
"count": 5,
"href": "/app/rest/buildTypes/id:TestProject_ReadTests/parameters",
"property": [
{
"name": "BranchSpec",
"value": "+:refs/heads/*",
"inherited": true
},
{
"name": "BuildNumber",
"value": "%build.counter% / %teamcity.build.branch%",
"inherited": true
},
{
"name": "CheckoutRule",
"value": "+:.=>.",
"inherited": true
},
{
"name": "DefaultBranch",
"value": "refs/heads/master",
"inherited": true
},
{
"name": "VcsTriggerBranchFilter",
"value": "+:*",
"inherited": true
}
]
},

Then you can use PUT/DELETE requests to alter or delete parameters as described here.

0
Avatar
Permanently deleted user

Mikhail Efremov, thank you for your answer! I hope we'll switch to Kotlin DSL sooner or later but it it's needed to solve real tasks :)

I was surprised that a template leaves all its parameters in a configuration when it's detached, I hoped you'll say 'nah, it's possible to detach and remove all the params simultaneously'. There is a clear way to do it via REST API though, thanks!

0

Please sign in to leave a comment.