From charlesreid1

 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:


=Basics=
=Basic Example=


==What this page covers==
==What this example covers==


This page covers the setup of a simple Go pipeline on Travis CI:
This page covers the setup of a simple Go pipeline on Travis CI:
Line 11: Line 11:
* ask for code reviews
* ask for code reviews


==before you begin==
==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/
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 <code>main.go</code>, which defines a <code>Calculate()</code> method we can write a test for.
<pre>
// main.go
package main
import (
"fmt"
)
func calculate(x int) int {
return x + 2
}
func main() {
fmt.Println("This is a test")
}
</pre>
Next we write a unit test for our Go function. This is a simple barebones test, not following best practices.
<pre>
// main_test.go
package main
import "testing"
func test_calculation(t *testing.T) {
if calculate(2) != 4 {
t.Fatal("Test of calculate method failed")
}
}
</pre>
==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 <code>gofmt</code> utility:
<pre>
#!/bin/bash
#
# .travis.gofmt.sh
if [ -n "$(gofmt -l .)" ]; then
  echo "Go code is not formatted:"
  gofmt -d .
  exit 1
fi
</pre>


==dot travis yml==
==dot travis yml==
===barebones skeleton .travis.yml for Go===


<pre>
<pre>
Line 30: Line 89:
   - "1.11"
   - "1.11"
   - tip
   - tip
</pre>
===add go test and shell script to .travis.yml===
<pre>
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)
</pre>
</pre>


Line 36: Line 114:
Travis CI integration pipeline: https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/
Travis CI integration pipeline: https://tutorialedge.net/golang/building-ci-cd-pipeline-go-projects/


==Flags==
=Flags=


{{GoFlag}}
{{GoFlag}}

Latest revision as of 02:27, 13 December 2018

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