登录
首页 >  Golang >  Go问答

为什么 go module ssh 自定义私有仓库(非 github)配置仍然请求 https 获取?

来源:stackoverflow

时间:2024-04-18 16:09:35 394浏览 收藏

学习Golang要努力,但是不要急!今天的这篇文章《为什么 go module ssh 自定义私有仓库(非 github)配置仍然请求 https 获取?》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!

问题内容

我正在使用 go 模块。

为了使用模块版本,我无法使用本地模块。例如:

replace locakpkg => ../localpkg v0.1.0

上述操作将失败,因为替换本地路径到目前为止无法拥有版本(转到 1.15)。

因此,为了使模块版本正常工作,我决定使用私有 ssh 存储库。

我花了两天时间搜索如何使私人 ssh 存储库工作。

通过关注许多在线文章,我做到了

git config --global [email protected]:.insteadof https://private.com/
go env -w goprivate=private.com

我发现 go get 总是会执行 https fetch 来检查 ssl 凭证。所以我也正确配置了一个https服务器。

但最终我仍然收到错误消息:

unrecognized import path "private.com/foo": reading https://private.com/foo?go-get=1: 404 Not Found

我确实谷歌了这个错误,发现了这个规范https://golang.org/ref/mod#vcs-find,它说我必须让服务器回复 用于 https 获取请求。

  • 如果有办法在本地模块包中使用 git 标签版本控制,我可以在 go.mod 中使用本地替换,而不是配置私有 ssh 存储库。

  • 如果上述一点不可能,当我配置私有 ssh 存储库时如何避免 https 获取?我认为 ssh repo 与 https 协议无关。


解决方案


(我在linux上使用go 1.15。发布此答案时最新的稳定版本)

我解决了这个问题并在这里发帖,希望有一天这能对其他人有所帮助。我在网上搜索没有找到正确的答案。

简而言之,答案是在所有地方都使用 .git 后缀。如果没有 .git 后缀,go mod tidygo get 将使用 https 而不是 ssh (git)。

在客户处:

如果您在服务器上使用 /repopath/foo.git 路径,则文件 ~/.gitconfig (在 linux 上):

[url "ssh://[email protected]"]
    insteadof = https://private.com

如果您在服务器上使用 ~/repopath/foo.git 路径,则文件 ~/.gitconfig (在 linux 上):

[url "[email protected]:"]
    insteadof = https://private.com/

执行以下命令在 linux 上更新 ~/.config/go/env

go env -w goprivate=private.com

go.mod中,应该使用

require private.com/repopath/foo.git v0.1.0

file.go中,应该是

import private.com/repopath/foo.git

在 ssh 服务器

在私有服务器的 foo.git/go.mod 中应该有:

module private.com/repopath/foo.git

并确保服务器上的 git 存储库具有标签版本 v0.1.0。不要忘记在客户端使用 git push --tags 将标签版本更新到服务器。如果没有--tags,则不会推送tag版本。

在所有需要的地方添加.git后缀后,go mod tidygo get将不再发送https请求。

本篇关于《为什么 go module ssh 自定义私有仓库(非 github)配置仍然请求 https 获取?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>