我正在寻找获得$ 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的,但这不应该改变这个过程:

$ 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

其他回答

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

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

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

这是Go Get中的硬代码。不是Git的原因。修改Go Source。 原因: reportforimportdynamic将请求:https://....go-get

// RepoRootForImportPath analyzes importPath to determine the
// version control system, and code repository to use.
func RepoRootForImportPath(importPath string, mod ModuleMode, security web.SecurityMode) (*RepoRoot, error) {
    rr, err := repoRootFromVCSPaths(importPath, security, vcsPaths)
    if err == errUnknownSite {
        rr, err = repoRootForImportDynamic(importPath, mod, security)
        if err != nil {
            err = importErrorf(importPath, "unrecognized import path %q: %v", importPath, err)
        }
    }
    if err != nil {
        rr1, err1 := repoRootFromVCSPaths(importPath, security, vcsPathsAfterDynamic)
        if err1 == nil {
            rr = rr1
            err = nil
        }
    }

所以添加gitlab域到vcsPaths就可以了。 下载go源代码:

vi ./src/cmd/go/internal/vcs/vcs.go    

查看下面的代码:

var vcsPaths = []*vcsPath{
    // GitHub
    {
        pathPrefix: "github.com",
        regexp:     lazyregexp.New(`^(?P<root>github\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

添加代码如下,XXXX Is Your Domain:

    // GitLab
    {
        pathPrefix: "gitlab.xxxx.com",
        regexp:     lazyregexp.New(`^(?P<root>gitlab.xxxx\.com/[A-Za-z0-9_.\-]+/[A-Za-z0-9_.\-]+)(/[A-Za-z0-9_.\-]+)*$`),
        vcs:        "git",
        repo:       "https://{root}",
        check:      noVCSSuffix,
    },

编译和替换go。

我已经创建了一个特定于用户的ssh-config,因此我的用户可以使用正确的凭证和密钥自动登录。

首先,我需要生成一个密钥对

ssh-keygen -t rsa -b 4096 -C "my@email.here"

并保存到例如~/.ssh/id_my_domain。请注意,这也是我连接到我的Github帐户的对口令(私有和公共),所以我的存储在~/.ssh/id_github_com。

然后我创建(或修改)了一个名为~/的文件。Ssh /config有一个条目:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_com

在另一个服务器上,“ssh-url”是admin@domain.com:username/private-repo.git,这个服务器的条目应该是:

Host domain.com
    HostName domain.com
    User admin
    IdentityFile ~/.ssh/id_domain_com

澄清一下,您需要确保正确设置了User、Host和HostName。

现在我可以浏览到go路径,然后去get <package>,例如去get main文件main/main。Go包含包(来自上一个例子)domain.com:username/private-repo.git。

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

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

module go.yourcompany.tld/the_repo

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

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

Edit

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