I started learning Go about two to three months ago but didn’t see people use it in CI like GoCD. Since Go has different file structure layout than other programming language like Ruby, Python or Java , how to run test and build the app in GoCD is slightly different. Here are three different scenarios that might help people to setup their pipeline in GoCD with their Golang project. This examples will give you a basic idea on how to setup the pipeline like how to build the file structure, what to set on various GO related environment variables like GOPATH, GOBIN, etc.
1. A standalone app without any external/third parties library
In this example, the app only use Go built-in libraries. It doesn’t need “go get” to pull any external/third library. My sample app is on GitHub (https://github.com/barrowkwan/gocd_golang_hello.git)
Create a new pipeline in GoCD called “Standalone”
Check out the repos to src/github.com/barrowkwan/gocd_golang_hello. If you are familiar with Go directory structure, you will know why the repos need to check out in this location. If Go agent’s root folder is /var/lib/go-agent/pipelines, you will find the repos checkout at /var/lib/go-agent/pipelines/Standalone/src/github.com/barrowkwan/gocd_golang_hello.
Create a stage and a job in that stage, then create job that run the build script ( src/github.com/barrowkwan/gocd_golang_hello/build.sh )
When Go run this build script, it will use Go Agent’s root folder ( ie /var/lib/go-agent/pipelines/Standalone ) as the working directory. Since our repos has checkout under /var/lib/go-agent/pipelines/Standalone as src/github.com/barrowkwan/gocd_golang_hello, if we configure GOPATH to /var/lib/go-agent/pipelines/Standalone, “go” will be able to look up our app or any other external libraries.
In my build.sh script, I use ‘pwd’ to set up environment variable GOPATH. Unless you configure your job in GoCD to run your script from a different working directory; otherwise, the script will run from Go agent’s root folder ( ie /var/lib/go-agent/pipelines/Standalone ). If you run “go get <other external library>” in your script, it will download the library to /var/lib/go-agent/pipelines/Standalone/src. I also setup environment variable GOBIN so that “go get” or “go install” will install the binary under /var/lib/go-agent/pipelines/Standalone/bin instead of the default OS location ( eg /opt/go/bin )
When build.sh run “go build”, it will build my app with the version checkout by GoCD under src/github.com/barrowkwan/gocd_golang_hello.
** I will post two other scenarios on how to build your Golang application with GoCD soon