Importing a private Go module hosted on an on-premises Bitbucket server.
My current company uses an on-premises Bitbucket to host a private Go module. We're having some difficulty setting up local development and CI/CD. I thought I'd share how we do it here.
This article assumes that you have a bitbucket server which accessible via on bitbucket.server and is accessible using either SSH or HTTPS.
Setup the module we want to import
First we need to setup the module that we want to import correctly in the Bitbucket. If you don’t already have the repo/module you need to initialize it with the full domain and path for the repository that looked like this.
# assuming you want to create a git repo at bitbucket.server/myproject/mymodulego mod init bitbucket.server/myproject/mymoduleIf you already have the repo, you need to make sure that the module name in go.mod file is in the correct format, like bitbucket.server/myproject/mymodule. You also have an options to add version tags in git. The tag is optional, but it’s good to have. The tags will be used to specify the version of the module and will be noted in the go.mod file. Here’s the example on how to add it.
git tag v0.0.1git add .git push --tagsConfiguring URL Rewriting
To make this works with either SSH or HTTPS when using the same clean URL in the bitbucket, we need to configure some URL rewriting. This is different between ssh and https.
Using SSH
To tell git to use SSH instead of HTTP for bitbucket.server access. The /scm/ part is important, since Bitbucket includes this URL part for HTTPS access but not for SSH.
Usually, the bitbucket is configured to listen to some custom port for the ssh. In this case, you need to tell your ssh client to use the custom port for the bitbucket.server . Edit the ~/.ssh/config file (create new if it doesn’t exists).
Host bitbucket.serverHostName bitbucket.serverPort <PORT>Using HTTPS
Here, we need to remote the /scm/ part to make it refer to the same bitbucket server as the ssh counterpart.
git config --global url."https://bitbucket.server/scm/".insteadOf "https://bitbucket.server/"After that, we need to configure the authentication. The recommended way is by using git credential helper.
- Run git config —global credential.helper store (Linux) or ensure your system helper is active (Win/Mac).
- Manually git clone any private repo from your Bitbucket server once using the terminal.
- Enter your Username and Personal Access Token (PAT) (preferred over password).
- Git saves this. Now go get can read these credentials automatically.
Set GOPRIVATE
Next, Inform the golang dependency management that any package from bitbucket.server is a private one
export GOPRIVATE=my-bitbucket.server
# Optionally, you can add that export command to your shell's rc filesecho 'export GOPRIVATE=my-bitbucket.server' >> ~/.bash_profileTest and try getting the private module
Test by adding/importing the module in the project you need it from.
import ( ... "bitbucket.server/myproject/mymodule")or
Attribution
I solve the problem by reading this article https://www.reddit.com/r/golang/comments/e9q2jr/private_repositories_with_onpremise_bitbucket/ and thought to write my own version of the article, and change/add things here and there. Thanks and credits to the original author: nanotimezone