How to use Go with a private GitLab repo

后端 未结 11 1623
时光说笑
时光说笑 2020-12-04 11:11

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 fo

相关标签:
11条回答
  • 2020-12-04 11:58

    Easiest way with 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

    0 讨论(0)
  • 2020-12-04 11:59

    You can setup your git credentials and Go will use them:

    1. generate a unique password on your github (somewhere in settings).
    2. git config credential.helper store
    3. echo https://your-github-username:your-generated-password@github.com >> ~/.git-credentials
    4. profit.
    0 讨论(0)
  • 2020-12-04 12:06

    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.

    0 讨论(0)
  • 2020-12-04 12:06

    GitLab version 11.8+ and Go version 1.13+ will work with BASIC auth by using your GitLab personal token. Go to Settings -> Access Tokens in your Gitlab, add a personal access token or use your existing one. In your ~/.netrc file, add following lines:

    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:

    before_script:
        - echo -e "machine <your GitLab domain>\nlogin gitlab-ci-token\npassword ${CI_JOB_TOKEN}" > ~/.netrc
    
    0 讨论(0)
  • 2020-12-04 12:07

    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

    0 讨论(0)
提交回复
热议问题