1

I'm trying to connect jenkins to a bitbucket git repository.

When I specify the repository URL in jenkins, it returns the following error message:

Failed to connect to repository : 
Command "git ls-remote -h <SSH_Git_url> 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.

I have successfully run the above command(where jenkins is running) using Git Bash and the known_hosts file has the correct host address along with the port.

I have also used the set git_ssh_command="ssh -i <ActualPathToPrivateKey>" before running the above command, to check if that helps.

Some other points(not sure if these add to the question but trying to provide as much information as possible):

a. The public\private keys were created using the `ssh-keygen` command in the c:\users\<UserName>\.ssh folder. 
b. The public key was copied and added as a read\write access key for the repository in bitbucket.
c. Jenkins runs as a windows service and logs on as a "Local service account".

What could I be doing wrong?

Update: 12/11/2018:

The git version is 2.17.1.windows.2.

I tried the options suggested by @VonC and I get the same error as below. In the configure individual job=> credentials dropdown nothing shows.

enter image description here

enter image description here

Ajit Goel
  • 4,180
  • 7
  • 59
  • 107
  • What is the repo url? (not the exact one, but to give me an idea to what it looks like) – VonC Dec 11 '18 at 15:38
  • @VonC, It is ssh://git@bitbucketdc-ssh.[PartialDomain].net:[PortNumber]/[ProjectName]/[RepoName].git. – Ajit Goel Dec 11 '18 at 15:48
  • It should work, but only if you associate the credential of the first screenshot in your second screenshot (which I don't see, maybe because you have hidden it) – VonC Dec 11 '18 at 15:53
  • The second screenshot doesn't show any credentials in the dropdown credentials. – Ajit Goel Dec 11 '18 at 16:35
  • That is the issue then. Do you have the Jenkins SSH credential plugin? – VonC Dec 11 '18 at 16:48
  • Could you try and associate the same private key (in your first screenshot) with username "git" instead of username "xxx", for testing? – VonC Dec 11 '18 at 16:49
  • I updated the user name to "git" and I have the same issue. I also have the Jenkins ssh credentials plugin 1.10 – Ajit Goel Dec 11 '18 at 17:29
  • As long as you cannot associate a credential to your Git repo URL, that error message will pop up. – VonC Dec 11 '18 at 17:33
  • The public key in the generated known_hosts file is different than the actual public key. Could that be an issue? – Ajit Goel Dec 11 '18 at 19:55
  • No, I believe this is the public key from the remote server, not your own local public key. (https://security.stackexchange.com/questions/20706/what-is-the-difference-between-authorized-keys-and-known-hosts-file-for-ssh) (https://stackoverflow.com/q/33243393/6309) – VonC Dec 11 '18 at 22:34

1 Answers1

1

Jenkins runs as a windows service and logs on as a "Local servvice account"

That means it might not have access the same environment variables as your account when you did your tests.
Make sure to register your ssh key in the SSH Credentials plugin.

And check your Git version: if 2.19.2, the ssh-keygen command has changed the generated key format (from PEM to openssh): see "Jenkins: what is the correct format for private key in Credentials".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250