【问题标题】:go build with private repositories使用私有存储库进行构建
【发布时间】:2021-04-12 14:20:04
【问题描述】:

我正在尝试构建一个使用私有 git 存储库的 go 应用程序。我的 github 帐户有一个 ssh 密钥,我的 .gitconfig 文件中有以下内容:

[url "https://username:token@github.com"]
    insteadOf = https://github.com
[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

当我执行 go test 或 go build 时,我被要求输入密码。然后我得到回复:

go: found github.com/x/businesses in github.com/x/businesses v0.0.0-yy
go: cmd/main/cmd imports
  github.com/x/businesses: github.com/x/businesses@v0.0.0-yy/go.mod: 
  verifying module: github.com/x/businesses@v0.0.0-yy/go.mod: 
  reading https://sum.golang.org/lookup/github.com/xx/businesses@v0.0.0-yy: 410 Gone
server response:
  not found: github.com/x/businesses@v0.0.0-yy: 
  invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* 
  in tmp/gopath/pkg/mod/cache/vcs/zz: 
    exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled

我尝试删除 .gitconfig 中的顶部 insteadOf 无缘无故地尝试一些东西。

【问题讨论】:

标签: go github build


【解决方案1】:

你的 git 配置应该看起来像 [url "git@github.com:"] insteadOf = https://github.com/ 或与您拥有的类似。

然后你可以通过告诉Go 你正在使用这样的私人仓库来获取你的 pacakge: GOPRIVATE="github.com/your_username_or_org" go get github.com/name_or_org/repo_name

另外,我通常不会在我的 SSH 密钥上设置密码,因为每次都输入密码有点烦人,但当然可以像 cmets 中指出的那样添加密码更安全。

【讨论】:

  • 带有 passphase 的密钥更安全,最好使用它以防有人访问您的密钥。它会加密您的密钥,您有更多时间更改密钥,直到有人找到密码(在最坏的情况下)您可以在 ssh passphase 存在时使用 ssh-agent。
猜你喜欢
  • 2022-11-12
  • 2022-01-14
  • 1970-01-01
  • 2021-03-26
  • 2019-01-09
  • 1970-01-01
  • 2021-06-19
  • 2015-11-15
  • 2017-12-21
相关资源
最近更新 更多