Jenkins,主机密钥验证失败,脚本返回退出代码 255

agu*_*ina 3 ssh jenkins jenkins-pipeline

我有一个building-serverJenkins 2.73.3 和另一个我部署应用程序的服务器。

我还设置了一个凭据以连接building-server到其他服务器。

但是每次我添加另一台服务器时都很难添加它,因为我在新服务器和命令行中设置了授权密钥,但在 Jenkins 中却没有。

这是一个失败的小食谱:

pipeline {
  agent any

  stages {

    stage('Set conditions') {
      steps {
        sshagent(['xxxx-xxxx-xxxx-xxxx-xxxx']) {
          sh "ssh user@product.company.com 'echo $HOME'"
        }
      }
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

这是日志失败:

[ssh-agent] Started.
[Pipeline] {
[Pipeline] sh
[check] Running shell script
+ ssh user@product.company.com echo /var/lib/jenkins
$ ssh-agent -k
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 12567 killed;
[ssh-agent] Stopped.
Host key verification failed.
[Pipeline] }
[Pipeline] // sshagent
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 255
Finished: FAILURE
Run Code Online (Sandbox Code Playgroud)

agu*_*ina 5

似乎解决方案是将参数 StrictHostKeyChecking 添加到 shell 脚本行

sh "ssh -o StrictHostKeyChecking=no user@product.company.com 'echo $HOME'"
Run Code Online (Sandbox Code Playgroud)

  • 虽然这会起作用,但它绕过了目标主机是您想要连接的人并且是潜在的漏洞的验证。您应该尝试通过 ssh 从每个构建代理到目标主机(如果不使用构建代理,则从 Jenkins master)将目标添加到构建代理的 `~/.ssh/known_hosts` 文件中。看到[这个答案](/sf/answers/1063728011/)到[这个问题](/sf/ask/1062193611/) (3认同)