使用私有存储库构建

go build with private repositories

我正在尝试构建一个使用 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 中的顶部而不是尝试一些东西。

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

然后您可以通过告诉 Go 您使用的是私人仓库来获取您的包裹: GOPRIVATE="github.com/your_username_or_org" go get github.com/name_or_org/repo_name

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