git - 如何使用别名访问 Jenkins 中的远程 Git 存储库?

标签 git github jenkins ssh domain-aliasing

精简版

我正在使用别名来连接我的远程 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 上的远程存储库的连接。我做了什么:

  1. Credentials: Jenkins -> Credentials -> [link] Global credentials (unrestricted) -> [radio button] Enter directly -> [textarea] Key -> added -> [button] Save.

  2. Git 插件:Jenkins -> 配置 -> [部分] Git 插件 -- 电子邮件和姓名设置:

enter image description here

  1. 项目 Git 设置:[project] -> Configuration -> [section] Source Code Management -> [section] Git -- Repository URL and credentials:

enter image description here

如屏幕截图所示,连接失败。

为什么它不起作用以及如何让它起作用,以便使用别名连接 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”:

enter image description here

关于git - 如何使用别名访问 Jenkins 中的远程 Git 存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449951/

相关文章:

ubuntu - 无法在 ubuntu 上找到 Jenkins config.xml

java - JENKINS REST API 抛出 403 禁止

jenkins - 读取 secret 文本文件的输出

windows - 无法从 github 克隆 git 存储库

git - 如何将已提交但未推送的更改移动到另一个分支?

git - 无法一开始就使用 git@github.com 克隆存储库?

android - 在 Android Studio 项目中包含现有代码的正确方法?

java - 无法共享我的 android studio 项目以 git 加载永远不会结束?

windows - 无法在 Windows Server 2008 中加载 Rcpp 包

github - 删除 GitHub 存储库的 Wiki