MindingData MindingData - 2 months ago 20x
PowerShell Question

Saving an Environment Variable back to Team City from Powershell

We have a need that periodically, we will run a build configuration that among other things, recreates tokens/logins etc. We want to save these back to Team City as Environment variables. Builds that we subsequently do will want to look at this Environment Variable store and do a string replace within our configurations as required.

I've taken a look at :

##teamcity[setParameter name='env.TEST' value='test']

But from reading the documentation, this is only used to pass variables between build steps within the same build. It doesn't actually save the variable back to Team City.

Is there any way (Preferably from a powershell script), to call Team City and tell it to add a Environment Variable (Or any other variable).


In order to persist a value back to a parameter you have to call the REST API.

I use a PowerShell script that acts as a wrapper around the Invoke-RestMethod cmdlets in PowerShell 3+ that can be reused in a build step to achieve what you want.

Step 1.

Save the script to a PowerShell file and add it to your source control rest-api-wrapper.ps1

Step 2.

Create a PowerShell build step referencing the script and pass in the following arguments, tailored for your situation






enter image description here

More details can be found here - TeamCity Documentation

Hope this helps