[英]How to access a remote Git repository in Jenkins using aliases?
精简版
我正在使用别名连接我的远程 Git 存储库。 它可以工作,但是当我将此类存储库添加到 Jenkins 时,它无法访问存储库并显示错误:
无法连接到存储库:命令“git -c core.askpass=true ls-remote -h git@github.com-foo:myaccount/myrepository.git HEAD”返回状态代码 128:stdout:stderr:ssh:无法解析主机名 github.com-foo:名称或服务未知 致命:无法从远程存储库读取。
请确保您拥有正确的访问权限并且存储库存在。
详细问题描述
为了以不同的用户身份commit
/ push
代码,我使用别名:
C:\\Users\\myusername\\.ssh\\config
...
#github.com-foo account
Host github.com-foo
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_foo
#github.com-bar account
Host github.com-bar
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_bar
...
然后代替
git clone git@github.com:myaccount/myrepository.git
我只是用
git clone git@github.com-foo:myaccount/myrepository.git
.ssh
目录可以作为共享文件夹从我的本地 VM 访问,因此也可以在 VM 上使用我的凭据。
一切正常。
现在我在 VM 上安装了 Jenkins,并且想要配置到存储在 GitHub 上的远程存储库的连接。 我做了什么:
凭证: Jenkins -> Credentials -> [link] Global credentials (unrestricted) -> [radio button] Enter directly -> [textarea] Key -> added -> [button] Save
。
Git 插件: Jenkins -> Configuration -> [section] Git plugin
-- 电子邮件和姓名设置:
[project] -> Configuration -> [section] Source Code Management -> [section] Git
-- 存储库 URL 和凭据: 如屏幕截图所示,连接失败。
为什么它不起作用以及如何使其工作,以便使用别名连接 Jenkins 中的远程 Git 存储库?
您需要为jenkins
ssh
配置。 文件
C:\Users\myusername\.ssh\config
适用于您的用户,但您也需要jenkins
用户的别名,在该用户下运行jenkins
。 找到jenkins
用户的主目录并在那里添加文件.ssh\\config
。
然后您可能还需要在其主目录中为jenkins
复制/创建不同的ssh
密钥,因为jenkins
用户无法访问您。
在此之后,应该设置文件评估权限。 它可以通过chown -R jenkins /var/lib/jenkins/.ssh
轻松完成。
在全局 Jenkins 设置中,私钥可以设置为“来自 Jenkins master ~/.ssh”:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.