Go/Travis CI
From charlesreid1
Contents
Basic Example
What this example covers
This page covers the setup of a simple Go pipeline on Travis CI:
- formats new code to meet standards
- test new additions to the code base (possibly using multiple versions of Go)
- prevent breaking changes from being merged to master
- ask for code reviews
Before you begin
Before starting, you'll need to set up a Travis account and integrate it with Github. See https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/
A simple Go test
Let's start with a simple Go test that we can use to illustrate how to set up Travis CI.
We begin with main.go
, which defines a Calculate()
method we can write a test for.
// main.go package main import ( "fmt" ) func calculate(x int) int { return x + 2 } func main() { fmt.Println("This is a test") }
Next we write a unit test for our Go function. This is a simple barebones test, not following best practices.
// main_test.go package main import "testing" func test_calculation(t *testing.T) { if calculate(2) != 4 { t.Fatal("Test of calculate method failed") } }
Writing a code-linting shell script
To have Travis CI automatically format our Go code, we can write a simple shell script to run the gofmt
utility:
#!/bin/bash # # .travis.gofmt.sh if [ -n "$(gofmt -l .)" ]; then echo "Go code is not formatted:" gofmt -d . exit 1 fi
dot travis yml
barebones skeleton .travis.yml for Go
--- # .travis.yml language: go sudo: false go: - "1.8" - "1.9" - "1.10" - "1.11" - tip
add go test and shell script to .travis.yml
language: go sudo: false go: - "1.8" - "1.9" - "1.10" - "1.11" - tip script: - ./.travis.gofmt.sh - go test -v -race $(go list ./... | grep -v vendor)
Resources
Travis CI integration pipeline: https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/
Flags
Go notes on Go
Go/Strings · Go/Arrays · Go/Slices
Go/Lists · Go/Maps · Go/Stacks · Go/Queues
Go/Naming Conventions · Go/Design Patterns
Go/Timing · Go/Profiling · Go/Benchmarking
Go/Tests · Go/Travis CI · Go/Makefiles
our vim + go setup uses custom solarized colors for Golang
|