登录
首页 >  Golang >  Go问答

带有私有 gitlab (ssh) 的 Goland 模块

来源:stackoverflow

时间:2024-04-21 16:57:35 440浏览 收藏

偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《带有私有 gitlab (ssh) 的 Goland 模块》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!

问题内容

美好的一天!我尝试从 glide 迁移到 go 模块(私人 gitlab 存储库) 并通过 ssh 签出代码

我有一个简单的项目,从私人 gitlab 存储库导入。

go.mod 看起来像:

module my.private.package/modtest

go 1.12

require my.private.package/statistics v1.0.0

当我尝试构建我的应用程序或运行测试时,我得到:

go: my.private.package/[email protected]: unrecognized import path "my.private.package/statistics" (parse https://my.private.package/statistics?go-get=1: no go-import meta tags ())
go: error loading module requirements

我尝试将设置添加到 git config:

[url "ssh://[email protected]:9999"]
        insteadOf = https://my.private.package

但仍然出现此错误。

有什么办法可以让它发挥作用吗? 谢谢。


解决方案


我之前处理过 go 模块和私有 gitlab。我们的私人 gitlab 有组和子组。您可能缺少的部分是 ~/.netrc 并且您的全局 git 配置可能不正确。

我为此制定了 github 要点。您可以在这里找到它:https://gist.github.com/MicahParks/1ba2b19c39d1e5fccc3e892837b10e21

您可以找到下面粘贴的要点:

问题

go 命令行工具需要能够从您的私有 gitlab 获取依赖项,但需要进行身份验证。

这假设您的私人 gitlab 托管在 privategitlab.company.com

环境变量

建议使用以下环境变量:

export go111module=on
export goprivate=privategitlab.company.com

上面的行可能最适合您的 shell 启动,例如 ~/.bashrc

说明

go111module=on 告诉 golang 命令行工具您正在使用模块。我还没有使用不使用的项目对此进行测试 私有 gitlab 上的 golang 模块。

goprivate=privategitlab.company.com 告诉 golang 命令行工具不要使用公共互联网资源作为主机名 列出(如公共模块代理)。

从您的私人 gitlab 获取个人访问令牌

为了将来证明这些说明,请遵循 this guide from the GitLab docs。 我知道 golang 命令行工具需要 read_api 作用域才能工作,我可能怀疑 read_repository 作为 好吧,但尚未证实这一点。

设置 ~/.netrc

为了让 golang 命令行工具向 gitlab 进行身份验证,最好使用 ~/.netrc 文件。

要创建该文件(如果不存在),请运行以下命令:

touch ~/.netrc
chmod 600 ~/.netrc

现在编辑文件的内容以匹配以下内容:

machine privategitlab.company.com login username_here password token_here

其中 username_here 替换为您的 gitlab 用户名,token_here 替换为在 上一节。

常见错误

不要不要使用类似以下内容设置全局 git 配置:

git config --global url."[email protected]:".insteadof "https://privategitlab.company.com"

我相信在撰写本文时,golang 命令行工具并不完全支持 ssh git,这可能会导致 与 ~/.netrc 冲突。

奖励:ssh 配置文件

为了定期使用 git 工具,而不是 golang 命令行工具,设置 ~/.ssh/config 文件会很方便。 为此,请运行以下命令:

mkdir ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

请注意,上述文件和目录的权限对于 ssh 在其默认配置中工作至关重要 大多数 linux 系统。

然后,编辑 ~/.ssh/config 文件以匹配以下内容:

Host privategitlab.company.com
  Hostname privategitlab.company.com
  User USERNAME_HERE
  IdentityFile ~/.ssh/id_rsa

请注意上述文件中的空格很重要,如果不正确将使文件无效。

其中 username_here 是您的 gitlab 用户名,~/.ssh/id_rsa 是文件系统中 ssh 私钥的路径。 您已将其公钥上传到 gitlab。这里是 some instructions

好了,本文到此结束,带大家了解了《带有私有 gitlab (ssh) 的 Goland 模块》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

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