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
mainand 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 thezomato-preprepository. 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.5and--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.ymlin the repository root. - Trigger on:
- Pushes to
main. - Every
pull_request.
- Pushes to
- Filter both triggers by these paths:
backend/**.github/workflows/backend.yml
- Use one job named
backendonubuntu-latest. - Define the job's default run directory as
backendusingdefaults.run.working-directory. Individual shell steps should not need to repeatcd 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.
- Image
- Include these steps in order:
actions/checkout@v4.actions/setup-go@v5, configured withgo-version-file: backend/go.modandcache-dependency-path: backend/go.sum.golangci/golangci-lint-action@v8, configured withversion: v2.5,working-directory: backend, andargs: --build-tags redis.- Run unit tests with
go test -race -count=1 ./.... - 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
@latestor@main. - Use
go-version-fileso the CI toolchain follows the version inbackend/go.modrather 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
redisbuild tag and receiveTEST_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 watchYou can also open the repository on GitHub and select the Actions tab to inspect workflow runs and individual step logs.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Continuous integration
2 items
- 1.23GitHub Actions for Gonot completed
- 1.24GitHub Actions: lint and race tests on every pushnot completed
Up next · Module 8 · 1 items · about 50 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
.github/workflows/backend.ymlruns on push and pull_request forbackend/**.- It sets up the Go version from
go.mod, runsgolangci-lint, thengo test -race ./.... - A Redis 7 service container with a health check is available at
127.0.0.1:6379. - The run is green on main.
Commands
git pushgh run watchThis exercise has no Go test. The commands above are the check: when each one shows what the list says, the exercise is done.