如何使用私人Gitlab回购GoLang?

Jam*_*men 35 git get package go gitlab

Gitlab是一种免费的,开源的托管私有.git存储库的方式,但它似乎不适用于GoLang.创建项目时,它会生成以下形式的URL:

git@1.2.3.4:private-developers/project.git
Run Code Online (Sandbox Code Playgroud)

哪里:

  • 1.2.3.4 是gitlab服务器的IP地址
  • private-developers 是一个可以访问私人仓库的用户组

Golang 1.2.1似乎不理解这种语法.

go get git@1.2.3.4:private-developers/project.git
Run Code Online (Sandbox Code Playgroud)

结果是:

package git@23.251.148.129/project.git: unrecognized import path "git@1.2.3.4/project.git"
Run Code Online (Sandbox Code Playgroud)

有没有办法让这个工作?

Ric*_*ith 36

运行此命令:

git config --global url."git@1.2.3.4:".insteadOf "https://1.2.3.4/"
Run Code Online (Sandbox Code Playgroud)

假设您拥有对git clone存储库的正确权限,这将使服务器上的所有存储库都能go get正常工作.1.2.3.4

我用版本1.6.2,1.8和1.9.1测试了这个.


dap*_*lho 17

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

  1. 创建SSH密钥对.请务必不要覆盖默认保存的现有对~/.ssh/.

    ssh-keygen -t rsa -b 4096
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在Gitlab项目中创建一个新的秘密变量.使用SSH_PRIVATE_KEY作为重点和您的内容私有密钥.

  3. .gitlab-ci.yml用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'
    
    Run Code Online (Sandbox Code Playgroud)
  4. 加入大众从步骤1作为创建的密钥对关键部署的关键在你需要的项目go get.

  • 在我向`before_script`部分添加了另一行之后,这对我有用:-git config --global url。“ git@gitlab.com:”。instof“ https://gitlab.com/” (2认同)

小智 9

使用 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}
Run Code Online (Sandbox Code Playgroud)

在此处查看更多详细信息:https : //docs.gitlab.com/ee/user/project/new_ci_build_permissions_model.html#dependent-repositories


Jim*_*imB 7

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

git clone git@gitlab:private-developers/project.git $GOPATH/src/gitlab/private-developers/project
Run Code Online (Sandbox Code Playgroud)

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


Cin*_*ndy 6

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>
Run Code Online (Sandbox Code Playgroud)

那么你应该能够做到在本地获取。

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

before_script:
    - echo -e "machine <your GitLab domain>\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
Run Code Online (Sandbox Code Playgroud)