Test Go code with GitHub actions

suggest change

GitHub Actions allow you to run code in your repository, on GitHub servers, after every push.

This can be used for many things. This article discusses how to use it for running tests (as a replacement for hosted CI services like travis.ci).

A GitHub Action is defined by a Dockerfile in some GitHub repository. Since you can have multiple actions, you create a directory for each action.

An action is triggered by a workflow description in .github/main.workflow file in a repository.

Workflow is specific to each repository, actions can be shared.

A workflow running time (a sum of all actions in a workflow) is limited to 1 hr.

Workflow

Here's the simplest workflow:

workflow "run go test on push" {
  on = "push"
  resolves = ["test"]
}

action "test" {
  uses = "kjk/siser/action-go-test@master"
}

Explaining workflow section:

For a full list of options see documentation.

Explaining: action section:

For more information see documentation.

Actions can be re-used. "kjk/siser/action-go-test@master" specifies an action in https://github.com/kjk/siser/tree/master/action-go-test. @master says that master branch should be used.

For simplicity I put the action in the same repository that uses the action but I could, for example, create a separate repository just for actions and re-use them in multiple repos.

Action

Actions are Docker containers (describing docker is beyond the scope of this article).

Docker containers are infinitely flexible: you can run any logic in the container.

Our goal is to simply run go test on our code.

Dockerfile describes how to build a container (with docker build).

Our action-go-test/Dockerfile is the simplest possible:

FROM golang:latest

LABEL "name"="Go test"

LABEL "com.github.actions.name"="Go test"
LABEL "com.github.actions.description"="Run go test on code"
LABEL "com.github.actions.icon"="package"
LABEL "com.github.actions.color"="#E0EBF5"

COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

There is an existing, publicly available golang:latest docker image in official Docker hub repository of images.

You can use specific versions like golang:1.11.6. We ask for the latest.

LABEL statement are for just documentation.

A file action-go-test/entrypoint.sh describes the code that will be executed when we run the container with docker run. It runs go test:

#!/bin/bash

set -e

export GOPATH=/tmp
go test

To note:

Now in the commit list you'll see:

Yellow dot means: "action execution in progress".

Green check mark means: "action finished running successfully".

Red cross means: "action failed".

When you click on them, you'll see a short summary of checks:

Link details points to a page like https://github.com/kjk/siser/runs/81810467 showing a log of action execution.

Actions can do a lot more. See awesome actions list and actions in GitHub Marketplace for more inspiration.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents