MindingData
MindingData

Reputation: 12460

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).

Upvotes: 2

Views: 1350

Answers (1)

Matt
Matt

Reputation: 3704

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

%teamcity.serverUrl%/httpAuth/app/rest/projects/project_id/parameters/parameter_name

"Username"

"Password"

"PUT"

"TheValueToSave"

enter image description here

More details can be found here - TeamCity Documentation

Hope this helps

Upvotes: 6

Related Questions