Backend CI workflow
You build the backend CI workflow that tests against MySQL and Redis, then publishes the API and worker images to GHCR with the commit SHA.
Ticket
Build .github/workflows/backend.yml as a GitHub Actions workflow that validates the Go backend and publishes separate Docker images for the API and worker. The workflow must run tests against MySQL and Redis, apply database migrations, lint the code, and execute race-enabled tests before publishing either image.
Your existing workflow already includes the Week 6 govulncheck step. Keep that step when updating the workflow. Do not replace the existing security checks with the new CI jobs.
The image publishing job must run only after the test job succeeds and only for pushes to main. It publishes both images to GitHub Container Registry (GHCR), tagged with the commit SHA. The next deployment module uses these images to run the application on a server.
Acceptance criteria
.github/workflows/backend.ymltriggers on bothpushandpull_requestevents.- The job that runs the race-enabled tests defines MySQL and Redis as service containers. The MySQL image starts with
mysql:, and the Redis image starts withredis:. Both services have mapped ports and healthcheck options containing--health-cmd. - The job that runs the tests sets
TEST_REDIS_ADDRto a value starting with127.0.0.1:andTEST_MYSQL_DSNto a value containing127.0.0.1. - A job with service containers runs Go tests using
go testwith the-raceflag. The same job also runs a linter throughgolangci-lint,make lint, orgolangci/golangci-lint-action. - Keep the existing Week 6
govulncheckstep in.github/workflows/backend.yml. The Week 6 workflow testTestW6B6_CIWorkflowRunsGovulncheckmust continue to pass. - The image publishing job depends on the test job through
needs. Itsifcondition containsrefs/heads/main. - The API and worker images are both pushed to GHCR using
docker/build-push-action. Their image names areghcr.io/yashdagar/zomato-apiandghcr.io/yashdagar/zomato-worker. Both use${{ github.sha }}as their tag. Neither image receives alatesttag. At least two build-and-push steps must havepush: true. - Both image build steps use
backendas their build context. The API build usesbackend/Dockerfile.api, and the worker build usesbackend/Dockerfile.worker. - Each image publishing job logs in using
docker/login-actionwithregistry: ghcr.ioand a password containing${{ secrets.GITHUB_TOKEN }}. The job or workflow haspackages: writepermission. - Every GitHub Action referenced through
usesis pinned to a version tag. Do not use floating references such as@mainor@master. - The workflow does not contain literal credentials, real account passwords, or personal access tokens.
Constraints
- Keep both
pushandpull_requestas workflow triggers. - Keep the Week 6
govulncheckstep. - Do not use floating GitHub Action references.
- Do not put real credentials or personal access tokens in the workflow.
- Do not modify the provided tests.
Design direction
Keep the workflow in .github/workflows/backend.yml. Separate validation from image publishing into two jobs.
Use a test job for the backend checks. GitHub Actions service containers give this job disposable MySQL and Redis instances. Configure their healthchecks so the test runner does not start using dependencies that are still initializing.
The runner accesses mapped service ports through 127.0.0.1. Configure the MySQL service with MYSQL_ROOT_PASSWORD and MYSQL_DATABASE=zomato. Set the test environment variables to the expected connection addresses. Install or invoke golang-migrate, then apply the existing migrations before running integration tests.
Run linting and race-enabled tests in the test job. Keep the Redis integration build tag when invoking go test, so the Redis-backed tests are included. Preserve the existing govulncheck step from Week 6 rather than removing it while reorganizing the workflow.
Use a separate publishing job with needs referencing the test job. This creates a test-gated build: a failed test job prevents image publishing. Combine the push event condition with the explicit main branch reference.
The publishing job should have contents: read and packages: write permissions. Use GitHub's built-in secrets.GITHUB_TOKEN through environment secrets rather than creating a personal access token. Configure Docker Buildx before the builds and authenticate to the container registry.
Build the API and worker independently. Use the same backend build context but select the appropriate Dockerfile for each image. Enable GitHub Actions build caching with cache-from and cache-to, both using type=gha.
Use the commit SHA as the image SHA tag. It identifies the exact source revision used to produce an image, unlike a mutable latest tag. Keep both image builds behind the same successful test job.
Any workflow layout that meets the contract passes. Keep changes confined to the workflow file. Do not modify application source code, Dockerfiles, or database migrations.
How to check your work
Run the repository tests from the repository root:
set -e
cd backend
make test W=7B2The repository tests inspect the workflow YAML for triggers, service containers, connection settings, linting, race tests, job dependencies, image builds, registry authentication, version-pinned actions, and credentials. The Week 6 test also checks that govulncheck remains configured.
You can optionally run actionlint to check workflow syntax and common GitHub Actions configuration mistakes:
docker run --rm -v "$PWD:/repo" -w /repo rhysd/actionlint:latestRead the first failing assertion to identify which part of the workflow contract needs attention before investigating later failures.
You do not need to push an image or trigger an actual GitHub Actions run to complete this exercise.
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.
Production containers and CI
4 items
- 7.1Multi-stage Dockerfiles for Gonot completed
- 7.2Production container stacknot completed
- 7.3Gated CI image publishingnot completed
- 7.4Backend CI workflownot completed
Up next · Module 2 · 3 items · about 3 h 30 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Workflow triggers on push and pull request.
- Test job provisions health-checked MySQL and Redis services.
- Migrations, linting, govulncheck, and race-enabled tests are configured.
- Image publishing depends on successful tests and runs only on pushes to main.
- API and worker images are pushed to GHCR with commit SHA tags.
- All W7B2 tests pass, and optional actionlint checks succeed.
Commands
cd backend && make test W=7B2docker run --rm -v "$PWD:/repo" -w /repo rhysd/actionlint:latestTests
TestW7B2_WorkflowRunsOnPushAndPullRequestinbackend/internal/deploy/w7b2_test.goTestW7B2_TestJobRunsMysqlAndRedisAsHealthCheckedServicesinbackend/internal/deploy/w7b2_test.goTestW7B2_TestJobPointsTestsAtTheServicesinbackend/internal/deploy/w7b2_test.goTestW7B2_TestJobLintsAndRunsRaceTestsinbackend/internal/deploy/w7b2_test.goTestW7B2_ImagesAreBuiltOnlyAfterTestsPassOnMaininbackend/internal/deploy/w7b2_test.goTestW7B2_BothImagesArePushedToGhcrTaggedWithTheCommitShainbackend/internal/deploy/w7b2_test.goTestW7B2_BuildStepsUseTheBackendContextAndBothDockerfilesinbackend/internal/deploy/w7b2_test.goTestW7B2_BuildJobCanLogInToGhcrWithTheBuiltInTokeninbackend/internal/deploy/w7b2_test.goTestW7B2_EveryActionIsPinnedToAVersioninbackend/internal/deploy/w7b2_test.goTestW7B2_WorkflowHasNoLiteralCredentialsinbackend/internal/deploy/w7b2_test.go