How to Dynamically Obtain User Profile Path in Self hosted Windows Agent for Bitbucket Pipelines

Gajesh Bhat
Contributor
July 4, 2024

Hello everyone,

I'm running a pipeline on a Self hosted Windows runner in Bitbucket Pipelines and need to clean the temporary files located in atlassian_runners\atlassian-bitbucket-pipelines-runner\temp\*  as not doing that while running docker containers in windows using the runner will move the runner into unhealthy state. (I have built my own wrapper around the self hosted windows runner as Windows Docker Runners aren't supported in Bitbucket Pipelines yet).  See my other question for more hints on how I am doing it.  https://community.atlassian.com/t5/Bitbucket-questions/Issues-running-Windows-Docker-Container-on-Pipelines-Self-hosted/qaq-p/2735498 However, I'm struggling to dynamically obtain the user profile path. My goal is to avoid hardcoding the user profile path and handle it dynamically within the script.

Here's what I've tried so far:

  1. Using [System.Environment]::GetFolderPath('UserProfile')

     $UserProfile = [System.Environment]::GetFolderPath('UserProfile')
  2. $TempPath = "$UserProfile\atlassian_runners\atlassian-bitbucket-pipelines-runner\temp\*"
  3. Remove-Item -Path $TempPath -Recurse -Force

This returns the system profile path (C:\Windows\system32\config\systemprofile) instead of the intended user profile path. It seems that the script runs under a different context (Possibly in a Sandboxed JVM environment) in the Bitbucket pipeline.

Questions:

  1. How can I reliably obtain the user profile path dynamically in a Bitbucket pipeline running on a Windows agent?
  2. Is there a known issue with the Windows agent in Bitbucket Pipelines that affects retrieving the user profile path?
  3. Are there any best practices or alternative methods to achieve this?

Any help or guidance would be greatly appreciated!

Regards,

Gajesh

1 answer

1 vote
Theodora Boudale
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 9, 2024

Hi Gajesh,

The environment variable $env:USERPROFILE should give you the path to the current user's home directory during a Pipelines build with self-hosted Windows runners.

If the runner goes into an unhealthy state consistently, I suggest addressing that issue first. Possible causes for the runner being unable to clean the build directory are

  • The permissions for one or more files (or directories) within a build directory are preventing the runner from deleting files.
  • A program or service is accessing one or more files (or directories) in the build directory at the time of cleanup.

I suggest looking into these two possible causes first to prevent the runner from going into an unhealthy state.

Kind regards,
Theodora

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
TAGS
AUG Leaders

Atlassian Community Events