繁体   English   中英

如何在私有 GitLab 存储库中使用 Go

[英]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"

有没有办法让它发挥作用?

运行此命令:

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 版本对此进行了测试。

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

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

     ssh-keygen -t rsa -b 4096
  2. 在您的 Gitlab 项目中创建一个新的秘密变量 使用SSH_PRIVATE_KEY作为Key ,您的私钥的内容作为Value

  3. 使用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'
  4. 加入从步骤1中,你需要将项目创建为重点部署密钥对的公钥go get

使用 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

如果go get无法获取 repo,您​​可以随时直接使用 git 进行初始克隆:

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

然后工具将正常工作,期待go get -u这将需要-f标志,因为 git remote 与规范导入路径不匹配。

GitLab 11.8+ 版和 Go 1.13+ 版将使用您的 GitLab 个人令牌与 BASIC auth 一起使用。 转到 Gitlab 中的设置 -> 访问令牌,添加个人访问令牌或使用现有的访问令牌。 在您的 ~/.netrc 文件中,添加以下几行:

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

然后你应该可以去本地。

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

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

Gitlab 确实支持go get本机。

go get将向您提供的 url 发出 http 请求,并查找指向确切源代码管理路径的元标记。

对于我的 gitlab 安装,这是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。

我通常的做法是:

确保您使用的是 SSH。

完成后,您可以将 git 配置为使用ssh代替https

如果您使用的是 Mac OX。 你可以运行 vim ~/.gitconfig 并添加

[url "git@gitlab.com:"]
insteadOf = https://gitlab.com/

配置好后就可以运行了

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

我希望这有帮助。

对于 HTTPS 私有 gitlab 存储库,@Rick Smith 的回答就足够了。 这里是对HTTP repo的补偿,先运行命令:

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

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

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

dep 5.2 版开始, dep支持 Gitlab 私有存储库的私有存储库。

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

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

... (more private repositories if needed)
  1. 在您的 Go 存储库中,运行dep命令来解析私有包。 在这种情况下,
$ dep ensure -v

作为记录,这在使用 gitlab 7.3.2 的 go 之外工作,并且正如 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 中,但我还没有机会对其进行测试:即将进行的错误修复

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM