As the title suggests, in this tutorial we will look at setting up a CI solution for a Golang project using Jenkins pipelines. We recently worked on a pretty cool project for the Radiology department at the University Hospital of Basel, for which Go was a great fit. Since we have an in-house Jenkins CI solution, we also wanted to set it up for our project.
Go is pretty flexible and a very simple solution involves installing it globally on all the agents and ensuring
that environment variables are correctly set in the pipeline configuration (Jenkinsfile
). However, this
solution has the disadvantage that managing Golang versions will be a manual process. Jenkins comes with a Go plugin
which allows you to configure multiple Golang installations and we decided to go with this route to keep things clean.
First install the plugin via the "Manage Plugins" options in the "Manage Jenkins" menu and then
configure the go installations via the "Global Tool Configuration" option.
Thereafter, using the Golang installation in the pipeline configuration is as simple as adding the following tools section:
tools {
go 'go-1.12.1'
}
Note that since our project builds a binary with all resources embedded using Rice, we needed to install the binary which meant that we needed to configure the path to include the go packages location so that the rice binary could be found without requiring the full path:
stages {
stage('Test') {
steps {
// for rice
withEnv(["PATH+EXTRA=${HOME}/go/bin"]){
sh 'go get github.com/GeertJohan/go.rice/rice'
sh 'make build'
}
}
}
}
Note that the reason we wanted to use pipelines is to allow creating a database container to enable full integration testing. The complete Jenkinsfile can be found here.