Jenkins构建流水线SSH连接Git错误:Host key verification failed 报错问题

报错信息:

无法连接仓库: Command "git ls-remote -h -- git@ip:xxx/xxx.git HEAD"
returned status code 128:
stdout:
stderr: No ED25519 host key is known for <ip> and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rightsand the repository exists.

问题解释:

主要是因为主机密钥验证的问题

解决方法:

方法一:

 打开jenkins系统配置中的全局配置

将Git Host Key Verification Configuration中的Host Key Verification Strategy改为No verification

保存即可

这是通过将主机密钥验证策略改成不用证书验证的模式(注意:在考虑安全性的情况下不要使用这种方法)

方法二:

通过在jenkins服务器上配置主机密钥验证

步骤:

通过ssh-keyscan生成gitlab服务器的主机密钥验证,并将这个文件写入jenkins配置文件中的known_hosts验证文件里,已达到验证某个主机密钥验证

jenkins部署在服务器上

sudo -u jenkins ssh-keyscan -H <Gitlab服务器ip> >> /var/lib/jenkins/.ssh/known_hosts

jenkins是通过docker启动:

sudo docker exec -u jenkins <jenkins_container_name> ssh-keyscan -H <Gitlab服务器ip> >> /var/jenkins_home/.ssh/known_hosts