Recently, we upgraded to using go modules as opposed to using dep for our package management. Today I want to show you a quick tutorial on how to get things set up if you have to pull in private repos with go modules. This is for local development, although, it can be applied to your CI/CD environment like bitbucket pipelines or Jenkins.
- Add a rule to your git config to use credentials. ( ssh or username/password)
- Set your GOPRIVATE env var
And that’s it.. you’re done and ready to use go modules for your private repos.
1 – Adding rule to git config:
The first rule tells the git client to use ssh instead of https, this will use your ssh credentials to pull repos.
The second rule tells the client to use username and password.
Only one needs to be set, not both, depending on how you are configured to access the repositories.
git config --global url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"
export BITBUCKET_USERNAME=BOB
export BITBUCKET_APP_PASSWORD=134
git config --global url."https://${BITBUCKET_USERNAME}:${BITBUCKET_APP_PASSWORD}@bitbucket.org".insteadOf "https://bitbucket.org/"
In order to create an app password go to your user account settings and follow the screenshot below .
2 – Setting GOPRIVATE
export GOPRIVATE=bitbucket.org/{yourworkspacename}
side note- if you’re using Goland as your IDE, I found that if you set the GOPRIVATE in the IDE, it does not apply to the terminals running in the IDE, so you need to explicitly set it in the terminal, or if you want it as a global in all your terminals, then set it in your .bash_profile file. ( if using osx )
This has save me so many times! I reference it to everyone having this problem! Thanks Matt! Lifesaver!