Failed to connect to repository: Error performing git command ls-remote -h

During a continuous integration cycle, one of the initial steps is to fetch the code from source code repository. Just before fetching the code, we need to connect the orchestration tool used for continuous integration (CI) with the source code repository.

Now suppose we are using Jenkins as the orchestration tool and GitHub as the source code repository. When we try to connect Jenkins with the remote GitHub repository, we might encounter this error “Failed to connect to repository: Error performing git command ls-remote -h“. In this post we will discuss few points that we can check when this error occurs which can be helpful in resolution of this error.

Here we will discuss one of the many scenarios in which this error can occur.

Scenario

We will create a Jenkins pipeline project for this scenario, however, this error can also occur in other types of Jenkins projects when trying to connect to GitHub.

Once a pipeline project is created, 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.

Pipeline syntax screen

In the Pipeline section, select Select SCM as Git. We 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

Learn more about GitHub and Jenkins:

How to integrate a GitHub repository with a Jenkins pipeline

Trigger a Jenkins job on code push event in Github via webhooks

To read more posts on GitHub and GitHub Actions Click here

To read more posts on CI CD with Jenkins  Click here

Resolution

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
  • Authentication

Git Installation

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.

Git Configuration

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.

Manage Jenkins –> Tools

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.

Authentication

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. The error implies that you have a private GitHub repository which needs to be authenticated in order to fetch the code.

Pipeline syntax screen

In this case as mentioned in the error description, in order to access the private GitHub repository, we need to use a personal access token to authenticate the user. To understand how to create a personal access token in GitHub 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.

Add Credentials screen

In this screen you need to provide the username and mention the token as the password. You can provide a description for reference eg GitHub PAT and you can mention the same description text 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 select list.

This should authenticate your repository and remove the error and you should be able to fetch the code from your GitHub repository with pipeline script.

Conclusion

We faced the error “Failed to connect to repository…” as we were unable to connect to the GitHub repository. We discussed few possible reasons for it like “missing installations”and /or” configuration for git”. Furthermore, we must also ensure authentication of GitHub repository from Jenkins using personal access token.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *