Go’s dependency management is based solely on git repos. Most of the libraries have publicly available repos from which you can clone the source and then build your project.
In my case, most of the Go that I’m writing ends up in private repos in Bitbucket. Currently, I’ve got a program that I am writing that is dependent on a library that I have written that is hosted as a private repo in Bitbucket. I wanted to setup a Bitbucket Pipeline to build and then run the unit and integration tests when a PR was submitted.
Initially, I had a bit of a hard time figuring out how to set up the pipeline to work correctly and posted this Stack Overflow question. Not being one to give up, I kept at it and stumbled upon some Atlassian documentation that gave me what I needed to get it sorted out.
The key (no pun intended) ended up being the distribution of SSH keys between the repos and some additional go specific environment variables in the pipeline code.
image: golang:1.16 pipelines: pull-requests: '**': - step: name: Build and test script: - /bin/bash build/bitbucket-pipelines.sh
The pipeline yaml is straightforward. I’m using a golang builder image, defining a pull-request trigger for any branch, and specifying a shell script that contains the details for buiding and running my tests. The script is in the build directory which is in the base of my repository.
# Export the following go specific env vars to configure the tool chain so that I can clone # repos from my private Bitbucket project. export GONOPROXY="bitbucket.org/my-project" export GONOSUMDB="bitbucket.org/my-project" export GOPRIVATE="bitbucket.org/my-project" # Configure git to force using ssh instead of https to clone the repos hosted in # my Bitbucket project git config --global url."email@example.com:my-project".insteadof "https://bitbucket.org/my-project" CGO_ENABLED=0 go build CGO_ENABLED=0 go test -count=1 -tags=unit ./... CGO_ENABLED=0 go test -count=1 -tags=integration ./integration_tests/
SSH Key Configuration
With this basic pipeline code you then need to create a distribute ssh keys between the projects
Create the Pipeline SSH key pair in the Repo where you are running the pipeline
- In the repo where you are running the build, and composing the library, go to Repository settings. Then under PIPELINES, click on SSH keys
- Click Generate keys to create a new key pair.
- Copy the Public key to your clipboard. This public key will be added to all of dependent repos to enable us to clone and build with their source code.
For each of the dependency repos do the following:
- Go to Repository Settings and under GENERAL click on Access keys
- Click Add key and enter a Label and then paste the public key in the Key field.
- Then click Add SSH key
Once the public keys have been distributed to the dependent repositories your pipeline will have the permissions required to clone during the build.