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

标签 powershell environment-variables gitlab-ci gitlab-ci-runner

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

我现在正在研究这个运行者的工作原理以及如何根据我的需要调整它的环境。我修改了 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:\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

然后,我从运行者主机(即我的本地计算机)上的常规 powershell 终端运行完全相同的命令,并得到以下结果:

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

考虑到我的运行程序是我的本地计算机,在 config.toml 中带有 Powershell shell 执行器,我对路径不同感到有点惊讶。

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

最佳答案

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

  1. 运行者正在使用的用户帐户
  2. 您在本地使用的 powershell 配置文件(运行程序不会使用该配置文件)
  3. 在运行程序的 config.toml 中对环境变量进行的任何更改
  4. 通过 CI/CD 变量更改/添加环境变量。

用户帐户

有效的PATH系统环境变量和用户环境变量的组合。为了让您的运行程序反射(reflect)您在运行 powershell 时在本地看到的相同环境变量,您必须使用相同的用户帐户,否则您看到的用户环境变量可能会丢失/因用户帐户而异。

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

要更改 GitLab 运行程序使用的用户,请转到 服务 -> gitlab-runner -> (右键单击)属性 -> 登录选项卡并选择运行者应使用的帐户。

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

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

但是,在您的情况下,您似乎使用相同的用户(C:\Users\921479\ 出现在提供的两个示例中)。因此,这可能不是您案例中的具体问题(但我在这里提到它是为了其他遇到该问题的人的利益)。

Powershell 配置文件

您的差异可能是由于(未)使用powershell 配置文件造成的。 Powershell 配置文件可以更改环境变量,例如 PATH

许多程序,including Anaconda's conda init powershell使用 powershell 配置文件来影响 PATH 更改。但是,powershell 的 GitLab Runner shell 执行器 passes the -NoProfile为作业运行 powershell 命令时的参数,这意味着即使使用相同的正确用户帐户,任何此类配置文件更改也不会出现在 GitLab 作业中。此时,无法覆盖此行为,但有一个 feature request为了它。

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

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

关于powershell - 自托管 gitlab 运行器。 PATH 环境变量的内容与常规 powershell 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70532683/

相关文章:

powershell - 如何使用 Powershell 在 IIS 10 上更新站点的 "physical path"?

windows - 为什么 Windows 7 中的 %AppData% 似乎指向错误的文件夹?

git - 从私有(private) gitlab 服务器克隆项目时出现问题

gitlab - 如何在 GitLab CI 的管道中将变量值从一个作业传递到下一个作业?

powershell哈希表问题

windows - Strawberry Perl——默认情况下编码转换在哪里完成?

powershell - 如何拥有父脚本但保持与 Powershell 脚本的关注点分离?

configuration - 使用环境变量设置类型列表的 Play 框架配置变量

linux - linux命令行查看进程的命令行和环境变量的方法

gitlab - 通过 CI runner 将文件推送到 gitlab-ci