[英]How to use Go with a private GitLab repo
GitLab is a free, open-source way to host private .git
repositories but it does not seem to work with Go. GitLab 是一种免费的开源方式来托管私有
.git
存储库,但它似乎不适用于 Go。 When you create a project it generates a URL of the form:当您创建一个项目时,它会生成一个 URL 形式:
git@1.2.3.4:private-developers/project.git
where:在哪里:
1.2.3.4
is the IP address of the gitlab server 1.2.3.4
是gitlab服务器的IP地址private-developers
is a user group which has access to the private repo private-developers
是一个可以访问私有仓库的用户组Golang 1.2.1 doesn't seem to understand this syntax. Golang 1.2.1 似乎不理解这种语法。
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?有没有办法让它发挥作用?
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
.假设你有正确的权限来
git clone
库,这将使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 版本对此进行了测试。
This issue is now resolved in Gitlab 8.* but is still unintuitive.这个问题现在在 Gitlab 8.* 中得到解决,但仍然不直观。 The most difficult challenge indeed is
go get
and the following steps will allow you to overcome those:最困难的挑战确实是
go get
,以下步骤将使您克服这些挑战:
Create an SSH key pair.创建 SSH 密钥对。 Be sure to not overwrite an existing pair that is by default saved in
~/.ssh/
.确保不要覆盖默认保存在
~/.ssh/
的现有对。
ssh-keygen -t rsa -b 4096
Create a new Secret Variable in your Gitlab project.在您的 Gitlab 项目中创建一个新的秘密变量。 Use
SSH_PRIVATE_KEY
as Key and the content of your private key as Value .使用
SSH_PRIVATE_KEY
作为Key ,您的私钥的内容作为Value 。
Modify your .gitlab-ci.yml
with a before_script
.使用
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'
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
.加入从步骤1中,你需要将项目创建为重点部署密钥对的公钥
go get
。
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
If go get
can'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 -u
which will require the -f
flag because the git remote doesn't match the canonical import path.然后工具将正常工作,期待
go get -u
这将需要-f
标志,因为 git remote 与规范导入路径不匹配。
GitLab version 11.8+ and Go version 1.13+ will work with BASIC auth by using your GitLab personal token. GitLab 11.8+ 版和 Go 1.13+ 版将使用您的 GitLab 个人令牌与 BASIC auth 一起使用。 Go to Settings -> Access Tokens in your Gitlab, add a personal access token or use your existing one.
转到 Gitlab 中的设置 -> 访问令牌,添加个人访问令牌或使用现有的访问令牌。 In your ~/.netrc file, add following lines:
在您的 ~/.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
Gitlab does support go get
natively. Gitlab 确实支持
go get
本机。
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. go get
将向您提供的 url 发出 http 请求,并查找指向确切源代码管理路径的元标记。
For my gitlab installation this is mygitlabdomain.com/myProject/myRepo
.对于我的 gitlab 安装,这是
mygitlabdomain.com/myProject/myRepo
。 For you I assume this would be 1.2.3.4/private-developers/project
.对于您,我假设这将是
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.不幸的是,它似乎只提供了 http scm 路径,而不是 ssh 路径,所以我必须输入我的凭据才能进行克隆。 You can easily fiddle with the remote in your local repository after it clones if you want to update to the ssh url.
如果您想更新到 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.您可以通过戳
http://1.2.3.4:private-developers/project?go-get=1
并查看源代码并查找元标记来测试 url。
The way I usually do it is:我通常的做法是:
Ensure you are using SSH.确保您使用的是 SSH。
once that's done you can configure your git to use ssh
instead https
完成后,您可以将 git 配置为使用
ssh
代替https
If you are using Mac OX.如果您使用的是 Mac OX。 you can run vim ~/.gitconfig and add
你可以运行 vim ~/.gitconfig 并添加
[url "git@gitlab.com:"]
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.我希望这有帮助。
For HTTPS private gitlab repo, @Rick Smith's answer is enough.对于 HTTPS 私有 gitlab 存储库,@Rick Smith 的回答就足够了。 Here's a compensation for HTTP repo, first run the command:
这里是对HTTP repo的补偿,先运行命令:
git config --global url."git@mygitlab.com:".insteadOf "http://mygitlab.com/"
then use below go get
command to get the golang project:然后使用下面的
go get
命令来获取 golang 项目:
go get -v -insecure mygitlab.com/user/repo
From dep version 5.2, dep
supports private repositories for Gitlab private repositories.从dep 5.2 版开始,
dep
支持 Gitlab 私有存储库的私有存储库。
On .netrc file, you can provide your Gitlab username and access token for accessing private repositories.在.netrc文件中,您可以提供您的 Gitlab 用户名和访问令牌以访问私有存储库。
.netrc
file in your $HOME directory.netrc
文件$ touch $HOME/.netrc
.netrc
with your Gitlab credentials.netrc
machine gitlab.<private>.com
login <gitlab-username>
password <gitlab-access-token>
... (more private repositories if needed)
dep
command to resolve private packages.dep
命令来解析私有包。 In this case,$ dep ensure -v
For the record, this works outside of go using gitlab 7.3.2 and, as JimB has observed, can be used as a workaround.作为记录,这在使用 gitlab 7.3.2 的 go 之外工作,并且正如 JimB 所观察到的,可以用作一种解决方法。 I find that i do get prompted for username/password, even though an SSH key is registered with gitlab:
我发现即使在 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 git@1.2.3.4:private-developers/project.git
Neither works with go currently.目前两者都不适用于 go。 A fix may be in 7.9 but i haven't had a chance to test it: upcoming bugfix
修复程序可能在 7.9 中,但我还没有机会对其进行测试:即将进行的错误修复
You can setup your git credentials and Go will use them:您可以设置您的 git 凭据,Go 将使用它们:
git config credential.helper store
echo https://your-github-username:your-generated-password@github.com >> ~/.git-credentials
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.