Import private go modules from gitlab
Go modules is a dependency management system which has been introduced in go 1.11 https://golang.org/doc/go1.11#modules and it’s been default since go 1.13 https://blog.golang.org/using-go-modules.
Here is the repo https://gitlab.com/mbharti/test-private-module which is using private module https://gitlab.com/mbharti/greetings (Remember to create your own private module before you try to run test-private-module
and use that in go.mod
).
Greetings
module just has one file main.go
which has following code in it.
package greetingsimport "fmt"func Greet() {
fmt.Println("Welcome")
}
Greet
is called inside main.go
of test-private-module
project.
Now, clone https://gitlab.com/mbharti/test-private-module and go inside test-private-module
folder and run go build main.go
. It will give you below error:
go: gitlab.com/mbharti/greetings@v0.0.1: reading gitlab.com/mbharti/greetings/go.mod at revision v0.0.1: unknown revision v0.0.1
To dig deeper, Try to build again with -x
flag go build -x main.go
➜ test-private-module git:(master) go build -x main.go
WORK=/var/folders/0j/2168h7_50lbgxkb2wnbgn4pr0000gn/T/go-build869513335
# get https://proxy.golang.org/gitlab.com/mbharti/greetings/@v/v0.0.1.mod
# get https://proxy.golang.org/gitlab.com/mbharti/greetings/@v/v0.0.1.mod: 410 Gone (0.193s)
# get https://gitlab.com/mbharti/greetings?go-get=1
# get https://gitlab.com/mbharti/greetings?go-get=1: 200 OK (0.359s)
mkdir -p /Users/manoj.bharti/go/pkg/mod/cache/vcs # git3 https://gitlab.com/mbharti/greetings.git
# lock /Users/manoj.bharti/go/pkg/mod/cache/vcs/dd8ca0a55ffeee98f858381e5008c99c920135aace5956d11f0201aeb04318d2.lock# /Users/manoj.bharti/go/pkg/mod/cache/vcs/dd8ca0a55ffeee98f858381e5008c99c920135aace5956d11f0201aeb04318d2 for git3 https://gitlab.com/mbharti/greetings.git
cd /Users/manoj.bharti/go/pkg/mod/cache/vcs/dd8ca0a55ffeee98f858381e5008c99c920135aace5956d11f0201aeb04318d2; git tag -l
0.018s # cd /Users/manoj.bharti/go/pkg/mod/cache/vcs/dd8ca0a55ffeee98f858381e5008c99c920135aace5956d11f0201aeb04318d2; git tag -l
cd /Users/manoj.bharti/go/pkg/mod/cache/vcs/dd8ca0a55ffeee98f858381e5008c99c920135aace5956d11f0201aeb04318d2; git ls-remote -q origin
1.208s # cd /Users/manoj.bharti/go/pkg/mod/cache/vcs/dd8ca0a55ffeee98f858381e5008c99c920135aace5956d11f0201aeb04318d2; git ls-remote -q origin
# get https://gitlab.com/mbharti/greetings.git
# get https://gitlab.com/mbharti/greetings.git: 503 Service Unavailable (0.973s)
go: gitlab.com/mbharti/greetings@v0.0.1: reading gitlab.com/mbharti/greetings/go.mod at revision v0.0.1: unknown revision v0.0.1
First issue: 410 Gone
This is happening due to GOPROXY
https://golang.org/doc/go1.13#modules
The
GOPROXY
environment variable may now be set to a comma-separated list of proxy URLs or the special tokendirect
, and its default value is nowhttps://proxy.golang.org,direct
. When resolving a package path to its containing module, thego
command will try all candidate module paths on each proxy in the list in succession. An unreachable proxy or HTTP status code other than 404 or 410 terminates the search without consulting the remaining proxies.
To fix this issue, Set GOPRIVATE
environment variableexport GOPRIVATE=gitlab.com/mbharti/*
Now, If you try to build again. 410 Gone
issue should be fixed.
Second issue: unknown revision v0.0.1
This is because go modules communicates with git over https
https://golang.org/doc/faq#git_https, so we need to instruct git to use ssh
instead of https
.
Run following command
git config --global url."git@gitlab.com:".insteadof="https://gitlab.com"
it will add following below mentioned content to your ~/.gititconfig
, you can add it manually as well instead of running above command.
[url "git@gitlab.com:"] insteadOf = https://gitlab.com
Now, Run go run main.go
and it should be successfully able to fetch the private module https://gitlab.com/mbharti/greetings and print welcome
🎉
➜ test-private-module git:(master) ✗ go run main.go
go: downloading gitlab.com/mbharti/greetings v0.0.1
Welcome
Private module is located within gitlab sub-group
Repositories within gitlab subgroups are not recognised properly and here is link of the issue https://gitlab.com/gitlab-org/gitlab-foss/-/issues/32149. To fix it, let’s use replace directive https://golang.org/ref/mod#go-mod-file-replace. Modify the following command as per your repository path and add it to thego.mod
in the end.
replace (
gitlab.com/fixl-go-test/sub-group/go => gitlab.com/fixl-go-test/sub-group/go.git v0.0.1
)