我正在寻找获得$ go与私人存储库工作的方法,经过许多谷歌尝试。

第一次尝试:

$ go get -v gitlab.com/secmask/awserver-go
Fetching https://gitlab.com/secmask/awserver-go?go-get=1
https fetch failed.
Fetching http://gitlab.com/secmask/awserver-go?go-get=1
Parsing meta tags from http://gitlab.com/secmask/awserver-go?go-get=1 (status code 200)
import "gitlab.com/secmask/awserver-go": parse http://gitlab.com/secmask/awserver-go?go-get=1: no go-import meta tags
package gitlab.com/secmask/awserver-go: unrecognized import path "gitlab.com/secmask/awserver-go

是的,它没有看到元标签,因为我不知道如何提供登录信息。

第二次尝试:

按照https://gist.github.com/shurcooL/6927554。将config添加到.gitconfig。

[url "ssh://git@gitlab.com/"]
    insteadOf = https://gitlab.com/
$ go get -v gitlab.com/secmask/awserver-go --> not work
$ go get -v gitlab.com/secmask/awserver-go.git --> work but I got src/gitlab.com/secmask/awserer-go.git

是的,它的工作,但与。git扩展与我的项目名称,我可以重命名为原来的,但每次做$ go get不太好,有没有其他方法?


当前回答

对于独立/最终回购,作为快速修复,为什么不直接在go中命名模块呢?Mod作为一个包使用贵公司的域名…?

module go.yourcompany.tld/the_repo

Go.yourcompany.tld甚至不必作为有效(子)域名存在…

同样,在同一个地方。mod你可以使用替换块/行来使用之前以同样的方式克隆的私有回购(在$GOPATH/src/go.yourcompany.tld中克隆的各自文件夹中)(为什么我们必须在GitHub中依赖太多?)

Edit

不用说,私人回购通常应该是私人回购,通常是标准的git回购,对吧?有了这些,为什么不直接克隆git,然后进入克隆文件夹?

其他回答

你有一个东西要配置。这个例子是基于GitHub的,但这不应该改变这个过程:

$ git config --global url.git@github.com:.insteadOf https://github.com/
$ cat ~/.gitconfig
[url "git@github.com:"]
    insteadOf = https://github.com/
$ go get github.com/private/repo

为了让Go模块工作(使用Go 1.11或更新版本),你还需要设置GOPRIVATE变量,以避免使用公共服务器获取代码:

export GOPRIVATE=github.com/private/repo

看起来像是GitLab的5769期。

在GitLab中,由于存储库总是以.git结尾,我必须在存储库名称的末尾指定.git以使其正常工作,例如: 进口“example.org/myuser/mygorepo.git” 和: $ go get example.org/myuser/mygorepo.git 看起来GitHub通过添加“。git”解决了这个问题。

它应该在“增加对Go的存储库检索的支持”中得到解决。#5958”,前提是正确的元标记就位。 尽管Go本身仍然存在一个问题:“cmd/ Go: Go get无法发现HTML5文档中的元标签”。

对于我来说,其他人提供的解决方案在go get时仍然给出了以下错误

git@gl.nimi24.com:拒绝权限(publickey)。 无法从远程存储库读取。 请确保您拥有正确的访问权限 并且存储库存在。

这个解决方案需要什么

如其他人所述: git配置——全局url."git@github.com:". insteadof "https://github.com/" 从我的。/ssh/id_rsa密钥中删除密码短语,该密钥用于验证到存储库的连接。这可以通过在提示时输入空密码来完成: ssh - keygen - p

为什么会这样

这不是一个很好的解决办法,因为在您的私钥上有一个密码短语总是更好的,但它在OpenSSH内部的某个地方引起了问题。

go get uses internally git, which uses openssh to open the connection. OpenSSH takes the certs necessary for authentication from .ssh/id_rsa. When executing git commands from the command line an agent can take care of opening the id_rsa file for you so that you do not have to specify the passphrase every time, but when executed in the belly of go get, this did not work somewhy in my case. OpenSSH wants to prompt you then for a password but since it is not possible due to how it was called, it prints to its debug log:

read_passphrase:不能打开/dev/tty:没有这样的设备或地址

然后就失败了。如果您从密钥文件中删除了密码短语,OpenSSH将在没有提示的情况下获取您的密钥,并且可以正常工作

这可能是由于Go同时获取模块并同时打开到Github的多个SSH连接造成的(如本文所述)。OpenSSH调试日志显示到存储库的初始连接成功,但后来由于某种原因再次尝试,这一次选择了请求密码短语,这在一定程度上支持了这一点。

然而,上述文章中提出的使用SSH连接多路复用的解决方案对我来说并不适用。为了记录,作者建议在受影响主机的ssh配置文件中添加以下conf:

  ControlMaster auto
  ControlPersist 3600
  ControlPath ~/.ssh/%r@%h:%p

但就像我说的,对我来说这行不通,也许我做错了

以上所有的方法对我都不起作用。克隆回购工作正确,但我仍然得到一个无法识别的导入错误。

因为它代表Go v1.13,我在文档中发现我们应该像这样使用GOPRIVATE env变量:

$ GOPRIVATE=github.com/ORGANISATION_OR_USER_NAME go get -u github.com/ORGANISATION_OR_USER_NAME/REPO_NAME

对于使用私有GitLabs的人来说,这里有一个片段可能会有所帮助:https://gist.github.com/MicahParks/1ba2b19c39d1e5fccc3e892837b10e21

也贴在下面:

问题

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

这假设您的私有GitLab托管在privategitlab.company.com。

环境变量

建议使用以下环境变量:

export GO111MODULE=on
export GOPRIVATE=privategitlab.company.com # this is comma delimited if using multiple private repos

上面的代码行可能最适合用于shell启动,比如~/.bashrc。

解释

GO111MODULE=on告诉Golang命令行工具您正在使用模块。我还没有在不使用的项目中测试过这个功能 私人GitLab上的Golang模块。

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

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

为了将来证明这些说明,请遵循GitLab文档中的指南。 我知道read_api作用域是Golang命令行工具工作所必需的,我可能怀疑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."git@privategitlab.company.com:".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。这里有一些说明。