Setting working directory for Powershell build step

Answered

Hi,

I am trying to run a powershell script as a build step and I need to change the working directory for the script. However, when running the build configuration, it is reporting a runner error due to script being looked for in the agent location. The goal is to use a variable for setting the working directory, but not even forcing an absolute path from the step configuration page is working.

Am I missing something in the configuration here? Using Command Line steps is not giving this problem when using the Working Directory / Script File items to run a batch script. Running 2020.1.1 with build 78657

Regards,

Olga

0
6 comments

This seems to be a bug. When running a step using PowerShell, value from working directory is ignored when Script = File. But when using Script = Source Code and the same file is called, it is executed without problem. (test.ps1 is just executing Get-Location)

Workaround is to use custom script, keep the working directory parameter and have the script launch the wanted file

 

 

0
Avatar
Fedor Rumyantsev

Hello Olga,

The working directory property (https://www.jetbrains.com/help/teamcity/build-working-directory.html) is interpreted not as a directory where to look for the script in question, but rather as a directory which will be considered current when the script will be executed. This is a direct equivalent of cd-ing (https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/cd) into a different directory before the script execution. 
PowerShell runner will always try to locate the script relative to the checkout directory. The workaround you have implemented will work, though, if a script is stored on the agent side in a static manner. 

0

I see. We will use the work around then.

It might be work adding then more information on the tooltip or documentation page. We were switching from CommandLine to Powershell scripts and the property is called the same way but seems to be working differently on the two build steps.

0
Avatar
Fedor Rumyantsev

Hello Olga,

I agree, Command Line runner has the different logic here, particularly because it will swap the context first, then look out for the executable. I have reached out internally to ask for the Powershell step documentation to specifically mention that the file-based execution will use the checkout directory instead of working directory for the file lookup - thank you for the notice!

0
Avatar
Fedor Rumyantsev

Just a quick update here; after internal discussion, I have registered a feature request: https://youtrack.jetbrains.com/issue/TW-66762 to make the PowerShell runner logic around the working dir the same as in the Command Line (and other similar runners). Please feel free to keep an eye on it and vote as you see fit.

0

Thanks for the update! I will keep an eye on the ticket. :)

0

Please sign in to leave a comment.