2 minutes
Golang Gitlab & SSH
Overview
This article will explain how to pull from a golang private repo on gitlab using ssh. Although this is specifically using gitlab it should work for other repository providers.
Requirements
- git
- go
- ssh
Setup
SSH
I won’t explain how to setup your keys, you can find that in other articles, here and on the web. You will however have to modify your ~/.ssh/config
[Edit ~/.ssh/config]
Host gitlab.example.com
Hostname gitlab.example.com
User git
Port 9022
The values for the above will be specific to your installation, the hostname and port especially. The user value “git” I found to be necessary even though I will later add that value to my ~/.gitconfig.
Git
There’s two ways to redirect to the ssh port on gitlab, one by directly adding to ~/.gitconfig, the second is by calling “git config [options]”. I’ll show both.
Git config
Call the following, and change the host to your own host.
git config --global --add url."ssh://git@gitlab.example.com/".insteadOf "https://gitlab.example.com/"
If you only want it to work with specific repos, remove the global and call that in the gitlab folder you cloned.
Editing .gitconfig
Directly edit ~/.gitconfig and add the following
[url "ssh://git@gitlab.example.com/"]
insteadOf = https://gitlab.example.com/
SSH Agent
You will need a ssh-agent setup for go to successfully pull private golang repos.
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_ed25519
Your key maybe id_rsa, so replace that in the “ssh-add”
GoPrivate
Go does have the ability to pull from private repos but looks like it is more centered around private repos with https endpoints.
If you have that kind of repo you would use an access token and specify GOPRIVATE=gitlab.example.com [command]
This article doesn’t cover this scenario, so you won’t need to specify GOPRIVATE
Conclusion
You should be all setup now, the only final thing to do is run:
go get -v
Edits
- [2024/08/16] Modified ~/.ssh/config example so Host and Hostname are the same