-1

I'm using jenkins docker image to setup my jenkins, there is a scenario where i need to pull from git in a script, when i build the pipeline the docker jenkins agent is spawned and the build fails as "Host key verification failed. fatal: Could not read from remote repository.", there is no ssh key as i'm using git@github.com:Repo, so i used credential binding plugin to pass the git global credentials that i have generated and authenticated with my git account(works fine), i saved the key in a variable as per the plugin, echo to my the dynamic docker jenkins agent during build, but it fails seeking a passphrase which ive never set, so is there a better way to inject the ssh key to a docker on runtime.

 withCredentials([sshUserPrivateKey(credentialsId: 'gitreadonly', keyFileVariable: 'rsa_key', passphraseVariable: '', usernameVariable: '')]) 
            {
    sh '''
                 echo "$rsa_key" >> ~/.ssh/id_rsa
                 cat ~/.ssh/id_rsa
                 chmod 600 ~/.ssh/id_rsa
                 eval $(ssh-agent -s)
                 ssh-add ~/.ssh/id_rsa
                 ssh-keyscan github.com >> ~/.ssh/known_hosts
                 $WORKSPACE/pull.sh 
            '''
    }

o/p

[

Pipeline] withCredentials
Masking supported pattern matches of $rsa_key or $
[Pipeline] {
[Pipeline] sh
+ echo ****
+ cat /root/.ssh/id_rsa
****
+ chmod 700 /root/.ssh/id_rsa
++ ssh-agent -s
+ eval 'SSH_AUTH_SOCK=/tmp/ssh-fUH6OSWT0N1g/agent.182;' export 'SSH_AUTH_SOCK;' 'SSH_AGENT_PID=183;' export 'SSH_AGENT_PID;' echo Agent pid '183;'
++ SSH_AUTH_SOCK=/tmp/agent.182
++ export SSH_AUTH_SOCK
++ SSH_AGENT_PID=183
++ export SSH_AGENT_PID
++ echo Agent pid 183
Agent pid 183
+ ssh-add /root/.ssh/id_rsa
Enter passphrase for /root/.ssh/id_rsa: 
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Sanjay M. P.
  • 919
  • 1
  • 16
  • 33
  • 1
    you need to create a key without passphrase I think – LinPy Nov 22 '19 at 06:53
  • Does this answer your question? [Git error: "Host Key Verification Failed" when connecting to remote repository](https://stackoverflow.com/questions/13363553/git-error-host-key-verification-failed-when-connecting-to-remote-repository) – Joao Vitorino Nov 22 '19 at 15:05
  • @LinPy :I had no passphrase, but it was still asking for it, when i was using the credentials binding plugin. – Sanjay M. P. Nov 23 '19 at 16:44
  • @JoaoVitorino No this environment was different it was a dynamic docker slave that was being created once i start a build, so i have no control over the docker slave as it dies once the build fails. – Sanjay M. P. Nov 23 '19 at 16:45
  • I found the solution though, there is "SSH Credentials Plugin" which actual solved my issue, through the plugin i can add the ssh key to the docker slave without any hassle. – Sanjay M. P. Nov 23 '19 at 16:47

1 Answers1

-1

I found the solution though, there is "SSH Credentials Plugin" which actual solved my issue, through the plugin i can add the ssh key to the docker slave without any hassle.

Sanjay M. P.
  • 919
  • 1
  • 16
  • 33