How to use Go with a private GitLab repo

huangapple go评论95阅读模式
英文:

How to use Go with a private GitLab repo

问题

GitLab是一种免费、开源的托管私有.git仓库的方式,但似乎无法与Go一起使用。当你创建一个项目时,它会生成以下形式的URL:

git@1.2.3.4:private-developers/project.git

其中:

  • 1.2.3.4是GitLab服务器的IP地址
  • private-developers是一个具有对私有仓库访问权限的用户组

然而,Golang 1.2.1似乎无法理解这种语法。

执行以下命令:

go get git@1.2.3.4:private-developers/project.git

会导致以下错误:

package git@23.251.148.129/project.git: unrecognized import path "git@1.2.3.4/project.git"

有没有办法让它正常工作呢?

英文:

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

git@1.2.3.4:private-developers/project.git

where:

  • 1.2.3.4 is the IP address of the gitlab server
  • private-developers is a user group which has access to the private repo

Golang 1.2.1 doesn't seem to understand this syntax.

go get git@1.2.3.4:private-developers/project.git

results in:

package git@23.251.148.129/project.git: unrecognized import path "git@1.2.3.4/project.git"

Is there a way to get this to work?

答案1

得分: 44

运行以下命令:

git config --global url."git@1.2.3.4:".insteadOf "https://1.2.3.4/"

假设你有正确的权限来git clone该存储库,这将使得go get在服务器1.2.3.4上的所有存储库中都能正常工作。

我已经在go版本1.6.2、1.8和1.9.1上进行了测试。

英文:

Run this command:

git config --global url."git@1.2.3.4:".insteadOf "https://1.2.3.4/"

Assuming you have the correct privileges to git clone the repository, this will make go get work for all repos on server 1.2.3.4.

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

答案2

得分: 23

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

  1. 创建一个SSH密钥对。确保不要覆盖默认保存在~/.ssh/中的现有密钥对。

     ssh-keygen -t rsa -b 4096
    
  2. 在你的Gitlab项目中创建一个新的"Secret Variable"。将SSH_PRIVATE_KEY作为"Key",将你的私钥内容作为"Value"。

  3. .gitlab-ci.yml中添加一个before_script

     before_script:
       # 如果尚未安装ssh-agent,则安装它
       - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
       # 运行ssh-agent
       - eval $(ssh-agent -s)
       # 添加存储在SSH_PRIVATE_KEY中的SSH密钥
       - ssh-add <(echo "$SSH_PRIVATE_KEY")
       # 对于Docker构建,禁用主机密钥检查
       - mkdir -p ~/.ssh
       - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    
  4. 将第1步创建的公钥作为"Deploy Key"添加到需要go get的项目中。

英文:

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

  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 Variable in your Gitlab project. Use SSH_PRIVATE_KEY as Key and the content of your private key as Value.

  3. Modify your .gitlab-ci.yml with a before_script.

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

答案3

得分: 20

最简单的方法是使用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}

更多详细信息请参阅:https://docs.gitlab.com/ee/user/project/new_ci_build_permissions_model.html#dependent-repositories

英文:

Easiest way with Gitlab

before_script:
  - git config --global url.&quot;https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/&quot;.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

答案4

得分: 9

GitLab版本11.8+和Go版本1.13+可以通过使用GitLab个人令牌与基本身份验证一起使用。
在GitLab中,转到“设置”->“访问令牌”,添加一个个人访问令牌或使用现有的令牌。
在您的~/.netrc文件中,添加以下行:

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

然后您应该能够在本地执行go get命令。

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

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

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:

machine &lt;your GitLab domain&gt; (e.g. gitlab.com)
login &lt;your GitLab id&gt;
password &lt;your GitLab personal access token&gt;

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:

before_script:
    - echo -e &quot;machine &lt;your GitLab domain&gt;\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}&quot; &gt; ~/.netrc

答案5

得分: 7

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

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

然后工具将正常工作,除了go get -u将需要-f标志,因为git远程不匹配规范的导入路径。

英文:

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

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

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

答案6

得分: 3

Gitlab原生支持go get

go get会向您提供的URL发出HTTP请求,并查找指向确切源代码控制路径的元标签。

对于我的Gitlab安装,URL是mygitlabdomain.com/myProject/myRepo。对于您来说,我假设这将是1.2.3.4/private-developers/project

不幸的是,它似乎只提供HTTP SCM路径,而不是SSH路径,所以我不得不输入我的凭据来克隆。如果您想要更新为SSH URL,您可以在克隆后轻松地在本地存储库中调整远程设置。

您可以通过访问http://1.2.3.4:private-developers/project?go-get=1并查看源代码以及查找元标签来测试该URL。

英文:

Gitlab does support go get natively.

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

For my gitlab installation this is mygitlabdomain.com/myProject/myRepo. For you I assume this would be 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.

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

答案7

得分: 3

dep版本5.2开始,dep支持Gitlab私有仓库的使用。

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

  1. 在您的$HOME目录下创建.netrc文件
$ touch $HOME/.netrc
  1. 使用您的Gitlab凭据编辑.netrc文件
machine gitlab.&lt;private&gt;.com
login &lt;gitlab-username&gt;
password &lt;gitlab-access-token&gt;

...(如果需要更多私有仓库)
  1. 在您的Go仓库中,运行dep命令以解析私有包。在这种情况下,
$ dep ensure -v
英文:

From dep version 5.2, dep supports private repositories for Gitlab private repositories.

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

  1. Create .netrc file in your $HOME directory
$ touch $HOME/.netrc
  1. Edit your .netrc with your Gitlab credentials
machine gitlab.&lt;private&gt;.com
login &lt;gitlab-username&gt;
password &lt;gitlab-access-token&gt;

... (more private repositories if needed)
  1. In your Go repository, run the dep command to resolve private packages. In this case,
$ dep ensure -v

答案8

得分: 3

我通常的做法是:

确保你正在使用SSH。

一旦完成,你可以配置你的git使用ssh而不是https

如果你使用的是Mac OS,你可以运行vim ~/.gitconfig,并添加以下内容:

insteadOf = https://gitlab.com/

配置完成后,你可以运行以下命令:

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

希望对你有所帮助。

英文:

The way I usually do it is:

Ensure you are using SSH.

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

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

insteadOf = https://gitlab.com/

once configured you can run

GOPRIVATE=&quot;gitlab.com/your_username_or_group&quot; go get gitlab.com/name_or_group/repo_name

I hope that helps.

答案9

得分: 2

对于HTTPS私有的GitLab仓库,@Rick Smith的回答已经足够了。以下是针对HTTP仓库的补充,首先运行以下命令:

git config --global url."git@mygitlab.com:".insteadOf "http://mygitlab.com/"

然后使用下面的go get命令获取Golang项目:

go get -v -insecure mygitlab.com/user/repo
英文:

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

git config --global url.&quot;git@mygitlab.com:&quot;.insteadOf &quot;http://mygitlab.com/&quot;

then use below go get command to get the golang project:

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

答案10

得分: 1

记录一下,在使用gitlab 7.3.2之外的环境中,这个方法是可行的,正如JimB所观察到的,可以用作解决方法。我发现,即使在gitlab注册了SSH密钥,我仍然会被提示输入用户名/密码:

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

或者,我可以使用SSH的等效方式,由于我在gitlab注册了SSH密钥,这样可以避免提示:

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

目前,这两种方法在go中都不起作用。修复可能在7.9版本中进行,但我还没有机会测试。

即将修复的错误

英文:

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:

<!-- language: lang-bash -->

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:

<!-- language: lang-bash -->

git clone git@1.2.3.4: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

答案11

得分: 0

你可以设置你的git凭据,Go将使用它们:

  1. 在你的github上生成一个唯一的密码(在设置中的某个地方)。
  2. git config credential.helper store
  3. echo https://your-github-username:your-generated-password@github.com >> ~/.git-credentials
  4. 获利。
英文:

You can setup your git credentials and Go will use them:

  1. generate a unique password on your github (somewhere in settings).
  2. git config credential.helper store
  3. echo https://your-github-username:your-generated-password@github.com &gt;&gt; ~/.git-credentials
  4. profit.

huangapple
  • 本文由 发表于 2015年4月18日 03:19:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/29707689.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定