Using PATH in Git Configuration

Article ID:360048202171
6 minute readKnowledge base

Issue

  • Best Practice for configuring Git path

Resolution

Jenkins allows for multiple configurations of git for the Jenkins instance and in the agents. You may encounter errors using different paths for the Git executable in Jenkins and Agents, such as the following:

[poll] Last Built Revision: Revision XXXXXXXXX (refs/remotes/origin/master) using credential XXXXXXXXX
> /usr/local/git/bin/git --version # timeout=10
> /usr/local/git/bin/git --version # timeout=10
using GIT_ASKPASS to set credentials XXXXXXXXX Credentials
> /agent/git/path/git ls-remote -h https://repositoryURL.git # timeout=10
FATAL: hudson.plugins.git.GitException: Error performing command: /agent/git/path/git ls-remote -h https://repositoryURL.git
java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
...

In this case, this error appears during the polling action, and the polling is never performed. Nevertheless, if we run a manual build, it will work fine.

To avoid these kinds of issues, we can set a specific configuration where we will set the git executable file path in the $PATH environment variable, and we will set a relative path (using only the git command) for the git installations.

To fix this, perform the following steps for using the $PATH environment variable in the git installation:

Adding Git executable to the $PATH environment variable in the Jenkins instance

First, we will need to add the Git executable to the $PATH environment variable in the Jenkins instance. This process depends on the OS we have:

Adding Git executable to the $PATH environment variable in Linux or Unix based OS

If we are running the Jenkins instance over a Linux OS or Mac OS or based on Unix OS, we will need to perform the following steps for adding the Git executable to the $PATHenvironment variable:

  1. Login into the machine where the Jenkins instance is hosted as the same user who runs the Jenkins instance.

  2. Check if the git installation is included in the current $PATH environment variable running the following command in a terminal:

git --version
  1. An output like the following will mean that we have one git installation included in the $PATH environment variable.

git version 2.17.1

Additionally, we can check if this Git installation is the correct installation using the following command:

which git

If the output of the previous command is the path where the Git installation is located (which is the one you want to use), then you could go to the next step [#configuringgitexecutableinthejenkinscontroller]. Otherwise, it means you have more than one Git installation in your machine, and you may face some issues if you switch the Git installation in the path. Go to [#impossibletousepathenvironmentvariablewhatcanido]

In the other case, output like the following will mean that you don’t have one Git installation included in the $PATH environment variable, and you can add it.

bash: git: command not found
  1. For adding the Git installation to the $PATH environment variable, you only need to add the following line to the appropriate file that will be read when your shell is launched. There are a few different places where you could conceivably set the variable name: ~/.bash_profile, ~/.bashrc, etc. Please contact a more suitable team to know the best file to add the export line.

export PATH=$PATH:/place/with/the/file

Where /place/with/the/file is the path to the Git installation.

  1. Once you performed this change, you can log in again in your system and check it running the git --version command and the which git command.

  2. Jenkins will need to be restarted for the PATH environment variable change to be reflected in Jenkins.

Adding Git executable to the %PATH% environment variable in Windows OS

If we are running the Jenkins instance on Windows OS, we will need to perform the following steps for adding the Git executable to the %PATH%environment variable:

  1. Login into the machine where the Jenkins instance is hosted as the same user who runs the Jenkins instance.

  2. Check if the git installation is included in the %PATH% environment variable running the following command in a PowerShell or CMD terminal:

git --version
  1. An output like the following will mean that we have one git installation included in the %PATH% environment variable.

git version 2.27.0.windows.1

Additionally, We can check if this Git installation is the correct installation using the following commands:

where.exe git

If the output of the previous command is the path where the Git installation, which you want to use, is located, then you could go to the next step [#configuringgitexecutableinthejenkinscontroller]. Otherwise, it means you have more than one Git installation in your machine, and you may face some issues if you switch the Git installation in the path. Go to [#impossibletousepathenvironmentvariablewhatcanido]

In the other case, output like the following will mean that you don’t have one Git installation included in the %PATH% environment variable, and you can add it.

bash: git: command not found
  1. For adding the Git installation to the %PATH% environment variable, you will need to navigate to Edit the system environment variables window inside the Control Panel. Click in the Environment Variable.... Double-Click over the Path variable and add the path of the Git installation binary to the Path variable. Remember, you have two Path variables. You will need to choose the right one depending on your requirements (the Path variable inside the User Variables for ... section will affect only to the current user; and the Path variable inside the System variables section will affect to all users on the Windows machine). Please, contact a more suitable team for choosing the best Path variable to add the path of the Git installation binary.

  2. Once you performed this change, you can login again in your system and check it running the git --version command and the where.exe git command in a terminal.

Adding Git executable to the $PATH environment variable in the agents

For adding the Git executable path to the $PATH environment variable in the agents, we will need to follow the same steps of the [#addinggitexecutabletothepathenvironmentvariableinthejenkinsinstance], but running all commands in the agent machines.

Configuring Git executable in the Jenkins controller

Once the Git installation binary is correctly included in the $PATH environment variable of all machines of your system (Jenkins controller and agents) is time to modify the git configuration of your Jenkins instance.

  1. Login into the Jenkins instance as an administrator.

  2. Go to Manage Jenkins -> Global Tool Configuration -> Git -> Git installations

  3. Type git in the Path to Git executable of each Git installation where you want to use the Git installation binary included in the $PATH environment variable.

Results

Once you have performed the previous steps, the Git installation binary included in the $PATH environment variable will be used in all jobs which use the configured Git installation tool.

Impossible to use the $PATH environment variable. What can I do?

Sometimes, we can’t configure a specific Git installation binary in the $PATH environment variable due to some situations: a multiple git installation in the agent or controller machines; multiples git versions in the same machine; etc.

In this case, we will need to be more specific, setting the git path in each system, and we will not use the $PATH environment variable. Initially, we have three locations where we can change the git configuration inside the Jenkins instance:

In the Global Tool Configuration

In Manage Jenkins -> Global Tool Configuration -> Git -> Git installations we can add and edit any Git installation, setting the Path to Git executable. Here, we can leave the Install automatically checkbox unchecked, and each time Jenkins uses this git installation, it will search for the Git binary in the Path to Git executable folder wherever this Git installation is used (in the agent or the controller). Therefore, we can face some issues if we have a different path of the Git executable in the controller and the agent.

In the node configurations

In Manage Jenkins -> Manage Nodes, we can modify the Git binary path for each Git installation (added in the Global Tool Configuration section). Once we are in Manage Nodes, we can click over Configure for each agent. Inside the Configuration page of the agent, we can check the Tool Locations checkbox and add a new location for the specific Git installation.

Tool-locations

Where:

  • Name: the git installation that you want to change the Git binary location.

  • Home: the path of the Git installation binary on this agent machine.

In the job configuration

You can choose which Git installation to use in the Job configuration or the Pipeline code. The steps to configure this are: gitTool and https://plugins.jenkins.io/git/#configuration