GOPRIVATE和私有git仓库用于go mod

huangapple go评论105阅读模式
英文:

GOPRIVATE and private git repository for go mod

问题

尝试在我的旧组织上使用go mod tidygo mod download命令时,我只需要设置GOPRIVATE,将SSH公钥添加到GitHub/Bitbucket/GitLab上,设置.ssh/config文件中的IdentityFile用于连接自定义域名,然后设置.gitconfig文件如下所示:

go env -w GOPRIVATE="github.com/xxx/*"

# .gitconfig
insteadOf = https://github.com/xxx/

但是对于另一个使用Bitbucket的组织,这种方法不起作用。尝试使用身份验证密钥或SSH时,在执行go mod tidy/download命令时仍然遇到问题。

go env -w GOPRIVATE="bitbucket.xxx/*,bitbucket.xxx:9022/*" 
# 9022是用于克隆仓库的SSH端口

# .gitconfig
#
# ACCTOKEN是我的访问令牌
insteadOf = https://bitbucket.xxx/

这两种方法都不起作用,没有.gitconfig文件会显示错误:

go: bitbucket.xxx/yyy/zzz/aaa/bbb@v0.0.0-20220728131025-25f2fba58852: invalid version: git ls-remote -q origin in /home/asd/go/pkg/mod/cache/vcs/be703481c10c8d59451fefcffbf5e7341aab5edf1c25acbaa8356c8fbeef42d2: exit status 128:
        fatal: could not read Username for 'https://bitbucket.xxx': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

如果在.gitconfig中使用访问令牌,错误信息如下:

go: bitbucket.xxx/yyy/zzz/aaa/bbb@v0.0.0-20220728131025-25f2fba58852: invalid version: git ls-remote -q origin in /home/asd/go/pkg/mod/cache/vcs/be703481c10c8d59451fefcffbf5e7341aab5edf1c25acbaa8356c8fbeef42d2: exit status 128:
        fatal: Authentication failed for 'https://bitbucket.xxx/scm/yyy/zzz.git/'

无法正确使用git clone https://bitbucket.xxx/scm/yyy/zzz.git,无论是密码还是访问令牌都被拒绝。

如果在.gitconfig中使用SSH,错误信息如下:

go: bitbucket.xxx/yyy/zzz/aaa/bbb@v0.0.0-20220728131025-25f2fba58852: invalid version: git ls-remote -q origin in /home/asd/go/pkg/mod/cache/vcs/be703481c10c8d59451fefcffbf5e7341aab5edf1c25acbaa8356c8fbeef42d2: exit status 128:
        Repository not found
        The requested repository does not exist, or you do not have permission to access it.
        fatal: Could not read from remote repository.
        
        Please make sure you have the correct access rights
        and the repository exists.

我可以使用这个.gitconfig文件正常地执行git clone ssh://git@bitbucket.xxx:9022/yyy/zzz.git,但是如果使用go mod(或者在使用IntelliJ时),会失败。

xxx是组织的域名,该组织要求在SSH密钥上使用密码。

我有什么遗漏吗?或者如何强制go mod使用SSH而不是HTTPS进行克隆/下载?

英文:

Tried to go mod tidy or go mod download
on my old organization, all I need to do is just to set GOPRIVATE, add ssh public key to the github/bitbucket/gitlab, set .ssh/config to which IdentityFile to be used to connect for custom domain, then set .gitconfig something like this:

go env -w GOPRIVATE="github.com/xxx/*"

# .gitconfig
insteadOf = https://github.com/xxx/

but this other organization won't work (using bitbucket), tried using auth key or ssh still got problem when doing go mod tidy/download

go env -w GOPRIVATE="bitbucket.xxx/*,bitbucket.xxx:9022/*" 
# 9022 is their ssh port used for cloning a repo

# .gitconfig
#
# ACCTOKEN is my access token
insteadOf = https://bitbucket.xxx/

both didn't work, without the .gitconfig it shows error:

go: bitbucket.xxx/yyy/zzz/aaa/bbb@v0.0.0-20220728131025-25f2fba58852: invalid version: git ls-remote -q origin in /home/asd/go/pkg/mod/cache/vcs/be703481c10c8d59451fefcffbf5e7341aab5edf1c25acbaa8356c8fbeef42d2: exit status 128:
        fatal: could not read Username for 'https://bitbucket.xxx': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.

if using access token on .gitconfig, the error is:

go: bitbucket.xxx/yyy/zzz/aaa/bbb@v0.0.0-20220728131025-25f2fba58852: invalid version: git ls-remote -q origin in /home/asd/go/pkg/mod/cache/vcs/be703481c10c8d59451fefcffbf5e7341aab5edf1c25acbaa8356c8fbeef42d2: exit status 128:
        fatal: Authentication failed for 'https://bitbucket.xxx/scm/yyy/zzz.git/'

^ cannot git clone https://bitbucket.xxx/scm/yyy/zzz.git properly using this, password or access token always rejected

if using ssh on .gitconfig, the error is:

go: bitbucket.xxx/yyy/zzz/aaa/bbb@v0.0.0-20220728131025-25f2fba58852: invalid version: git ls-remote -q origin in /home/asd/go/pkg/mod/cache/vcs/be703481c10c8d59451fefcffbf5e7341aab5edf1c25acbaa8356c8fbeef42d2: exit status 128:
        Repository not found
        The requested repository does not exist, or you do not have permission to access it.
        fatal: Could not read from remote repository.
        
        Please make sure you have the correct access rights
        and the repository exists.

I can git clone ssh://git@bitbucket.xxx:9022/yyy/zzz.git normally using this .gitconfig, but failed if using go mod (or when using IntelliJ).

xxx is the organization domain name, the organization enforced to use password on ssh key.

Did I missed something? Or how can I force go mod to clone/download using ssh instead of https?

答案1

得分: 1

没问题,.gitconfig 文件有误,应该修改为以下内容:

insteadOf = https://bitbucket.xxx/scm/

^ 在我的情况下,后缀中有 scm

现在可以正常执行 git clone https://bitbucket.xxx/scm/yyy/zzz.git 命令,并且 go mod 也可以正常工作。

英文:

Nevermind, the .gitconfig is wrong, it should be like this:

insteadOf = https://bitbucket.xxx/scm/

^ in my case, there is scm in the suffix.

now can git clone https://bitbucket.xxx/scm/yyy/zzz.git properly, with go mod also works.

huangapple
  • 本文由 发表于 2022年9月8日 15:46:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73645428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定