Home > other >  Self-hosted gitlab runner. PATH environment variable has different contents than regular powershell
Self-hosted gitlab runner. PATH environment variable has different contents than regular powershell

Time:01-01

After some initial problems I was finally able to set up a self-hosted GitLab Runner on my personal laptop.

I'm now looking into how this runner works and how I can tweak it's environment to my needs. I modified the YML file to run a simple command echoing the PATH environment variable:

stages:          # List of stages for jobs, and their order of execution
  - run 

deploy-job:
  stage: run
  script:
    - echo $env:path.split(";")

When I commit and push this up to GitLab, the pipeline displays the following environment:

C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\Users\921479\AppData\Local\Programs\Python\Python39\Scripts\
C:\Users\921479\AppData\Local\Programs\Python\Python39\
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Windows\System32\OpenSSH\
C:\ProgramData\chocolatey\bin
C:\Users\921479\AppData\Local\SumatraPDF
C:\texlive\2021\bin\win32
C:\Program Files (x86)\PDFtk Server\bin\
C:\Program Files (x86)\Lua\5.1
C:\Program Files (x86)\Lua\5.1\clibs
C:\Program Files\Inkscape\bin
C:\Program Files\dotnet\
C:\Program Files\WindowsPowerShell\Scripts
C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\
C:\Program Files (x86)\dotnet\
C:\Program Files (x86)\glab
C:\Program Files\Git\cmd
C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps
C:\Program Files\Oracle\VirtualBox
C:\Program Files (x86)\Oracle\VirtualBox

I then ran the exact same command from a regular powershell terminal on the runner host (i.e. my local machine), and got the following:

C:\Users\921479\Miniconda3\envs\temp
C:\Users\921479\Miniconda3\envs\temp\Library\mingw-w64\bin
C:\Users\921479\Miniconda3\envs\temp\Library\usr\bin
C:\Users\921479\Miniconda3\envs\temp\Library\bin
C:\Users\921479\Miniconda3\envs\temp\Scripts
C:\Users\921479\Miniconda3\envs\temp\bin
C:\Users\921479\Miniconda3\condabin
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\Users\921479\AppData\Local\Programs\Python\Python39\Scripts
C:\Users\921479\AppData\Local\Programs\Python\Python39
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0
C:\Windows\System32\OpenSSH
C:\ProgramData\chocolatey\bin
C:\Users\921479\AppData\Local\SumatraPDF
C:\texlive\2021\bin\win32
C:\Program Files (x86)\PDFtk Server\bin
C:\Program Files (x86)\Lua\5.1
C:\Program Files (x86)\Lua\5.1\clibs
C:\Program Files\Inkscape\bin
C:\Program Files\dotnet
C:\Program Files\WindowsPowerShell\Scripts
C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit
C:\Program Files (x86)\dotnet
C:\Program Files (x86)\glab
C:\Program Files\Git\cmd
C:\Users\921479\AppData\Local\Microsoft\WindowsApps
C:\Users\921479\.dotnet\tools
C:\Program Files\Tracker Software\PDF Editor
C:\context\tex\texmf-win64\bin
C:\Program Files (x86)\Symantec\Symantec Endpoint Protection
C:\Program Files (x86)\Windows Kits\10\bin\10.0.22000.0\x64
C:\Users\921479\handle
C:\Program Files\Google\Chrome\Application
C:\Users\921479\AppData\Local\Pandoc

I'm a bit surprised that the paths are different, considering my runner is my local machine with a Powershell shell executor in the config.toml.

Why does PATH in the Runner and PATH from a regular Powershell environment have different contents?

CodePudding user response:

There's a few reasons why environment variables may be different. Chiefly:

  1. The user account being used by the runner
  2. The powershell profile you're using locally (which will not be used by the runner)
  3. Any changes to environment variables made in the runner's config.toml
  4. environment variables changed/added through CI/CD variables.

User account

The effective PATH is a combination of both the system environment variables as well as user environment variables. For your runner to reflect the same environment variables that you see locally when running powershell, you must use the same user account, otherwise user environment variables you're seeing may be missing/different based on the user account.

One way to fix differences that may be caused by the user would be to change the user used by the gitlab service

To change the user used by the GitLab runner, go to services -> gitlab-runner -> (right-click) properties -> Log On tab and choose the account the runner should use.

Alternatively, specify this when installing the runner:

.\gitlab-runner.exe install --user ENTER-YOUR-USERNAME --password ENTER-YOUR-PASSWORD

However, in your case you seem to be using the same user (C:\Users\921479\ being present in both examples provided). So this may not be the specific problem in your case (but I mention it here for the benefit of others who come across the question).

Powershell profile

Your differences are likely the result of the powershell profile (not) being used. Powershell profiles can alter, among other things, environment variables like PATH.

Many programs, including Anaconda's conda init powershell make use of powershell profiles to effect PATH changes. However, the GitLab Runner shell executor for powershell passes the -NoProfile argument when running powershell commands for jobs, meaning any such profile changes won't be present in the GitLab job, even if the same correct user account is used. At this time, it is not possible to override this behavior, but there is a feature request for it.

To work around this problem, you can do any of the following:

  1. add these PATH /environment variables to the User environment variables (System -> Environment Variables)
  2. Add the PATH /environment variables to the system
  3. Make the necessary environment changes in the runner's environment key in the config.toml. See advanced configuration.
  4. Add the variables as CI/CD variables.
  • Related