GitHub Actions: lint and race tests on every push

Add a workflow that runs golangci-lint and go test -race with a Redis service container on every push and pull request.

Objective

Create a GitHub Actions workflow that automatically checks the Go backend whenever code is pushed to main or a pull request is opened or updated. This gives every change a repeatable lint, unit test, and Redis integration test check before it is merged.

By the end:

  • The workflow runs on pushes to main and on every pull request affecting the backend or workflow file.
  • CI uses the Go version declared in backend/go.mod.
  • Linting and race-enabled unit tests run as separate steps.
  • Redis integration tests run against a Redis 7 service container.
  • A deliberately broken test makes the GitHub Actions check fail.

Deliverables

  • .github/workflows/backend.yml: Create the backend CI workflow at the root of the zomato-prep repository. It must define the triggers, permissions, concurrency group, runner, Redis service, and all required steps.

    The workflow must include these checks:

    yaml
    - name: Lint
      uses: golangci/golangci-lint-action@v8
    
    - name: Unit tests
      run: go test -race -count=1 ./...
    
    - name: Redis integration tests
      run: go test -race -count=1 -tags redis ./internal/...

    Configure the lint action with version v2.5 and --build-tags redis. Check the action's release page if you choose a different v2 minor version.

Requirements

Constraints

  • Use GitHub Actions to create a workflow at .github/workflows/backend.yml in the repository root.
  • Trigger on:
    • Pushes to main.
    • Every pull_request.
  • Filter both triggers by these paths:
    • backend/**
    • .github/workflows/backend.yml
  • Use one job named backend on ubuntu-latest.
  • Define the job's default run directory as backend using defaults.run.working-directory. Individual shell steps should not need to repeat cd backend.
  • Add a Redis Service containers in CI at the job level with:
    • Image redis:7-alpine.
    • Port mapping 6379:6379.
    • Health check options containing --health-cmd "redis-cli ping", --health-interval 5s, --health-timeout 3s, and --health-retries 10.
  • Include these steps in order:
    1. actions/checkout@v4.
    2. actions/setup-go@v5, configured with go-version-file: backend/go.mod and cache-dependency-path: backend/go.sum.
    3. golangci/golangci-lint-action@v8, configured with version: v2.5, working-directory: backend, and args: --build-tags redis.
    4. Run unit tests with go test -race -count=1 ./....
    5. Run Redis integration tests with go test -race -count=1 -tags redis ./internal/....
  • Configure the Redis integration test step with the environment variable TEST_REDIS_ADDR: 127.0.0.1:6379.
  • The Redis service must be reachable through 127.0.0.1:6379. The job runs directly on the GitHub-hosted runner, not inside a container.
  • Configure workflow concurrency using a group unique to the Git ref and cancel-in-progress: true.
  • Set workflow permissions to contents: read.
  • Pin every action to a major version tag. Do not use @latest or @main.
  • Use go-version-file so the CI toolchain follows the version in backend/go.mod rather than drifting from the local development environment.
  • Do not add secrets. The workflow needs no credentials for these checks.
  • Ensure the YAML is valid and the workflow is recognized by GitHub Actions.
  • Push the repository to your personal GitHub account. A workflow file in a local repository will not run until it is pushed to GitHub.

Acceptance criteria

  • Successful run: After pushing the workflow, a run appears in the repository's Actions tab and completes successfully.

  • Separate checks: The run visibly contains distinct lint, unit test, and Redis integration test steps.

  • Race detection: Unit and Redis integration tests both run with -race.

  • Redis integration: The Redis tests run with the redis build tag and receive TEST_REDIS_ADDR.

  • Failure detection: Deliberately break a test on a branch and push it. The corresponding workflow run must fail. Fix the test and push again to get a green run.

  • Run inspection: From the repository root, use:

    bash
    git add .github
    git commit -m "Add backend CI workflow"
    git push
    
    gh run list --limit 3
    gh run watch

    You can also open the repository on GitHub and select the Actions tab to inspect workflow runs and individual step logs.

Finished Exercise 1.24?

Continuous integration

2 items

  • 1.23GitHub Actions for Gonot completed
  • 1.24GitHub Actions: lint and race tests on every pushnot completed

Capacity estimation and user load

Up next · Module 8 · 1 items · about 50 min

Start module 8

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. .github/workflows/backend.yml runs on push and pull_request for backend/**.
  2. It sets up the Go version from go.mod, runs golangci-lint, then go test -race ./....
  3. A Redis 7 service container with a health check is available at 127.0.0.1:6379.
  4. The run is green on main.

Commands

terminal
git push
terminal
gh run watch

This exercise has no Go test. The commands above are the check: when each one shows what the list says, the exercise is done.