设置jenkins git返回“主机密钥validation失败”错误

我想把我的一个jenkins项目放到使用git的源代码pipe理中。

但是,当我在项目configuration页面的源代码pipe理选项卡下键入git@gitlab.com:myGitlabAccount/myProjectName.git 我看到以下错误信息:

 Failed to connect to repository : Command "git.exe ls-remote -h git@gitlab.com:myGitlabAccount/myProjectName.git" returned status code 128: stdout: stderr: Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 

当我在Windows命令行inputgit.exe ls-remote -h git@gitlab.com:myGitlabAccount/myProjectName.git时,没有错误信息。

我可以成功地git clone git@gitlab.com:myGitlabAccount/myProjectName.git

我认为, jenkins主机密钥validation失败是我的问题,如果我是一个Linuxjenkins用户的解决scheme。

但是我在windows环境下使用jenkins,并且在窗口中找不到任何叫jenkins的用户。

我怎样才能切换到jenkins用户,并在Windows环境中手动键入git ls-remote -h git@bitbucket.org:person/projectmarket.git HEAD

您需要在您的jenkins机器上生成ssh密钥并将其添加到您的gitlab帐户

如果你使用的是windows打开git bash并输入以下内容:

 # generate keys ssh-keygen -t rsa 

点击每个步骤的ENTER ,如果你已经有一个密钥,你会得到一个证书已经存在的警告。

现在复制公钥并将其粘贴到你的gitlab帐户下

 # grab the key cat ~/.ssh/id_rsa.pub 

登录你的gitlab帐户,并添加此密钥(登录后,您将在顶部菜单上看到ssh-keys)

在这里输入图像说明

在这里输入图像说明

因为我把jenkins作为windows服务安装,所以我必须去jenkins服务页面,把它的登录账号改成我用来登录windows的账号。

通过这种方式,jenkins可以访问我在Windows环境中使用的known_hosts文件。

如果您将Jenkins默认安装为Windows服务,则可以复制目录下名为“known_hosts”的文件

C:\ Windows \ System32 \ Config \ SystemProfile \ .ssh

这将由Jenkins读取,并允许它访问你的Git服务器。