0

In my TeamCity project I have a PowerShell build step, I need to get the current working dir of team city in the script. I tried this code to get it from the environment variables, however, the environment variable is apparently null:

"Working Dir: " + $env:teamcity_build_workingDir

How can I access the TeamCity variables, or how can I get the current working path of the TeamCity project?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128

3 Answers3

3

According to TeamCity documentation, there are no environment variable for the teamcity.build.workingdirectory-property.

System Property Name: teamcity.build.workingDir

Environment Variable Name: none

Description: Working directory where the build is started. This is a path where TeamCity build runner is supposed to start a process. This is a runner-specific property, thus it has different value for each new step.

You could try $pwd or Get-Location which returns PowerShell's current working directory. Hopefully the PowerShell process was started in the same working directory as the build-runner. Ex:

"Working Dir: " + $pwd
"Working Dir: " + (Get-Location)
Frode F.
  • 52,376
  • 9
  • 98
  • 114
0
$myDirectory = Split-Path $MyInvocation.MyCommand.Path

learn.microsoft.com

The incredible Jan
  • 741
  • 10
  • 17
0

For your PowerShell build step, you can make that set an environment variable before running your code. e.g. if you normally want to run my-script.ps1 you can change the build step to "source code" and then use something like

$env:TEAMCITY_BUILD_WORKINGDIR = "%teamcity.build.workingDir%"
. "./my-script.ps1"

Then you can use $env:TEAMCITY_BUILD_WORKINGDIR inside of your script(s).

Wilka
  • 28,701
  • 14
  • 75
  • 97