精简版
我正在使用别名来连接我的远程 Git 存储库。它有效,但是当我将这样的存储库添加到 Jenkins 时,它无法访问存储库并显示错误:
Failed to connect to repository : Command "git -c core.askpass=true ls-remote -h git@github.com-foo:myaccount/myrepository.git HEAD" returned status code 128: stdout: stderr: ssh: Could not resolve hostname github.com-foo: Name or service not known fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
详细的问题描述
为了以不同的用户身份提交
/推送
代码,我使用了别名:
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 上使用我的凭据。
一切正常。
现在,我在虚拟机上安装了 Jenkins,并希望配置与存储在 GitHub 上的远程存储库的连接。我做了什么:
Credentials:
Jenkins -> Credentials -> [link] Global credentials (unrestricted) -> [radio button] Enter directly -> [textarea] Key -> added -> [button] Save
.Git 插件:
Jenkins -> 配置 -> [部分] Git 插件
-- 电子邮件和姓名设置:
- 项目 Git 设置:
[project] -> Configuration -> [section] Source Code Management -> [section] Git
-- Repository URL and credentials:
如屏幕截图所示,连接失败。
为什么它不起作用以及如何让它起作用,以便使用别名连接 Jenkins 中的远程 Git 存储库?
最佳答案
你需要给 jenkins
ssh
配置。文件
C:\Users\myusername\.ssh\config
适用于您的用户,但您还需要 jenkins
用户的别名,jenkins
在该用户下运行。找到 jenkins
用户的主目录并在其中添加文件 .ssh\config
。
然后您可能还需要在其主目录中为 jenkins
复制/创建不同的 ssh
key ,因为您无法从 jenkins
访问> 用户。
在此之后,应该设置文件访问权限。可以通过 chown -R jenkins/var/lib/jenkins/.ssh
轻松完成。
在全局 Jenkins 设置中,私钥可以设置为“来自 Jenkins master ~/.ssh”:
关于git - 如何使用别名访问 Jenkins 中的远程 Git 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449951/