Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to access TeamCity system properties in a Powershell script?

I'm trying to set up a new build configuration in TeamCity using the Powershell runner. However, I can't seem to find a way to access the TeamCity System Properties in the build script. I've seen hints that it is possible, but cannot find documentation on how to do it.

I have tried accessing the system properties using Powershell variable syntax, $variable. I have also printed out all variables in memory and see no teamcity variables to use.

Is this possible with the Powershell runner, and if so what is the syntax necessary to get it working?

like image 241
Paul Avatar asked Nov 07 '12 21:11

Paul


2 Answers

TeamCity will set up environment variables, such as build.number (you can see a list of these within TeamCity).

In Powershell you can access environment variables using the env "provider", e.g.

$env:PATH

TeamCity variables are accessible by replacing the . with a _, so the build.number variable can be accessed as

$env:build_number
like image 153
Marcus Avatar answered Nov 20 '22 07:11

Marcus


As it says in the TeamCity documentation, the system parameters are passed to the build script runner, but not all build script runners know what to do with them. In the case of the Powershell script runner, when using a script file, they don't propagate down to your scripts.

It's occurred to me to write a psake-optimized build runner that does, but in the meantime you can do one of the following:

  • explicitly map any of the TeamCity build properties to script parameters using the parameter expansion that's available within the Script Source box. eg .\build.ps1 -someParam:%build.name%

  • use environment parameters, which can be accessed explicitly within PowerShell using $env:NAME_IN_TEAMCITY syntax, eg $env:TEAMCITY_VERSION, or looped over and pushed into variable scope

  • access the build properties file that TeamCity makes available during the build. The file is available at $env:TEAMCITY_BUILD_PROPERTIES_FILE, and if you load the XML version it's fairly easy to loop through and push them all into scope (though you do get everything as a string of course). I posted a gist on how to do this (https://gist.github.com/piers7/6432985). Or, if using Psake, modify the script above to return you a hashtable which you can pass directly to Psake's -properties argument.

like image 17
piers7 Avatar answered Nov 20 '22 07:11

piers7