How to use Go with a private GitLab repo

Solution 1:

Run this command:

git config --global url."[email protected]:".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.

Solution 2:

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:

  1. 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
    
  2. Create a new Secret Variable in your Gitlab project. Use SSH_PRIVATE_KEY as Key and the content of your private key as Value.

  3. 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'
    
  4. 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.