Search code examples
bashgitjenkinsjenkins-declarative-pipeline

Jenkins pipeline Git error: “Host Key Verification Failed” when connecting to remote repository


I am using my PC as a server to build my Jenkins script with declarative pipeline.

when running a git clone on the git bash, it works well:

$ git clone ssh://[email protected]:29418/myProjects/thisProject.git ThisFirmware
Cloning into 'ThisFirmware'...
remote: Counting objects: 889, done
remote: Finding sources: 100% (203/203)
Receiving objects:  79% (121769/152721), 48.11 MiB | 10.35 MiB/s

but running it on the same machine through pipelining :

sh "git clone ssh://[email protected]:29418/myProjects/thisProject.git ThisFirmware"

gives me the following error

[Pipeline] sh
+ git clone ssh://[email protected]:29418/myProjects/thisProject.git ThisFirmware
Cloning into 'ThisFirmware'...
Host key verification failed.
fatal: Could not read from remote repository.

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

any hints?

To be sure I was using the same key, thanks to Jens, the pipeline script couldn't find the key.

  • Doing cat ~/.ssh/id_rsa.pub on the pipeline, resulted in + cat /c/windows/system32/config/systemprofile/.ssh/id_rsa.pub cat: /c/windows/system32/config/systemprofile/.ssh/id_rsa.pub: No such file or directory

  • So I copied the ssh file from my guillaumed user ssh path to the SYSTEM ssh path but I can't use it if i'm logged in with SYSTEM user instead of guillaumed user.

what do I have to do?

this link didn't help.


Solution

  • Indeed, you can't checkout with ssh if you are logged as SYSTEM.

    To checkout as "thisUser":

    You'll need to configure your Jenkins.

    1/ add a user "thisUser"

    2/ add user's ssh credentials "thisUser-ssh-credentials"

    3/ use ssh-agent plugin https://wiki.jenkins.io/display/JENKINS/SSH+Agent+Plugin

    Then configure your pipeline

    dir(server_workspace) 
    {
        sshagent(credentials: ['thisUser-ssh-credentials'] ) 
        {
            sh "git clone ssh://[email protected]:port/yourProjects/thisProject.git"
        }
    }