Start learning free

LESSON 155 OF 173 · BACKEND: SHIPPING IT

Run the Tests on Every Push

Practice

Continuous integration (CI) means your tests run by themselves every time you push. On GitHub you ask for it with a workflow: a YAML file in the folder .github/workflows/, made of key: value lines nested by their indentation. The key on says which event starts it, and each job runs its steps from top to bottom on a fresh machine, the runner.

Here is a small workflow of another kind. It starts on every pull request and runs one command:

name: Hello
on: pull_request
jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - run: echo Hello from the runner

A step either uses a ready-made action, such as actions/checkout, which copies your repository onto the runner, or runs a command, as you would type it in a terminal. The runner starts with its own Go and nothing of yours: no code, no modules. So the order matters: copy the code, set up the Go the shop is built with, download the modules go.mod and go.sum pin with go mod download, then run the tests with go test ./.... The job stops at the first step that fails.

You write the workflow as the Go raw string workflow: everything between its backticks is the file, indented with spaces. The check reads it the way GitHub would, runs the job and must print:

Workflow: Tests
Starts on: push
Job test, on a fresh ubuntu-latest machine
Step 1: uses actions/checkout@v6
  copied the repository: main.go, main_test.go, go.mod, go.sum
Step 2: uses actions/setup-go@v6 with go-version 1.23
  Go 1.23 is ready
Step 3: run go mod download
  downloaded modernc.org/sqlite v1.34.5, the version go.mod and go.sum pin
Step 4: run go test ./...
  ok shop: 3 tests passed
The job passed: every push now runs the tests on Go 1.23.

Your task: the starting workflow already has its name and the job test on ubuntu-latest. Add the trigger on: push and four steps: check out the code with actions/checkout@v6, set up Go '1.23' with actions/setup-go@v6, download the modules with go mod download, and run the tests with go test ./....


Assignment
Follow the Coding Tutorial to write a GitHub Actions workflow that runs the shop's tests on Go 1.23 on every push.


Hint
Indentation is the structure, and it must be spaces, never tabs: the step items start 6 spaces in with a dash, with: sits 8 spaces in under its uses, and go-version 10. Write the version in quotes, '1.23', so YAML keeps it as text.


STEP 1 OF 7
    Ctrl + Enter to check