Jenkins - 无法从Slave节点克隆Git。SSH密钥

11
我刚刚设置了我的第一个Jenkins从节点。我运行构建时,遇到了SSH密钥问题。 主Jenkins服务器正在以用户“jenkins”身份运行。我已经设置了SSH密钥,以便我可以从主服务器到从节点进行无密码SSH连接。
例如,从主机:
jenkins@master:~$ ssh slave
Last login: Tue Apr 17 10:30:22 2012 from masterjenkins.com
$ whoami
jenkins

这证明从节点也是在“jenkins”用户下运行的 (我已经从jenkins@slave复制了公共ssh密钥到远程git服务器)。我能够在从节点上手动执行 git clone,但当我从主节点开始构建时,会收到这样的消息:

    ERROR: Error cloning remote repo 'origin' : Could not clone git@host:abc
hudson.plugins.git.GitException: Could not clone git@host:abc
Caused by: hudson.plugins.git.GitException: Error performing command: git clone --progress -o origin git@host:abc /var/lib/jenkins/workspace/abc_build
Command "git clone --progress -o origin git@host:abc /var/lib/jenkins/workspace/abc_build" returned status code 128: Initialized empty Git repository in /var/lib/jenkins/workspace/abc_build/.git/
Host key verification failed.
fatal: The remote end hung up unexpectedly
Caused by: hudson.plugins.git.GitException: Command "git clone --progress -o origin git@host:abc /var/lib/jenkins/workspace/abc_build" returned status code 128: Initialized empty Git repository in /var/lib/jenkins/workspace/abc_build/.git/
Host key verification failed.
fatal: The remote end hung up unexpectedly
Trying next repository
ERROR: Could not clone repository
FATAL: Could not clone

所以它仍然暗示我的SSH密钥没有正确设置。 有人能告诉我需要将哪些密钥复制到哪里吗?

非常感谢, ns


1
同时,主机密钥验证失败似乎表明您的Jenkins用户从未ssh到该服务器,并且您尚未接受主机密钥,请尝试使用jenkins用户从CLI进行ssh以确保其正常工作,并接受主机密钥。 - Doon
@Doon 这可能是问题的根源。我最开始以为你是试图从 Jenkins 尝试克隆的同一台机器上进行 ssh。 - Andrew T Finnell
抓住那个混蛋了!感谢你们的所有建议。我复制并粘贴了Jenkins尝试在从机上运行的命令: git clone --progress -o origin git@host:abc /var/lib/jenkins/workspace/abc 结果发现我在/root/.ssh/known_hosts中有一些冒犯性的密钥。 删除这些密钥并再次连接到Git存储库后,它终于成功了! - nonshatter
1个回答

5

根据克隆URL,看起来您混淆了两种不同的身份验证方法。您正在尝试使用用户git而不是jenkins作为主机的SSH登录。通常,当您托管自己的GIT存储库并使用git@servername:reponame克隆时,您会使用类似于gitolite的东西。

您是否设置过与gitolite类似的任何内容?

尝试像这样以jenkins用户的身份进行SSH登录。

ssh git@slave 

那么看看返回的内容。这是一个更符合你正在进行的git@host:abc的SSH。

如果您在服务器上没有设置其他内容,则将克隆URL更改为jenkins@host:pathtorepo

更新

/home/git/.ssh/authorized_keys

应该有这样一个条目:(这全部在一行上)

# gitolite start
command="/home/git/bin/gl-auth-command jenkins",no
-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAt3+od84Gc9NBVrVb3MKjekHcBDwXXONnVYMNVpuRadoz/FPJTkOIxozKVPJDPI670O252giYpF59sOKqAJL0xEVUrhq8cDFuFwQsSAp0ed1kp/GRxx+pwytL58rcVJEHAy2DkD1z5HlNaZyvIxQyfLTnYfuL1Hx6Qe7dal7mXO0= keycomment
# gitolite end

在gitolite中向jenkins添加仓库权限: (您可能需要在托管您的存储库的同一台机器上克隆,作为gitolite用户)
git clone git@host:gitolite-admin 
cd gitolite-admin
cd conf
vi gitolite.conf

现在找到“abc”的条目,如果不存在则添加一个。
repo    abc
  RW+            = jenkins

现在提交并推送更改。
git commit -a -m "Adding user jenkins to repo abc"
git push

现在请再次执行ssh git@host命令,以查看gitolite是否已经提供您新的权限信息。

@nonshatter 这意味着gitolite或其他东西没有正确设置。您确定在服务器上正确设置了authorized_keys吗?我使用gitlab来管理我的gitolite实例。在您可以成功无需密码地ssh git@host之前,您的克隆将无法工作。首先要检查的是服务器上git用户和您的jenkins名称的authorized_keys是否包含了正确的公钥。 - Andrew T Finnell
1
@nonshatter,您正在尝试从Jenkins克隆名为“testing”的存储库吗? - Andrew T Finnell
嘿,不是这样的,它叫做“abc”。Gitolite在打印权限后立即关闭连接是正常的吗?抱歉,这对我来说都是新技术。 - nonshatter
2
@nonshatter,“有一件事情让我感到困扰,就是我无法轻松地找到一种简单的方法来查看Jenkins在从机上运行的用户。” - 一种方法是创建一个附加到从机的作业,在shell构建步骤中运行whoami - malenkiy_scot
如果你遇到了主机密钥问题,你需要运行ssh命令并接受主机密钥或将其添加到你的known_hosts文件中。 - Andrew T Finnell
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接