Setting up Travis CI for Go project

Wilson Tan
The Startup
Published in
3 min readJun 22, 2020

--

Note: This is part of a series that focuses on different CICD tools, where we will be looking at CircleCI next. If you are interested to know more about GitHub Actions, you can click here.

In this article, we are going to focus on two main things:

  1. What is Travis CI?
  2. How to setup Travis CI for Go project?

What is Travis CI?

Travis CI is a hosted continuous integration service that can be used on GitHub or Bitbucket repository.

As a continuous integration platform, Travis CI supports your development process by automatically building and testing code changes, providing immediate feedback on the success of the change.

Travis CI supports various programming languages and offers FREE plan for public repositories. Yes, charges are applicable for private repositories (you can refer here for their pricing)

A .travis.yml is used to specify the steps to be taken by Travis CI, this is to be added to your repository so that Travis CI will run build on the source code.

Setting up Travis CI

In this example, we will be using our Go project hosted in GitHub repository and of course, Travis CI does support other programming languages as well.

  1. Signup an account on Travis CI. Note: Do use https://travis-ci.com/ instead https://travis-ci.org/ as Travis CI have merged both public and private repositories on the same platform (https://travis-ci.com/) as of May 2018.
  2. Once signup, under settings, activate the GitHub Apps Integration for Travis CI which will link you back to GitHub page to install Travis app. We will then see a list of repositories that we have activated Travis CI.
  3. Now, we can add .travis.yml into the root directory and specify what kind of project and the version to be used.
language: go
go:
- “1.14”

4. Specify the branch for Travis CI to run the build. We can either make an inclusion or an exception. In this example, we are specifying an inclusion to run on master, staging and develop branch.

branches:
only:
- master
- staging
- develop

5. Add builds, stages, jobs and phases. Refer to https://docs.travis-ci.com/user/for-beginners/#builds-stages-jobs-and-phases for more explanation about what each is. Put it simply, it’s build > stage > job > phase where build is the topmost level of the hierarchy. Note that stages run sequentially whereas jobs run simultaneously.

jobs:
include:
- stage: CI
name: “Package & build”
script:
- echo “Hello World”
- go build .
- name: “Run vet”
script:
- go vet .
- name: “Run Linting”
install:
- go get -u golang.org/x/lint/golint
script:
- golint .

- name: “Unit Tests”
script:
- cd test && go test -v

- stage: Deploy
if: branch = master
name: “Dummy deploy stage”
script:
- echo “Deploy”

a. In the first code block, we specify the stage name as “CI” and also create the first job called Package & build.

b. In the second code block, we do not specify the stage anymore and Travis CI will create the job under the same stage as previous job.

c. Next, we will add in the install phase which runs before the script phase so that golint can be installed first.

d. The final job is Unit Tests. Note that all these jobs under the same stage run simultaneously.

e. We then have another stage called Deploy. This stage will not be executed if the previous stage fails. Notice that we have implemented if condition here to ensure that it only runs on master branch.

Screenshot of the build in Travis CI

6. Add Slack notification. To achieve this, you need to add Travis CI app on your Slack and you will be given a token for integration use. Add the following lines in your .travis.yml by replacing the Slack workspace name and token accordingly.

notifications:
slack: <slack workspace>:<token>

Voila! We are done!

Summary

  1. Travis CI provides continuous integration service to repository hosted on GitHub and Bitbucket.
  2. It is FREE to use for public repositories.
  3. It is very simple to define the build for Travis CI and their UI is rather intuitive.

Here is the full source code for .travis.yml:

.travis.yml source code

--

--