Modify PATH for gitlab-runner
Asked Answered
C

5

13

I want to install a gitlab-runner (executor shaell) on my Windows 10 box. I start the job on the gitlab server and it always ends up with the message the command "git" cannot be found (roughly translated into english). As a matter of fact git is not part of my path. How can I modify my PATH variable for the shell the gitlab-runner starts?

To use git on the command line in windows I usually set it with the statement: PATH %PATH%C:\Program Files\Git\bin.

Is it documented somewhere, git has to be available to the runner? How can I see the command line the runner invokes (i.e. the call to git)?

Charybdis answered 15/9, 2017 at 15:4 Comment(0)
P
12

This GitLab Runner issue answers your question.

The environment setting doesn't work since it gets evaluated before the variables are set, but you can use pre_build_script in the runner config to update the path.

[[runners]]
  name = "My Runner"
  url = "https://gitlab.com/"
  token = "Abcd1234"
  executor = "shell"
  pre_build_script = "set Path=%GIT_HOME%\\usr\\bin;%Path%"
Parachronism answered 7/5, 2019 at 20:53 Comment(1)
Since my runner was using powershell, I had to use pre_build_script = "$Env:PATH += \";C:\\Program Files\\7-Zip\"", but it worked. Thanks.Nieberg
C
6

For testing purposes I started the gitlab-runner like: gitlab-runner -l debug --debug run --config config.toml --service gitlab-runner from the directory where the gitlab-runner.exe and the config.toml file reside.

I added the following line to the runners section of my config.toml file:

environment = ['PATH=%PATH%;d:/java/bin;C:/Program Files/Git/bin;c:/program files (x86)/apache-ant-1.10.1/bin']

Charybdis answered 15/9, 2017 at 15:54 Comment(2)
I tried your solution but I still have the error message : "the command "git" cannot be found"...Carpogonium
It seems the gitlab-runner does the variable expansion using the syntax ${ENV_VAR} even on Windows. However, it also seems that one cannot use the original value of the environment variables. So when setting environment = ['SOME_VALUE=foo','PATH=${PATH};${SOME_VALUE}'], I get the value ;foo for %PATH%. So the variable of SOME_VALUE is expanded as expected but PATH is expanded to an empty value.Chrysarobin
H
1

On macOS the only thing that really worked for me is this solution of adding the PATH to Library/LaunchAgents/gitlab-runner.plist:

<key>EnvironmentVariables</key>
<dict>
    <key>PATH</key>
    <string>/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>

Then restart gitlab-runner and you are good to go.

I suppose there is a Windows equivalent of this solution as well.

Halla answered 7/11, 2019 at 11:16 Comment(1)
It seems Windows lacks this feature: "The service control manager does not support passing custom environment variables to a service at startup. Also, the service control manager does not detect and pass on changes to environment variables as the service is running. Instead of making a service dependent on an environment variable, use registry values or ServiceMain arguments." from learn.microsoft.com/en-us/windows/win32/services/…Invagination
V
0

One can also configure the shell executor environment editing $HOME/.profile (or $HOME.bash_profile) of the gitlab-runner user.

To check the gitlab-runner home directory, run $ sudo cat /etc/passwd | grep gitlab-runner | cut -d: -f6 .

Vulgate answered 25/7, 2022 at 15:27 Comment(0)
N
0

On Windows 11 I use:

[[runners]]
  name = "win11"
  ...
  environment = ['PATH=%PATH%;C:\Git\cmd']

As my Git installation is under c:\git – adjust this path.

Reference: https://forum.gitlab.com/t/runner-git-not-recognized-windows-11/106053/3

Nino answered 14/6 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.