我正在寻找获得$ 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不太好,有没有其他方法?


当前回答

在这里生成一个github oauth令牌,并将您的github令牌作为环境变量导出:

export GITHUB_TOKEN=123

设置git配置为使用基本认证url:

git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/".insteadOf "https://github.com/"

现在你可以去拿你的私人回购了。

其他回答

在尝试了多种解决方案后,我的问题仍然存在。设置~/.netrc和SSH配置文件后的最终解决方案是在~/.bash_profile中添加以下行

出口GOPRIVATE = " github.com/(组织)”

对于使用私有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。这里有一些说明。

在设置GOPRIVATE和git配置后…

人们在获取私有资源时仍然会遇到这样的问题:

https fetch: Get "https://private/user/repo?go-get=1": EOF

他们不能使用私人回购没有。git扩展名。

原因是go工具不知道这个repo的VCS协议,git或svn或其他任何协议,不像github.com或golang.org,它们是硬编码到go的源代码中。

然后go工具将在获取你的私有repo之前执行一个https查询:

https://private/user/repo?go-get=1

如果你的私人回购不支持https请求,请使用replace直接告诉它:

require private/user/repo v1.0.0

...

replace private/user/repo => private.server/user/repo.git v1.0.0

https://golang.org/cmd/go/#hdr-Remote_import_paths

正确的方法是手动将存储库放在正确的位置。一旦有了存储库,您可以使用go get -u来更新包,然后使用install来安装它。一个名为

github.com/secmask/awserver-go

进入

$GOPATH/src/github.com/secmask/awserver-go

您键入的命令是:

cd $GOPATH/src/github.com/secmask
git clone git@github.com:secmask/awserver-go.git

在这里生成一个github oauth令牌,并将您的github令牌作为环境变量导出:

export GITHUB_TOKEN=123

设置git配置为使用基本认证url:

git config --global url."https://$GITHUB_TOKEN:x-oauth-basic@github.com/".insteadOf "https://github.com/"

现在你可以去拿你的私人回购了。