Downloading private repo under Gitlab subgroup using GO Module

To download a private repository hosted in Gitlab/Github with GO Module, we can change the url to add git user and access token:

git config --global url."https://${GIT_USER}:${GIT_TOKEN}@gitlab.com/".insteadOf "https://gitlab.com/"

In Gitlab CI/CD, it should be https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/.

However, if the private respository is under a Gitlab subgroup, eg, gitlab.com/company/groupName/repository, the above approach doesn’t work. The following approach with netrc will fix it:

printf "machine gitlab.com\n\
    login ${GIT_USER}\n\
    password ${GIT_TOKEN}\n"\
    >> /root/.netrc
go env -w GOPRIVATE=gitlab.com/company
Yuhui Lin
Yuhui Lin
Software Engineer
Next
Previous