自托管的 gitlab 运行器。 PATH 环境变量的内容与常规 powershell 不同

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

经过一些 我终于能够在我的个人笔记本电脑上设置一个自托管的 GitLab Runner。

我现在正在研究这个 运行ner 是如何工作的,以及如何根据我的需要调整它的环境。我将 YML 文件修改为 运行 一个与 PATH 环境变量相呼应的简单命令:

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

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

当我提交并将其推送到 GitLab 时,管道显示以下环境:

C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\Users1479\AppData\Local\Programs\Python\Python39\Scripts\
C:\Users1479\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:\Users1479\AppData\Local\SumatraPDF
C:\texlive21\bin\win32
C:\Program Files (x86)\PDFtk Server\bin\
C:\Program Files (x86)\Lua.1
C:\Program Files (x86)\Lua.1\clibs
C:\Program Files\Inkscape\bin
C:\Program Files\dotnet\
C:\Program Files\WindowsPowerShell\Scripts
C:\Program Files (x86)\Windows Kits\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

然后我 运行 从 运行ner 主机(即我的本地机器)上的常规 powershell 终端执行完全相同的命令,并得到以下信息:

C:\Users1479\Miniconda3\envs\temp
C:\Users1479\Miniconda3\envs\temp\Library\mingw-w64\bin
C:\Users1479\Miniconda3\envs\temp\Library\usr\bin
C:\Users1479\Miniconda3\envs\temp\Library\bin
C:\Users1479\Miniconda3\envs\temp\Scripts
C:\Users1479\Miniconda3\envs\temp\bin
C:\Users1479\Miniconda3\condabin
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\Users1479\AppData\Local\Programs\Python\Python39\Scripts
C:\Users1479\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:\Users1479\AppData\Local\SumatraPDF
C:\texlive21\bin\win32
C:\Program Files (x86)\PDFtk Server\bin
C:\Program Files (x86)\Lua.1
C:\Program Files (x86)\Lua.1\clibs
C:\Program Files\Inkscape\bin
C:\Program Files\dotnet
C:\Program Files\WindowsPowerShell\Scripts
C:\Program Files (x86)\Windows Kits\Windows Performance Toolkit
C:\Program Files (x86)\dotnet
C:\Program Files (x86)\glab
C:\Program Files\Git\cmd
C:\Users1479\AppData\Local\Microsoft\WindowsApps
C:\Users1479\.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\bin.0.22000.0\x64
C:\Users1479\handle
C:\Program Files\Google\Chrome\Application
C:\Users1479\AppData\Local\Pandoc

考虑到我的 运行ner 我的本地机器有 Powershell shell config.toml.

中的执行者

为什么 Runner 中的 PATH 和常规 Powershell 环境中的 PATH 内容不同?

环境变量可能不同的原因有几个。主要是:

  1. 跑步者正在使用的用户帐户
  2. 您在本地使用的 powershell 配置文件(跑步者不会使用)
  3. 在运行器的 config.toml
  4. 中对环境变量所做的任何更改
  5. 环境变量 changed/added 到 CI/CD 变量。

用户帐号

有效的PATH系统环境变量和用户环境变量的组合。为了让您的运行程序反映您在 运行 powershell 时在本地看到的相同环境变量,您必须使用相同的用户帐户,否则您看到的用户环境变量可能 missing/different 基于用户帐户。

修复可能由用户引起的差异的一种方法是更改​​ gitlab 服务使用的用户

要更改 GitLab runner 使用的用户,请转到 services -> gitlab-runner ->(右键单击) 属性 -> 登录 选项卡并选择跑步者应该使用的帐户。

或者,在安装运行器时指定:

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

但是,在您的情况下,您似乎使用的是同一个用户(C:\Users1479\ 出现在所提供的两个示例中)。所以这可能不是您遇到的具体问题(但我在这里提到它是为了让遇到这个问题的其他人受益)。

Powershell 配置文件

您的差异可能是 powershell 配置文件(未)使用的结果。 Powershell 配置文件可以改变环境变量,例如 PATH.

许多程序,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

要解决此问题,您可以执行以下任一操作:

  1. 将这些 PATH /环境变量添加到用户环境变量(系统 -> 环境变量)
  2. PATH/环境变量添加到系统
  3. config.toml 中运行器的 environment 键中进行必要的环境更改。参见 advanced configuration
  4. 将变量添加为 CI/CD variables