如何在私有 GitLab 存储库中使用 Go [英] 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 项目中创建一个新的秘密变量.使用 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 *
StrictHostKeyChecking no
" > ~/.ssh/config'
将在步骤 1 中创建的密钥对中的 public 密钥添加为您需要go get的项目中的部署密钥代码>.
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
.
这篇关于如何在私有 GitLab 存储库中使用 Go的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!