Failed to connect to repository: Error performing git command ls-remote -h: GitHub error in Jenkins
In this post we will discuss few points to consider when this error occurs while trying to connect to GitHub repository from Jenkins.
For clarity, let us try to re-create the scenario where we might face this error using Jenkins pipeline project.
Learn more about GitHub:
To read other posts regarding GitHub and GitHub Actions Click here
Learn more about Jenkins:
To read other posts regarding CI CD with Jenkins Click here
Here we will discuss one such scenario where this error can occur, this error can also occur in any other types of Jenkins projects where we are trying to connect to GitHub.
Let us go to the project dashboard and then select Configure from the project menu. In the Configure screen, we will go to the Pipeline section.
In the Pipeline section, select Select SCM as Git. We will now provide the URL of the GitHub repository in the box labeled as URL. Now if we move the cursor away from the box, we might see this error.
Failed to connect to repository: Error performing git command C:\Program Files\Git\bin\git.exe ls-remote -h https://github.com/<githubusername>/<githubreponame> HEAD
The most probable reason for occurrence this error is related to git installation on the system.
Hence we need to check the following:
- Git Installation
- Git Configuration
Check whether Git is installed on your system. If not, then you should download git for your OS from https://git-scm.com/downloads and complete the installation.
Once git is installed, the next step is to provide the git installation path in Jenkins. For this, go to Manage Jenkins —> Tools —> Git Installations.
In the Name textbox, provide the name by which you might want to refer to this installation from the script e.g. Git. In the Path to Git executable section, you need to provide the complete path to the git executable on your drive. If you have already set the path to the git executable in the PATH environment variable on the system, then you might only mention git.exe.
If you are now able to connect with your Github repository, you might still encounter this error. However, in this case, there would be further details below this error as shown in the snapshot below.
The reason for this error is authentication failure. That means you have a private GitHub repository which needs to be authenticated in order to fetch the code.
In this case as mentioned in the error description, we need to use a personal access token to authenticate the user. To understand how to create a personal access token in GitHub use personal access token to authenticate, follow the steps mentioned in the post Generate fine grained personal access token in GitHub
Once you are able to create a personal access token in GitHub, the next step is to create credentials in Jenkins. In the Credentials box, select Add New, which will open the Add Credentials screen.
Here you can select the username password credentials as shown below. Provide the username and mention the token in the password section. Provide a description for reference eg GitHub PAT and provide the same in the ID section. Save the. Credentials. Once you are back on the Configure project screen, you should be able to select the new credentials from the Credentials box.
This should authenticate your repository and remove the error.
Now you should be able to work with the code in your GitHub repository from your pipeline script.
We faced the error “Failed to connect to repository…” as we were unable to connect to the GitHub repository due to missing installations and /or configuration for git. Furthermore, this also required authentication of github repo from Jenkins using personal access token.