9

So I have a private repo that my main.go imports. I'm getting this error when I do a go build:

cannot find module for path

Do I need to do anything special for a private repo? I have been googling and can't find any good information. It works fine with dep.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
user1179567
  • 163
  • 1
  • 2
  • 5

4 Answers4

13

Do this

git config --global --add url."git@your-repo.com:".insteadOf "https://your-repo.com/"
export GOPRIVATE='your-repo.com'

Make sure your git clone via ssh works.

Ricardo La Rosa
  • 146
  • 1
  • 4
3

(Answer duplicated from this SO Question)

I wrote up a solution for this on Medium: Go Modules with Private Git Repositories.

The way we handle it is basically the same as the answer from Alex Pliutau, and the blog goes into some more detail with examples for how to set up your git config with tokens from GitHub/GitLab/BitBucket. It also goes into a working Dockerfile example for using modules with private repos.

The relevant bit for GitLab:

git config --global \
  url."https://oauth2:${personal_access_token}@privategitlab.com".insteadOf \
  "https://privategitlab.com"

#or 

git config --global \
  url."https://${user}:${personal_access_token}@privategitlab.com".insteadOf \
  "https://privategitlab.com"

I hope it's helpful.

timjonesdev
  • 179
  • 1
  • 11
1

You should use a SSH Key to fetch your repository, check if your SSH key is in system keychain too:

ssh-add -K ~/.ssh/id_rsa

Rodrigo Brito
  • 378
  • 3
  • 9
  • One important point is that the default name of the key that the Docker is going to see is id_rsa (and a few other defaults). This is very important. If your key has a different name, it won't work. Also I'm not sure why you would need the `-K` flag. – Alexis Wilke Oct 29 '21 at 18:56
0

Given that such a private repo is often in active development, I personally simply clone it to the "proper" location in my $GOPATH and use the source management (e.g. git) as you would any other project. Adding the SSH key as in Rodrigo's answer is great, but if you are actively developing the private repo anyway, the extra step to clone it to the right directory isn't by any means a difficult step vs being able to go get it.

So, for example, for a private repo hosted on Github, I would cd to $GOHOME/src/github.com/git-username-for-repo then git clone the-repo

RayfenWindspear
  • 6,116
  • 1
  • 30
  • 42
  • That works well if you have one report. If you have 100... (i.e. A depends on B and you're building a docker for A, the docker has to retrieve B) – Alexis Wilke Oct 28 '21 at 17:44