github twitter instagram email
Easy Continuous Integration for Go
May 25, 2018
2 minutes read

This week I attended OpenStack Summit in Vancouver, while the overwhelming focal point was containers, CI/CD systems got some healthy attention. Among the conversation was where small projects should get their CI of which third party, self hosted, bash scripts, etc were all options.

The consensus was that hosting your own Jenkins for fun was a great option (blog post on self-hosted Jenkins in K8s coming soon) but for those who don’t want to maintain your own CI system there are several incredibly simple options that are free for open source projects.

I’m going to focus on Travis-CI as it is the dominate, free, hosted CI, and came highly recommended from the conversations at the summit.

I decided to test it on a project I created to learn Golang; a one time password (OTP) library which can be found here.

  1. Sign in to Travis with your GitHub account and add your repository
  2. Add a .travis.yml file to your project root with the following
language: go

# versions to test
go:
 - 1.9
 - release
 - tip

# get dependencies
before_install:
  - go get -t -v ./...

# run tests
script:
  - go test -v -race ./...

All your commits will now run CI test builds

The Travis dashboard has pregenerated badges you can add to your project to show your current build status.

Want Code Coverage?!

Adding code coverage is simple. I tested a few services and found CodeCov to be the best. Here’s how to get easy, simple code coverage monitoring

  1. Login to CodeCov with yout GitHub account
  2. Add your repository to your watch list
  3. Update your .travis.yml with these to
language: go

# versions to test
go:
 - 1.9
 - release
 - tip

# get dependencies
before_install:
  - go get -t -v ./...

# run tests
script:
  - go test -race -coverprofile=coverage.txt -covermode=atomic

# push results to CodeCov
after_success:
  - bash <(curl -s https://codecov.io/bash)

And there you have it, CI /w Code Coverage implemented in 15 minutes. You can see my OTP projects latest builds and code coverage (checkout that 100% coverage 😎).

Kepe in mind this is a very simple implementation and there are endless customizations that one can use. Here’s a much more expansive example .travis.yml build script.

If you go with the more expansive route I would recommend setting up some pre-commit hooks to run the linters and gocyclo to remove added frustration of having to go back and forth yak-shaving.


Back to posts