Go (Language) with GoCD

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”

Screen Shot 2016-08-09 at 10.56.12 PMCheck 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.

Screen Shot 2016-08-09 at 10.56.38 PM

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 )

Screen Shot 2016-08-09 at 10.56.47 PM

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.

Screen Shot 2016-08-09 at 10.56.52 PM

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


This entry was posted in GoCD, Golang. Bookmark the permalink.

2 Responses to Go (Language) with GoCD

  1. t says:


    Thank you for writing this article. I was tasked to build GoCD pipeline(s) for Golang executable(s) and on Windows platform (gulp!). What you wrote really makes sense, it got me through a few hurdles. My next challenge is to create a GoCD deployment pipeline that will install Golang Windows service.

  2. Pingback: Go (Language – Golang) with GoCD – Part Two | Barrow Kwan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s