git 如何在私有 GitLab 存储库中使用 Go

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29707689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:05:28  来源:igfitidea点击:

How to use Go with a private GitLab repo

gitgogetpackagegitlab

提问by James Fremen

GitLab is a free, open-source way to host private .gitrepositories but it does not seem to work with Go. When you create a project it generates a URL of the form:

GitLab 是一种免费的开源方式来托管私有.git存储库,但它似乎不适用于 Go。当您创建一个项目时,它会生成一个 URL 形式:

[email protected]:private-developers/project.git

where:

在哪里:

  • 1.2.3.4is the IP address of the gitlab server
  • private-developersis a user group which has access to the private repo
  • 1.2.3.4是gitlab服务器的IP地址
  • private-developers是一个可以访问私有仓库的用户组

Golang 1.2.1 doesn't seem to understand this syntax.

Golang 1.2.1 似乎不理解这种语法。

go get [email protected]:private-developers/project.git

results in:

结果是:

package [email protected]/project.git: unrecognized import path "[email protected]/project.git"

Is there a way to get this to work?

有没有办法让它发挥作用?

采纳答案by daplho

This issue is now resolved in Gitlab 8.* but is still unintuitive. The most difficult challenge indeed is go getand the following steps will allow you to overcome those:

这个问题现在在 Gitlab 8.* 中得到解决,但仍然不直观。最困难的挑战确实是go get,以下步骤将使您克服这些挑战:

  1. Create an SSH key pair. Be sure to not overwrite an existing pair that is by default saved in ~/.ssh/.

    ssh-keygen -t rsa -b 4096
    
  2. Create a new Secret Variablein your Gitlab project. Use SSH_PRIVATE_KEYas Keyand the content of your privatekey as Value.

  3. Modify your .gitlab-ci.ymlwith a before_script.

    before_script:
      # install ssh-agent if not already installed
      - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
      # run ssh-agent
      - eval $(ssh-agent -s)
      # add the SSH key stored in SSH_PRIVATE_KEY
      - ssh-add <(echo "$SSH_PRIVATE_KEY")
      # for Docker builds disable host key checking
      - mkdir -p ~/.ssh
      - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    
  4. Add the publickey from the key pair created in step 1 as a Deploy Keyin the project that you need to go get.

  1. 创建 SSH 密钥对。确保不要覆盖默认保存在~/.ssh/.

    ssh-keygen -t rsa -b 4096
    
  2. 在您的 Gitlab 项目中创建一个新的秘密变量。使用SSH_PRIVATE_KEY作为重点和您的内容私有密钥

  3. 修改你.gitlab-ci.ymlbefore_script

    before_script:
      # install ssh-agent if not already installed
      - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
      # run ssh-agent
      - eval $(ssh-agent -s)
      # add the SSH key stored in SSH_PRIVATE_KEY
      - ssh-add <(echo "$SSH_PRIVATE_KEY")
      # for Docker builds disable host key checking
      - mkdir -p ~/.ssh
      - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    
  4. 加入大众在步骤1中创建的密钥对关键部署的关键在你需要的项目go get

回答by Rick Smith

Run this command:

运行此命令:

git config --global url."[email protected]:".insteadOf "https://1.2.3.4/"

Assuming you have the correct privileges to git clonethe repository, this will make go getwork for allrepos on server 1.2.3.4.

假设您git clone对存储库具有正确的权限,这将对server 上的所有存储库go get起作用。1.2.3.4

I tested this with go version 1.6.2, 1.8, and 1.9.1.

我用 go 1.6.2、1.8 和 1.9.1 版本对此进行了测试。

回答by JimB

If go getcan't fetch the repo, you can always do the initial clone with git directly:

如果go get无法获取 repo,您​​始终可以直接使用 git 进行初始克隆:

git clone git@gitlab:private-developers/project.git $GOPATH/src/gitlab/private-developers/project

The tools will then work normally, expect for go get -uwhich will require the -fflag because the git remote doesn't match the canonical import path.

然后这些工具将正常工作,因为 git remote 与规范导入路径不匹配go get -u,因此需要该-f标志。

回答by captncraig

Gitlab does support go getnatively.

Gitlab 确实支持go get本机。

go getwill issue an http request to the url you provide and look for meta tags that point to the exact source control path.

go get将向您提供的 url 发出 http 请求,并查找指向确切源代码管理路径的元标记。

For my gitlab installation this is mygitlabdomain.com/myProject/myRepo. For you I assume this would be 1.2.3.4/private-developers/project.

对于我的 gitlab 安装,这是mygitlabdomain.com/myProject/myRepo. 对你来说,我认为这将是1.2.3.4/private-developers/project.

Unfortunately it only appears to give the http scm path, not the ssh path, so I had to enter my credentials to clone. You can easily fiddle with the remote in your local repository after it clones if you want to update to the ssh url.

不幸的是,它似乎只提供了 http scm 路径,而不是 ssh 路径,所以我必须输入我的凭据才能进行克隆。如果您想更新到 ssh url,您可以在克隆后轻松地在本地存储库中摆弄远程。

You can test the url by poking http://1.2.3.4:private-developers/project?go-get=1and viewing source and looking for the meta tag.

您可以通过戳http://1.2.3.4:private-developers/project?go-get=1和查看源代码并查找元标记来测试 url 。

回答by Feiyu Zhou

For HTTPS private gitlab repo, @Rick Smith's answer is enough. Here's a compensation for HTTP repo, first run the command:

对于 HTTPS 私有 gitlab 存储库,@Rick Smith 的回答就足够了。这里是对HTTP repo的补偿,先运行命令:

git config --global url."[email protected]:".insteadOf "http://mygitlab.com/"

then use below go getcommand to get the golang project:

然后使用以下go get命令获取golang项目:

go get -v  -insecure  mygitlab.com/user/repo

回答by Marlon Monroy

The way I usually do it is:

我通常的做法是:

Ensure you are using SSH.

确保您使用的是 SSH。

once that's done you can configure your git to use sshinstead https

一旦这样做,你可以配置你的git的使用ssh,而不是https

If you are using Mac OX. you can run vim ~/.gitconfig and add

如果您使用的是 Mac OX。你可以运行 vim ~/.gitconfig 并添加

[url "[email protected]:"]
insteadOf = https://gitlab.com/

once configured you can run

配置好后就可以运行了

GOPRIVATE="gitlab.com/your_username_or_group" go get gitlab.com/name_or_group/repo_name

I hope that helps.

我希望这有帮助。

回答by Paul Jacobus Van Staden

Easiest way with Gitlab

使用 Gitlab 的最简单方法

before_script:
  - git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/".insteadOf https://gitlab.com/
  - go env -w GOPRIVATE=gitlab.com/${CI_PROJECT_NAMESPACE}

See more details here: https://docs.gitlab.com/ee/user/project/new_ci_build_permissions_model.html#dependent-repositories

在此处查看更多详细信息:https: //docs.gitlab.com/ee/user/project/new_ci_build_permissions_model.html#dependent-repositories

回答by Cindy

GitLab version 11.8+ and Go version 1.13+ will work with BASIC auth by using your GitLab personal token. Go to Settings -> Access Tokens in your Gitlab, add a personal access token or use your existing one. In your ~/.netrc file, add following lines:

GitLab 11.8+ 版和 Go 1.13+ 版将使用您的 GitLab 个人令牌与 BASIC auth 一起使用。转到 Gitlab 中的设置 -> 访问令牌,添加个人访问令牌或使用现有的访问令牌。在您的 ~/.netrc 文件中,添加以下几行:

machine <your GitLab domain> (e.g. gitlab.com)
login <your GitLab id>
password <your GitLab personal access token>

Then you should be able to do go get locally.

那么你应该能够做到在本地获取。

If you need to build it in CI, then add following line in your .gitlab-ci.yml file:

如果您需要在 CI 中构建它,请在您的 .gitlab-ci.yml 文件中添加以下行:

before_script:
    - echo -e "machine <your GitLab domain>\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc

回答by Wesgur

From depversion 5.2, depsupports private repositories for Gitlab private repositories.

dep5.2 版开始,dep支持 Gitlab 私有存储库的私有存储库。

On .netrcfile, you can provide your Gitlab username and access token for accessing private repositories.

.netrc文件中,您可以提供您的 Gitlab 用户名和访问令牌以访问私有存储库。

  1. Create .netrcfile in your $HOME directory
  1. .netrc在 $HOME 目录中创建文件
$ touch $HOME/.netrc
  1. Edit your .netrcwith your Gitlab credentials
  1. .netrc使用您的 Gitlab 凭据编辑您的
machine gitlab.<private>.com
login <gitlab-username>
password <gitlab-access-token>

... (more private repositories if needed)
  1. In your Go repository, run the depcommand to resolve private packages. In this case,
  1. 在您的 Go 存储库中,运行dep命令以解析私有包。在这种情况下,
$ dep ensure -v

回答by James Fremen

For the record, this works outside of go using gitlab 7.3.2 and, as JimB has observed, can be used as a workaround. I find that i do get prompted for username/password, even though an SSH key is registered with gitlab:

作为记录,这在使用 gitlab 7.3.2 的 go 之外工作,并且正如 JimB 所观察到的,可以用作一种解决方法。我发现即使在 gitlab 中注册了 SSH 密钥,也会提示我输入用户名/密码:

git clone http://1.2.3.4/private-developers/project.git

Alternatively i can use the SSH equivalent which, since i have an SSH key registered with gitlab, avoids the prompts:

或者,我可以使用 SSH 等效项,因为我有一个在 gitlab 中注册的 SSH 密钥,因此可以避免出现提示:

git clone [email protected]:private-developers/project.git

Neither works with go currently. A fix may be in 7.9 but i haven't had a chance to test it: upcoming bugfix

目前两者都不适用于 go。修复程序可能在 7.9 中,但我还没有机会对其进行测试: 即将进行的错误修复