如何将Go与私有的GitLab存储库一起使用 [英] How to use Go with a private GitLab repo
问题描述
GitLab是一种免费的开放源代码方式,用于托管私有.git
存储库,但似乎不适用于Go.创建项目时,它会生成以下形式的URL:
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
其中:
-
1.2.3.4
是gitlab服务器的IP地址 -
private-developers
是有权访问私有存储库的用户组
1.2.3.4
is the IP address of the gitlab serverprivate-developers
is a user group which has access to the private repo
Golang 1.2.1似乎不了解这种语法.
Golang 1.2.1 doesn't seem to understand this syntax.
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"
是否有办法使它正常工作?
Is there a way to get this to work?
推荐答案
此问题现在已在Gitlab 8. *中得以解决,但是仍然不直观.实际上,最困难的挑战是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:
-
创建SSH密钥对.确保不覆盖默认情况下保存在
~/.ssh/
中的现有对.
ssh-keygen -t rsa -b 4096
在Gitlab项目中创建一个新的 Secret Variable .将SSH_PRIVATE_KEY
用作 Key ,并将 private 密钥的内容用作 Value .
Create a new Secret Variable in your Gitlab project. Use SSH_PRIVATE_KEY
as Key and the content of your private key as Value.
用before_script
修改.gitlab-ci.yml
.
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'
将步骤1中创建的密钥对中的 public 密钥添加为需要go get
的项目中的 Deploy密钥.
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
.
这篇关于如何将Go与私有的GitLab存储库一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!