Jenkins: Failed to connect to repository
Solution 1
The problem was that somehow I created the ssh files with the root user. So the files owner was root.
The solution was just change the ownership to the jenkins user.
chown jenkins id_rsa.pub
chown jenkins id_rsa
Solution 2
I had the exact same problem. The way I solved it on Mac is this:
- Switch to jenkins user (sudo -iu jenkins)
- Run: ssh-keygen (Note - You are creating ssh key pairs for jenkins user now. You should see something like this : Enter file in which to save the key (/Users/Shared/Jenkins/.ssh/id_rsa):
- Keep pressing Enter for default value till end
- Run the command showing in the Jenkins error message, on your teminal (eg : "git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD")
- You will be asked if you want to continue. Say yes
- The Github repo will be added to your known_hosts file in : /Users/Shared/Jenkins/.ssh/
- Go back to Jenkins portal and try your Github SSH url
- It should work. Good Luck
Solution 3
I faced a similar issue when I tried to connect jenkins in my Windows server with my private GIT repo. Following is the error returned in the source code management section of Jenkins job
Failed to connect to repository : Command "git.exe ls-remote -h ssh://git@my_server/repo.git HEAD" returned status code 128: stdout: stderr: Load key "C:\Windows\TEMP\ssh4813927591749610777.key": invalid format git@my_server: Permission denied (publickey). fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
This error is thrown because jenkins is not able to pick the private ssh key from its user directory. I solved this in the following manner
Step 1
In the jenkins job, fill up the following info under Source Code Management
Repositories
Repository URL: ssh://git@my_server/repo.git
Credentials: -none-
Step 2
In my setup jenkins is running under local system account, so the user directory is C:\Windows\System32\config\systemprofile
(This is the important thing in this setup that is not very obvious).
Now create ssh private and public keys using ssh-keygen -t rsa -C "key label"
via git bash
shell. The ssh private and public keys go under .ssh directory of your logged in user directory. Just copy the .ssh folder and paste it under C:\Windows\System32\config\systemprofile
Step 3
Add your public key to your GIT server account. Run the jenkins job and now you should be able to connect to the GIT account via ssh from jenkins.
Solution 4
In our case git
had to be installed on the Jenkins server.
Solution 5
This is a very tricky issue - even if you're familiar with how things are working in https with certificates (OTOH if you see my workaround, it seems very logical :)
If you want to connect to a GIT repository via http(s) from shell, you would make sure to have the public certificate stored (as file) on your machine. Then you would add that certificate to your GIT configuration
git config [--global] http.sslCAInfo "certificate"
(replace "certificate" with the complete path/name of the PEM file :)
For shell usage you would as well e.g. supply a '.netrc' provding your credentials for the http-server login. Having done that, you shall be able to do a 'git clone https://...' without any interactive provisioning of credentials.
However, for the Jenkins-service it's a bit different ... Here, the jenkins process needs to be aware of the server certificate - and it doesn't use the shell settings (in the meaning of the global git configuration file '.gitconfig') :P
What I needed to do is to add another parameter to the startup options of Jenkins.
... -Djavax.net.ssl.trustStore="keystore" ...
(replace "keystore" with the complete path/name like explained below :)
Now copy the keystore file of your webserver holding the certificate to some path (I know this is a dirty hack and not exactly secure :) and refer to it with the '-Djavax.net.ssl.trustStore=' parameter.
Now the Jenkins service will accept the certificate from the webserver providing the repository via https. Configure the GIT repository URL like
Note that you still require the '.netrc' under the jenkins-user home folder for the logon !!! Thus what I describe is to be seen as a workaround ... until a properly working credentials helper plugin is provided. IMHO this plugin (in its current version 1.9.4) is buggy.
I could never get the credentials-helper to work from Jenkins no matter what I tried :( At best I got to see some errors about the not accessible temporary credential helper file, etc. You can see lots of bugs reported about it in the Jenkins JIRA, but no fix.
So if somebody got it to work okay, please share the knowledge ...
P.S.: Using the Jenkins plugins in the following versions:
Credentials plugin 1.9.4, GIT client plugin 1.6.1, Jenkins GIT plugin 2.0.1
Related videos on Youtube
adolfosrs
A 28 years old developer who loves working hard on new technologies and believes that programming and innovation are the keys to find solutions that make people's lives easier. Hey! :) Did I help you? What about a BTC tip? 39TRzfRDW3iigBaZXXTAGS1u2KLjRLM3TM
Updated on July 09, 2022Comments
-
adolfosrs almost 2 years
I'm trying to connect jenkins on a github repo.
When I specify the Repo URL jenkins return the following error message:
Failed to connect to repository : Command "git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD" returned status code 128: stdout: stderr: Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
When using the HTTPS:// Url jenkins returns:
Failed to connect to repository : Failed to connect to https://github.com/adolfosrs/jenkins-test.git (status = 407)
I could successfully clone the repo at the same machine where jenkins is running and I also run the
git ls-remote -h [email protected]:adolfosrs/jenkins-test.git HEAD
command. So I have the right SSH at github.-
Michael Ver about 10 yearsMake sure you are logged in as the user that Jenkins runs as and check connection to githib
-
adolfosrs about 10 yearsWhat do you mean by "logged in as the user that Jenkins runs"? Logged in where? I could clone the repo so I have connection with github.
-
Michael Ver about 10 yearsJenkins runs as the user jenkins and has its own .ssh directory to store the list of public keys and known_hosts.
-
Michael Ver about 10 yearsTry sudo -i -u jenkins; git clone [email protected]:adolfosrs/jenkins-test.git
-
adolfosrs about 10 yearsOk. You are right. But now i'm getting "Failed to add the host to the list of known hosts (/var/lib/jenkins/.ssh/known_hosts). Permission denied (publickey)." Any idea?
-
-
Juergen Klasen about 10 yearsOkay, I forgot to mention that I'm - in my context - NOT talking about GITHUB, but an own repository server which is only accessible via https transport :)
-
IgorGanapolsky over 7 yearsI still get Permission denied (publickey).
-
IgorGanapolsky over 7 yearsWhat do you mean by
Select Username and Authentication key
? There is no such exact option. -
Harshal Vaidya over 7 yearsAuthentication key is your private key for connecting to GIT server
-
Moshisho over 7 yearsExact option is
SSH Username with private key
and you can also directly insert the private key you generated on GitHub or select the file that has the private key from your Jenkins master file system. -
prayagupa about 7 yearsdid not work. Had to manually add private key to jenkins UI, which is weird
-
Joshua T almost 5 years@prayagupd I also had to add the private key generated within Jenkins container as a 'Credential' within Jenkins UI in order to allow code cloning.
-
yehanny about 3 yearsI think adding the host to jenkins known_hosts did the trick, I was trying only with the ssh keys, cheers!
-
Dami almost 3 yearsyes just did a
sudo yum install git
for CentOS and Jenkins works for git repo.