Friday 13 October 2017

Jenkins to Git - SSH says "No"

As per my earlier post: -


I'm on a voyage of discovery with Jenkins and Git.

Whilst trying to plumb onto into t'other, I was hitting a blocker.

To recap, I have Jenkins installed on my MacBook, running locally, and I have Git running on a Docker container on a remote Mac.

Therefore, I'm connecting to the remote Git repository using SSH rather than, say, HTTPS or a local file-system.

This works OK for me using Git commands such as: -

and: -

git push

So, in the world of Jenkins, I thought it'd be equally simple.

To start with, I created a new job / project: -


chose Git as my SCM: -


added in the SSH URL: -


and immediately saw this: -

Failed to connect to repository : Command "git ls-remote -h ssh://git@192.168.1.214:2222/git-server/repos/myrepo.git HEAD" returned status code 128:
stdout: 
stderr: Permission denied (publickey,keyboard-interactive). 
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

before I'd had a chance to enter some credentials :-(

I clicked the button to add new creds: -


which led me here: -


I tried pasting the private key into the Key field, having used the command: -

ssh-keygen -y -f ~/.ssh/id_rsa

to retrieve the hash that represents that private key.

I'd previously validated that the retrieved hash matches the public key: -

~/.ssh/id_rsa.pub

However, when i switched back to the project configuration screen, I saw this: -

Failed to connect to repository : Command "git ls-remote -h ssh://git@192.168.1.214:2222/git-server/repos/myrepo.git HEAD" returned status code 128:
stdout: 
stderr: Load key "/Users/Shared/Jenkins/tmp/ssh6857222762876740778.key": invalid format 
Permission denied (publickey,keyboard-interactive). 
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

After much faffing about, I switched the Credentials to this: -


i.e. specifically pulling the key from ~/.ssh on the main Mac, upon which Jenkins is running ( hence the Jenkins master ).

But I was still seeing this: -

Failed to connect to repository : Command "git ls-remote -h ssh://git@192.168.1.214:2222/git-server/repos/myrepo.git HEAD" returned status code 128:
stdout: 
stderr: Load key "/Users/Shared/Jenkins/tmp/ssh6850003580465807718.key": invalid format 
Permission denied (publickey,keyboard-interactive). 
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Then I realised that it was looking for the home directory of A DIFFERENT USER :-)

There's even a clue in the error above: -

stderr: Load key "/Users/Shared/Jenkins/tmp/ssh6850003580465807718.key": invalid format 

So I switched to root: -

su -

Changed to the appropriate ~/.ssh directory for the Jenkins user: -

cd /Users/Shared/Jenkins/.ssh

and copied the private key: -

cp /Users/davidhay/.ssh/id_rsa .

and tried again.

Alas: -

Failed to connect to repository : Command "git ls-remote -h ssh://git@192.168.1.214:2222/git-server/repos/myrepo.git HEAD" returned status code 128:
stdout: 
stderr: Load key "/Users/Shared/Jenkins/tmp/ssh2229777690807748085.key": invalid format 
Permission denied (publickey,keyboard-interactive). 
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I validated that the key was in the right place: -

ls -al /Users/Shared/Jenkins/.ssh

total 16
drwx------  4 jenkins  jenkins   128 13 Oct 11:14 .
drwxr-xr-x  7 jenkins  jenkins   224 12 Oct 10:34 ..
-rw-------  1 root     jenkins  1766 13 Oct 11:14 id_rsa
-rw-r--r--  1 jenkins  jenkins   363 13 Oct 10:15 known_hosts


and then noticed the obvious mistake.

Can you see where I went wrong ?

-rw-------  1 root     jenkins  1766 13 Oct 11:14 id_rsa

I changed the permissions: -

chown jenkins:jenkins /Users/Shared/Jenkins/.ssh/id_rsa

validated the change: -

ls -al /Users/Shared/Jenkins/.ssh

total 16
drwx------  4 jenkins  jenkins   128 13 Oct 11:14 .
drwxr-xr-x  7 jenkins  jenkins   224 12 Oct 10:34 ..
-rw-------  1 jenkins  jenkins  1766 13 Oct 11:14 id_rsa
-rw-r--r--  1 jenkins  jenkins   363 13 Oct 10:15 known_hosts

and retried Jenkins: -


To finish, I added a new Build step: -


which compiles and executes the Java sourced from Git, and then ran the Build: -


Whilst I was on the Jenkins master, I also checked the workspace: -

ls -al /Users/Shared/Jenkins/Home/workspace/DaveHay

total 24
drwxr-xr-x   6 jenkins  jenkins  192 13 Oct 10:21 .
drwxr-xr-x   6 jenkins  jenkins  192 12 Oct 13:59 ..
drwxr-xr-x  13 jenkins  jenkins  416 13 Oct 11:26 .git
-rw-r--r--   1 jenkins  jenkins  462 13 Oct 11:26 HelloWorld.class
-rw-r--r--   1 jenkins  jenkins  148 13 Oct 10:21 HelloWorld.java
-rw-r--r--   1 jenkins  jenkins   25 13 Oct 10:18 Readme


which showed the newly compiled Java class.

Job done :-)

No comments:

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...