Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tomasruud/680cb2a3d3bb22c2017a8c1d203c17d9 to your computer and use it in GitHub Desktop.
Save tomasruud/680cb2a3d3bb22c2017a8c1d203c17d9 to your computer and use it in GitHub Desktop.
go get private GitLab with group and subgroup (Golang modules)

Problem

The go command line tool needs to be able to fetch dependencies from your private GitLab, but authenticaiton is required.

This assumes your private GitLab is hosted at gitlab.com/my-organization, and that you are using GO version >=1.17.

Environment variables

The following environment variables are recommended:

export GOPRIVATE=gitlab.com/my-organization

The above lines might fit best in your shell startup, like a ~/.bashrc.

Explanation

GOPRIVATE=gitlab.com/my-organization tells Golang command line tools to not use public internet resources for the hostnames listed (like the public module proxy).

This variable can list several domains to include, see https://goproxy.io/docs/GOPRIVATE-env.html for more details.

Get a personal access token from your private GitLab

To future proof these instructions, please follow this guide from the GitLab docs.

I know that the read_api scope is required for Golang command line tools to work, and I may suspect read_repository as well, but have not confirmed this.

Set up the ~/.netrc

In order for the Golang command line tools to authenticate to GitLab, a ~/.netrc file is best to use.

To create the file if it does not exist, run the following commands:

touch ~/.netrc
chmod 600 ~/.netrc

Now edit the contents of the file to match the following:

machine gitlab.com login USERNAME_HERE password TOKEN_HERE

Where USERNAME_HERE is replaced with your GitLab username and TOKEN_HERE is replaced with the access token aquired in the previous section.

Common mistakes

Do not set up a global git configuration with something along the lines of this:

git config --global url."git@privategitlab.company.com:".insteadOf "https://privategitlab.company.com"

I beleive at the time of writing this, the SSH git is not fully supported by Golang command line tools and this may cause conflicts with the ~/.netrc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment