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