Building Go Apps with Gitlab CI

Gitlab CI is amazing. I love how well it works… when you finally get it setup correctly. With Go, some of the articles on the Web state that you need to install the entire Go environment. These articles are out of date. Getting everything working made the YAML file messy and convoluted.

With recent updates to Gitlab, just use the golang docker image from DockerHub (it’s an official image, so it’s safe). My gitlab-ci.yml file now looks like this:

  - build

  image: golang:1.7
  stage: build
    - mkdir -p /go/src/
    - ln -s /builds/typicalrunt/website.go /go/src/
    - go build

Some things to note:

  • Use the Docker image golang pin it to a specific version for reliable builds.
  • Your code is cloned into the container in a directory called /builds/$GROUP/$PROJECT. Substitute $GROUP and $PROJECT for values specific to you.
  • Since the $GOPATH it set to /go, you need to symlink in your code into the proper directory. You may have to create the directory structure first (hence my use of mkdir).
  • Run go build with your package name and you’re done.

Tags: programming