[英]Unable to run git clone from a script placed in a pipeline stage
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 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 (工作正常),我根据插件将密钥保存在变量中,在构建过程中回显到我的动态 docker jenkins 代理,但它无法寻找我从未设置过的密码,所以有没有better way to inject the ssh key to a docker on runtime.
在运行时将 ssh 密钥注入 docker 的更好方法。
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 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
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.虽然我找到了解决方案,但“SSH Credentials Plugin”实际上解决了我的问题,通过该插件我可以毫不费力地将 ssh 密钥添加到 docker 从站。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.