Git SSH checkout fails with Jenkins and GitLab

Viewed 750

When I attempt a build on my Jenkins job that is configured to checkout from GitLab I am getting below error output (truncated) :

    ....................
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from git@10.777.77.777:root/xxx.git
    at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:1001)
    at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1242)
    at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1302)
    ....................................................
        ....................................................
    at hudson.model.Executor.run(Executor.java:431)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- git@10.777.77.777:root/xxx.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout: 
stderr: Load key "/var/lib/jenkins/workspace/gitlabjenkinsdemo@tmp/jenkins-gitclient-ssh1964800292912998995.key": invalid format
Permission denied, please try again.
Permission denied, please try again.
git@10.170.8.204: Permission denied (publickey,password).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2681)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2102)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.access$500(CliGitAPIImpl.java:86)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:624)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:999)
... 11 more

ERROR: Error fetching remote repo 'origin' Finished: FAILURE

I am trying to setup a CI/CD pipeline as per this post Jenkins_Gitlab

I expected that the private key I added should enable checkout out of the box (Note : Both Jenkins server and Gitlab server are running on the same Debian instance).

In the ~/.ssh directory I have the following :

root@myservername:~/.ssh# ls -ld
drwx------ 2 root root 4096 Oct 18 15:30 .

In the source code management tab of my Jenkins job I have also added the SSH link that I extracted from my project in Gitlab. However what I am unsure of is the message below that the project is showing in Gitlab :

GITLABERROR

What am I missing ?

1 Answers

Both Jenkins server and Gitlab server are running on the same Debian instance

Ideally, they would run with their own associated service account, not "root".

But still, even as root, this should work.
Check which private SSH key you have added, and make sure, at least for testing, it wasn't one protected with a passphrase (meaning the private key file does not have a Proc-Type: 4,ENCRYPTED line in it)

That might be a cause for the jenkins-gitclient-ssh1964800292912998995.key": invalid format you have.
(That or you have copied the private key file content with CRLF instead of LF as end-of-line)
(or you have copied a public key, where a private one was expected, or vice-versa)

From the discussion:

  • the private key had to be registered again in Jenkins (making sure its eol -- end-of-line -- are LF, not CRLF)
  • the branch needs to be renamed:

My repo had master when I did git init, so had to rename branch to main when I set the GitLab remote

Related