Using GitLab Private Go Package On Your Local Setup and CI 🚀

Muhammed İKİNCİ
Modanisa Engineering
3 min readDec 1, 2021

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

As we all know, every programming language has helper packages for some purpose. Thus, allows a written code to be used for repetitive tasks. The goal is to ensure that there is only one way of accomplishing the task, and it is as painless as possible.

Every software package manager (npm, go packages, etc.) work on public repositories. What about, we want to work on a private repo -in our case go package- how can we configure that not only our local machine but also our own pipeline? For this purpose, we will present a solution for private packages.

Assume that, we want to reuse our private package below.

https://gitlab.com/modanisa/my_package

If we try to install this package using go get we immediately get an error like below.

go get gitlab.com/modanisa/my_package.gitThe project you were looking for could not be found or you don't have permission to view it.

In the first step, we have to configure our go package manager. To install private repo, we have to change GOPRIVATE environment variable as below.

go env -w GOPRIVATE="gitlab.com/modanisa/*"

Next, we must provide ssh or access-token knowledge for getting the private package on our own machine. There are many ways to provide this knowledge. We just show a one way using .netrc.

.netrc

.netrc file is nothing but used for authentication purposes. You can find this file in your $HOME directory. If not used this file before, you cannot find so you have to create it.

As my favorite text edit nano, I open this file as

nano ~/.netrc

We have to put login and password (GitLab access token) credentials. To create GitLab access token, you must ensure our access token has read_api and read_repository scope at least.

For example, username: ikinci@gitlab.com, access:token: glpat-ydD***********)

machine gitlab.com
login <username>
password <access.token>

In this way, no need to use .gitconfig insteadOf thingies.

After all these changes explained above, if we try to install our package again, we continue to get an error as

go get: gitlab.com/modanisa/my_package.git: parsing go.mod:
module declares its path as:
gitlab.com/modanisa/my_package
but was required as:
gitlab.com/modanisa/my_package.git

To solve this error, we can use replace directive in our go.mod file. Therefore, we can add the following line our go.mod . In this way, we are getting rid of go.mod the parse error.

replace gitlab.com/modanisa/my_package => gitlab.com/modanisa/my_package.git v0.0.1

🔥 Integrating Private Package GitLab CI\CD Pipeline 🚀

It is so easy. We just add two lines of the code to our .gitlab.yml file. We recommend adding these lines your before_script

build:
stage: build
before_script:
- git config --global url.https://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}.insteadOf https://${CI_SERVER_HOST}
- export GOPRIVATE=${CI_SERVER_HOST}
script:
- go build

Thank you to the Modanisa Sherlock team (🔎) for the review. ❤️

--

--