27

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?

1
  • Answer according to the title is here.
    – Matthias
    Aug 17, 2017 at 13:34

4 Answers 4

40

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
5
  • Thanks, 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
    Nov 8, 2012 at 23:47
  • @Marcus Is this syntax correct? copy $env:system_teamcity_build_checkoutDir\chromedriver.exe $env:system_teamcity_build_tempDir Dec 4, 2015 at 13:20
  • It looks like some parameters drop the "system" prefix. e.g. %system.build.vcs.number% is actually $env:build_vcs_number.
    – Elfalem
    Nov 16, 2017 at 16:52
  • 1
    This 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.
    – sirdank
    Nov 27, 2018 at 17:19
  • "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 as env.build_number. You have to actually name the Env variable as env.build_number to refer to it as env.build_number. Oct 19, 2019 at 10:29
17

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.

6

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.

2
  • Thanks for this answer as well. This will be quite useful in the case I use the Script input mode in TeamCity.
    – Paul
    Nov 8, 2012 at 23:49
  • I preffer this way because my scripts can be decoupled from Teamcity or another environment. Nov 9, 2012 at 7:49
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.