来自私有 git 存储库的 Golang 和依赖项

问题描述

假设我有一个 Go 项目,它依赖于来自 github 的 2 个不同的私有存储库。我正在使用部署密钥,用于 github 身份验证。我正在尝试设置 CI 来构建、测试和部署整个过程。

CI 创建的环境,其中包含所有 2 个部署 ssh 密钥(让我们称它们为 ~/.ssh/key1~/.ssh/key2

当我运行 go build 时,它会尝试一次下载所有依赖项。如何避免 repo 不存在的异常(由于身份验证问题)?您将如何解决该问题?

解决方法

如果您使用两个不同的键,则需要修改这些依赖项的 URL 以引用这些单独的键。

这是通过 ~/.ssh/config 完成的(假设 Jenkins 代理由正确的用户运行,并且确实有权访问 ~/.ssh/config

在那个 ~/.ssh/config 中,您声明了两个“主机”条目,每个条目都引用了自己的私钥:

Host repo1
  User git
  Hostname git.server.com
  IdentityFile ~/.ssh/key1

Host repo2
  User git
  Hostname git.server.com
  IdentityFile ~/.ssh/key2

然后确保 repo1 的依赖项不再是 [email protected]:aUser/aRepo 而是:

repo1:aUser/aRepo