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.yml triggers on both push and pull_request events.
  • 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 with redis:. Both services have mapped ports and healthcheck options containing --health-cmd.
  • The job that runs the tests sets TEST_REDIS_ADDR to a value starting with 127.0.0.1: and TEST_MYSQL_DSN to a value containing 127.0.0.1.
  • A job with service containers runs Go tests using go test with the -race flag. The same job also runs a linter through golangci-lint, make lint, or golangci/golangci-lint-action.
  • Keep the existing Week 6 govulncheck step in .github/workflows/backend.yml. The Week 6 workflow test TestW6B6_CIWorkflowRunsGovulncheck must continue to pass.
  • The image publishing job depends on the test job through needs. Its if condition contains refs/heads/main.
  • The API and worker images are both pushed to GHCR using docker/build-push-action. Their image names are ghcr.io/yashdagar/zomato-api and ghcr.io/yashdagar/zomato-worker. Both use ${{ github.sha }} as their tag. Neither image receives a latest tag. At least two build-and-push steps must have push: true.
  • Both image build steps use backend as their build context. The API build uses backend/Dockerfile.api, and the worker build uses backend/Dockerfile.worker.
  • Each image publishing job logs in using docker/login-action with registry: ghcr.io and a password containing ${{ secrets.GITHUB_TOKEN }}. The job or workflow has packages: write permission.
  • Every GitHub Action referenced through uses is pinned to a version tag. Do not use floating references such as @main or @master.
  • The workflow does not contain literal credentials, real account passwords, or personal access tokens.

Constraints

  • Keep both push and pull_request as workflow triggers.
  • Keep the Week 6 govulncheck step.
  • 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:

bash
set -e
cd backend
make test W=7B2

The 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:

bash
docker run --rm -v "$PWD:/repo" -w /repo rhysd/actionlint:latest

Read 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.

Finished Exercise 7.4?

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

Single-server production deployment

Up next · Module 2 · 3 items · about 3 h 30 min

Start module 2

Implementation

Run locally

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

  1. Workflow triggers on push and pull request.
  2. Test job provisions health-checked MySQL and Redis services.
  3. Migrations, linting, govulncheck, and race-enabled tests are configured.
  4. Image publishing depends on successful tests and runs only on pushes to main.
  5. API and worker images are pushed to GHCR with commit SHA tags.
  6. All W7B2 tests pass, and optional actionlint checks succeed.

Commands

terminal
cd backend && make test W=7B2
terminal
docker run --rm -v "$PWD:/repo" -w /repo rhysd/actionlint:latest

Tests

  • TestW7B2_WorkflowRunsOnPushAndPullRequest in backend/internal/deploy/w7b2_test.go
  • TestW7B2_TestJobRunsMysqlAndRedisAsHealthCheckedServices in backend/internal/deploy/w7b2_test.go
  • TestW7B2_TestJobPointsTestsAtTheServices in backend/internal/deploy/w7b2_test.go
  • TestW7B2_TestJobLintsAndRunsRaceTests in backend/internal/deploy/w7b2_test.go
  • TestW7B2_ImagesAreBuiltOnlyAfterTestsPassOnMain in backend/internal/deploy/w7b2_test.go
  • TestW7B2_BothImagesArePushedToGhcrTaggedWithTheCommitSha in backend/internal/deploy/w7b2_test.go
  • TestW7B2_BuildStepsUseTheBackendContextAndBothDockerfiles in backend/internal/deploy/w7b2_test.go
  • TestW7B2_BuildJobCanLogInToGhcrWithTheBuiltInToken in backend/internal/deploy/w7b2_test.go
  • TestW7B2_EveryActionIsPinnedToAVersion in backend/internal/deploy/w7b2_test.go
  • TestW7B2_WorkflowHasNoLiteralCredentials in backend/internal/deploy/w7b2_test.go