fixes required for gitlab golang host url changes and nested subgroups
Working with GitLab subgroups and Golang is particularly difficult. When working with project dependencies that have nested sub-groups in GitLab our Golang dependency resolution becomes even trickier to handle.
Example:
- Original host:
gitlab.my.organization.com/my-group/project
- a private GitLab host - New host:
gitlab.com/my-organization/my-group/project
- An organization on GitLab's own site.- Note: that our project path has changed. We now have
my-organization/my-group/project
rather thanmy-group/project
.
- Note: that our project path has changed. We now have
Changes to the Golang files:
- All host URL changes need to be made for every import need to be modified.
- I have a note about this, using a complex
sed
replace will work!
- I have a note about this, using a complex
- Updating the
go.mod
andgo.sum
# go.mod
# Old
module gitlab.my.organization.com/my-group/project
# New
module gitlab.com/my-organization/my-group/project
# Requirements old
require (
gitlab.my.organization.com/my-group/project-2 v0.0.0-xyz-abc
)
# Requirements new
require (
gitlab.com/my-organization/my-group/project-2 v0.0.0-wvu-def # new hash required on dependent project updates too
)
# Retain a copy of the dependency in this location also
replace gitlab.com/my-organization/my-group/project-2 => gitlab.com/my-organization/my-group/project-2.git v0.0.0-wvu-def
Steps to resolve golang issues
- Remove
go.mod
andgo.sum
completely- Note: Technically we can remove
go.sum
and just remove all dependencies fromgo.mod
but keep the module and go version lines.
- Note: Technically we can remove
- Run
go mod tidy -e
, this will allow us to resolve dependencies but may fail on themock
portions, this will be resolved later.-e
states that for errors we simply continue without failing.
- Run
go generate ./...
to generate any mocks now that most dependencies are resolved. - Re-run
go mod tidy -e
to catch any mock related issues that were found in the previous step.
Side note - GitLab private repo access
If your project is in a private repository and has a dependency within another private repo you will need to update your GitLab project permissions as of GitLab version 15.9.
References:
- Allow access to your project with a job token
- GitLab access token setups
- GitLab subgroups and private repos
Resolving GitLab go get
behavior
Using .netrc
to override go get
behavior seems to be required for Golang to play nicely.
There are 2 changes, one for local development and one for GitLab pipelines.
Local development
# ~/.netrc
machine gitlab.com
login my-gitlab-username
password my-personal-access-token
Pipelines
The below can be generated in the CI pipeline as some form of script.
# ~/.netrc
machine gitlab.com
login gitlab-ci-token
password <my CI token here>