如何设置 GoCD 来处理使用 SSH 密钥进行身份验证的私有 git 存储库?

How to set up GoCD to handle private git repositories that use SSH keys for authentication?

我有一个 git 存储库,它使用 SSH 密钥对用户进行身份验证,我想将该存储库用作 GoCD material。 GoCD 给我这个错误:

Error performing command: --- Command ---
git ls-remote ssh://git@server/repo.git refs/heads/master
--- Environment ---
{GIT_ALLOW_PROTOCOL=http:https:ssh:git:file:rsync}
--- INPUT ----

--OUTPUT ---

--- ERROR ---
STDERR: Host key verification failed.
STDERR: fatal: Could not read from remote repository.
STDERR: 
STDERR: Please make sure you have the correct access rights
STDERR: and the repository exists.
---

有什么方法可以将 SSH 密钥添加到 GoCD?

在撰写此答案时,无法直接在 GoCD 中管理 SSH 密钥。要使其工作,您必须为 GoCD 服务器和所有代理生成 SSH 密钥,然后将它们添加到托管 git 存储库的服务器。您还可以将现有密钥复制到服务器和节点,但显然不推荐这样做。

例如,使用标准的 GoCD 服务器安装,您的系统中应该有 "go" 用户:

$ grep GoCD /etc/passwd
go:x:998:998:GoCD User:/var/go:/bin/bash

sudo 作为 "go" 用户并创建密钥

$ sudo su - go
$ ssh-keygen
...
$ ssh [server]
The authenticity of host '[server] ([1.3.3.7])' can't be established.
ECDSA key fingerprint is SHA256:Rxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[server]' (ECDSA) to the list of known hosts.
Permission denied (publickey,keyboard-interactive).

最后一步很重要,因为如果您不成功,GoCD 将给您同样的错误。

现在将您的密钥添加到 git 服务器并在 GoCD 中单击 "Check connection"。它应该打印 "Connection OK.".

为运行代理的每个节点和用户生成密钥。

如果您使用 windows 机器来托管 GoCD 服务器和代理, 它不 运行 在普通用户帐户下,它 运行 在“本地系统帐户”

因此,即使您可以从 git bash(以当前用户身份登录)访问您的 git 存储库,GOCD 也无法访问。

因此您需要为当前用户的本地系统帐户添加 SSH 密钥。

1.First 找到本地系统帐户的主目录(它不会位于 C:/Users 下)

2.Use 任何用于查找主目录的远程管理工具 - 如果您使用 http://download.sysinternals.com/files/PSTools.zip

a) 以管理员身份解压缩和 运行 命令行

b)PsExec.exe -i -s cmd.exe -启动工具 c)运行 echo %userprofile% 获取主目录 (eg:C:\Windows\system32\config\systemprofile)

3.Now 您可以从当前用户复制 SSH 密钥文件或使用 ssh 命令创建一个新文件。

尝试在 creating/copying SSH 密钥后检查连接,它会显示连接正常!

使用 Windows 10、本地 GoCD 服务器和本地 'private' Bitbucket 这对我有用。 (这是我的开发堆栈的一部分):

第一步:使用 'normal' 本地用户帐户验证操作。

  • 按照正常的 ssh 密钥生成为 Bitbucket 创建 ssh 密钥,例如,在这个例子中使用 ssh-keygen 我们将其称为 yourprivatekeyfile

  • yourprivatekeyfile.pub 的内容复制到一个新的 Bitbucket 访问密钥中,该密钥位于存储库的配置部分。 (即参考https://confluence.atlassian.com/bitbucket/set-up-an-ssh-key-728138079.html

  • 编辑(必要时创建)本地用户的 ~/.ssh/config 文件以包含以下行

Host 127.0.0.1
    Preferredauthentications publickey
    IdentityFile ~/.ssh/yourprivatekeyfile
  • 确保您可以 git clone ssh://git@127.0.0.1:7999/yourproject/yourrepo.git 您的私人 git 存储库,这将验证密钥是否适用于 Bitbucket 服务器。

第二步:让GoCD服务器使用你的密钥文件。本节假设您已经完成了 GoCD 服务器的 'normal' 安装,并且它作为本地系统帐户运行。

  1. 导航到您的本地系统帐户配置文件目录,可能是 c:/Windows/System32/config/systemprofile

  2. 如果不存在,请创建一个 .ssh 目录。

  3. Copy/move 您的私钥文件(如果您愿意,还可以 public 密钥文件)放入此 .ssh 目录。

  4. 如果不存在,请在此 .ssh 目录中创建一个 config 文件。

  5. 将上面为本地用户的 config 文件创建的行复制到此 config 文件中。 (服务器条目的行应该与本地用户的 config 文件中的行相同)

  6. 您的 GoCD Material(Git 存储库)应该具有 Bitbucket 克隆字段中为 SSH 操作提供的 ssh URL

  7. 在 'Test Connection' 中 Git 回购 material,它应该响应 'Connection OK'