可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
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:
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
Create a new Secret Variable in your Gitlab project. Use SSH_PRIVATE_KEY
as Key and the content of your private key as Value.
Modify your .gitlab-ci.yml
with 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'
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
.
回答2:
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.
回答3:
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.
回答4:
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.
回答5:
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:
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:
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
回答6:
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.
- Create
.netrc
file in your $HOME directory
$ touch $HOME/.netrc
- Edit your
.netrc
with your Gitlab credentials
machine gitlab.<private>.com
login <gitlab-username>
password <gitlab-access-token>
... (more private repositories if needed)
- In your Go repository, run the
dep
command to resolve private packages. In this case,
$ dep ensure -v
回答7:
You can setup your git credentials and Go will use them:
- generate a unique password on your github (somewhere in settings).
git config credential.helper store
echo https://your-github-username:your-generated-password@github.com >> ~/.git-credentials
- profit.
回答8:
For HTTPS private gitlab repo, @Rick Smith is enough. Here's a compensation for HTTP repo, first run the command:
git config --global url."git@mygitlab.com:".insteadOf "http://mygitlab.com/"
then use below go get
command to get the golang project:
go get -v -insecure mygitlab.com/user/repo
回答9:
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
[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.