简体   繁体   English

开始使用 ssh 而不是 https(不在 github 上)

[英]go get using ssh instead of https (NOT on github)

We have a private code repository accessible only through ssh/git (no https), and we would like to host our go code/modules there.我们有一个只能通过 ssh/git(无 https)访问的私有代码存储库,我们希望在那里托管我们的 go 代码/模块。

First I tried:首先我试过:

git config --global url."git@code.internal.local:".insteadOf "https://code.internal.local/"

so, both of the following work just fine:所以,以下两个工作都很好:

  • git clone git@code.internal.local:reponame.git
  • git clone https://code.internal.local/reponame

But go get code.internal.local/reponame fails, as go still insists on trying https://... not git.但是go get code.internal.local/reponame失败了,因为 go 仍然坚持尝试 https://... 而不是 git。

package code.internal.local/reponame: unrecognized import path "code.internal.local/reponame": https fetch: Get "https://code.internal.local/reponame?go-get=1": dial tcp 192.168.0.5:443: i/o timeout

The problem问题

The behaviour you're observing is detailed in the section "Remote import paths" of the go get documentation.您观察到的行为在go get文档的“远程导入路径”部分中有详细说明。 In particular, just by looking at the remote import path code.internal.local/reponame , go get has no way to know which VCS and at which URL in particular is used to actually host that package.特别是,仅通过查看远程导入路径code.internal.local/reponamego get无法知道具体使用哪个 VCS 和哪个 URL 来实际托管该包。

To solve that problem, go get employs a set of HTTPS (and HTTP, as a fallback, which has to be explicitly enabled) GET requests to a set of special URLs constructed out of the specified import path.为了解决这个问题, go get使用了一组 HTTPS(和 HTTP,作为回退,必须明确启用)GET 请求到一组由指定的导入路径构造的特殊 URL。 It is assumed that whatever serves such calls is able to respond with a reply which identifies the VCS to use and the URL of the repository.假设服务于此类调用的任何内容都能够以识别要使用的 VCS 和存储库 URL 的回复进行响应。

Possible solutions可能的解决方案

If you're using modules, you might set up a dedicated "module proxy" wherever is convenient for your team (also, there may be deployed many proxies) and make the team members use the GOPROXY environment variable which points at the convenient instance.如果您正在使用模块,您可以在您的团队方便的任何地方设置一个专用的“模块代理”(也可能部署了许多代理),并使团队成员使用指向方便实例的GOPROXY环境变量。 See go help modules for more info.有关更多信息,请参阅go help modules

Otherwise, if you can put a webserver to answer remote import path resolution requests, you can do that;否则,如果您可以放置​​一个网络服务器来回答远程导入路径解析请求,您就可以这样做; nginx and apache can be used for that just with their stock modules. nginx 和 apache 可以用于它们的库存模块。

Otherwise you might need to resort to manual operation.否则,您可能需要求助于手动操作。

I checked my .gitconfig and found this (for github private repo) -我检查了我的.gitconfig并找到了这个(对于github私有仓库)-

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

above configuration is working for me.以上配置对我有用。 Also you can try creating a .netrc file in your project root, but that should not be pushed to remote code repo.您也可以尝试在项目根目录中创建.netrc文件,但不应其推送到远程代码存储库。

I posted a detail answer here https://stackoverflow.com/a/65925691/348719我在这里发布了详细答案https://stackoverflow.com/a/65925691/348719

Basically, you should add .git suffix to require at client and module at server.基本上,你应该添加.git后缀为require在客户机和module的服务器。 Without .git suffix, go get will use https.如果没有.git后缀, go get将使用 https。

  • At client (go.mod) (change the version number to correct one):在客户端(go.mod)(将版本号更改为更正):
require code.internal.local/reponame.git v0.1.0

And it's better add go env -w GOPRIVATE=code.internal.local最好添加go env -w GOPRIVATE=code.internal.local

  • At server (go.mod)在服务器(go.mod)
module code.internal.local/reponame.git

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM