Is there a way to access TeamCity system properties in a Powershell script?
Solution 1
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
Solution 2
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 scopeaccess 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.
Solution 3
It is posible. Here is example how to pass system properties into PSake script:
& .\psake.ps1 -parameters @{build_number=%build.number%; personal_build=%build.is.personal%}
If you don't use Psake, you can define your variables like this:
$build_number = %build.number%
The %build.number%
part will be replaced with TeamCity provided data. I think, it works only in Source code script input mode.
Solution 4
I created a meta-runner that will pass through System parameters to parameters declared in the Powershell script. It's not perfect (if you put '@ in your source it will break) but it works for what I needed, you can find it here: https://gist.github.com/anonymous/ef60ada3f48f0fb25093
Related videos on Youtube
Paul
Full Stack Software Engineer and Solution Architect, specializing in .Net, JavaScript/NodeJS and Modern Frameworks, HTML and CSS, SQL and Document Databases, Cloud Systems, CMS, and more.
Updated on June 01, 2020Comments
-
Paul almost 4 years
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?
-
Matthias over 6 yearsAnswer according to the title is here.
-
-
Paul over 11 yearsThanks, this works. I was getting a bit hung up trying to get System Properties to work, but Environment Variables work perfectly for my needs right now. I'm not sure I even need System Properties at all now.
-
Paul over 11 yearsThanks for this answer as well. This will be quite useful in the case I use the Script input mode in TeamCity.
-
Aleš Roubíček over 11 yearsI preffer this way because my scripts can be decoupled from Teamcity or another environment.
-
alansiqueira27 over 8 years@Marcus Is this syntax correct? copy $env:system_teamcity_build_checkoutDir\chromedriver.exe $env:system_teamcity_build_tempDir
-
Elfalem over 6 yearsIt looks like some parameters drop the "system" prefix. e.g.
%system.build.vcs.number%
is actually$env:build_vcs_number
. -
sirdank over 5 yearsThis did not work for me.
$env:build_number
inside my powershell source code build step was always blank. Teamcity was, however, willing to replace%build.number%
directly in my powershell source code with my desired value. -
magicode118 over 4 years"TeamCity variables are accessible by replacing the . with a _". This can be misleading as one would think he would enter
env.build.number
in Env variables in TC and then refer to it asenv.build_number
. You have to actually name the Env variable asenv.build_number
to refer to it asenv.build_number
.