Search code examples
gogithubbuild

go build with private repositories


I am trying to build a go application that makes use of git repositories that are private. I have an ssh key for my github account and have the following in my .gitconfig file:

[url "https://username:[email protected]"]
    insteadOf = https://github.com
[url "ssh://[email protected]/"]
    insteadOf = https://github.com/

when I execute go test or go build I am asked for the passphrase. I then get a response:

go: found github.com/x/businesses in github.com/x/businesses v0.0.0-yy
go: cmd/main/cmd imports
  github.com/x/businesses: github.com/x/[email protected]/go.mod: 
  verifying module: github.com/x/[email protected]/go.mod: 
  reading https://sum.golang.org/lookup/github.com/xx/[email protected]: 410 Gone
server response:
  not found: github.com/x/[email protected]: 
  invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* 
  in tmp/gopath/pkg/mod/cache/vcs/zz: 
    exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled

I tried removing the top insteadOf in the .gitconfig for no reason other then to try something.


Solution

  • Your git config should look something like

    [url "[email protected]:"] insteadOf = https://github.com/
    

    or similar to what you have.

    Then you can go get your pacakge by telling Go that your using a private repo like this:

    GOPRIVATE="github.com/your_username_or_org" go get github.com/name_or_org/repo_name
    

    Also, I usually dont put a passprahse on my SSH keys since is kinda annoying to type it in each time but of course is more secure by adding it like someone pointed out in the comments.