假设我有一个 Go 项目,它依赖于来自 github 的 2 个不同的私有(private)存储库。我正在使用部署 key 进行 github 身份验证。我正在尝试设置 CI 来构建、测试和部署整个事情。
CI 创建了所有 2 个部署 ssh key 的环境(我们称它们为 ~/.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 的依赖项不再是 git@git.server.com:aUser/aRepo 而是:repo1:aUser/aRepo
关于git - 来自私有(private) git 存储库的 Golang 和依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66008776/