Makefile · Lesson 5 of 7 · 4 min read
Designing a DevOps Makefile
A production-quality example
# ---- Settings -------------------------------------------------------------
SHELL := bash
.SHELLFLAGS := -eu -o pipefail -c
.DEFAULT_GOAL := help
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables --no-builtin-rules
APP := orders
REGION ?= ap-south-1
REGISTRY ?= 123456789012.dkr.ecr.$(REGION).amazonaws.com
GIT_SHA := $(shell git rev-parse --short HEAD)
TAG ?= $(GIT_SHA)
IMAGE := $(REGISTRY)/$(APP)
ENV ?=
TOOLS_IMG := ghcr.io/acme/devtools:1.8.0 # pinned toolchain image
.PHONY: help dev lint test build docker-build docker-push scan deploy clean print-%
help: ## Show this help
@grep -hE '^[a-zA-Z0-9_%-]+:.*?## ' $(MAKEFILE_LIST) | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-16s\033[0m %s\n", $$1, $$2}'
dev: ## Start the local stack (compose)
docker compose up -d --build --wait
lint: ## Lint code, Dockerfile and manifests
golangci-lint run ./...
hadolint Dockerfile
kustomize build deploy/overlays/staging | kubeconform -strict -summary
test: ## Unit tests with race detector
go test -race -cover ./...
build: bin/$(APP) ## Build the binary
bin/$(APP): $(shell find . -name '*.go' -not -path './vendor/*') go.mod go.sum
CGO_ENABLED=0 go build -trimpath -o $@ ./cmd/$(APP)
docker-build: ## Build the container image
docker build --pull -t $(IMAGE):$(TAG) .
docker-push: docker-build ## Push the image to ECR
aws ecr get-login-password --region $(REGION) | docker login --username AWS --password-stdin $(REGISTRY)
docker push $(IMAGE):$(TAG)
scan: ## Scan the image for critical CVEs
trivy image --exit-code 1 --severity CRITICAL --ignore-unfixed $(IMAGE):$(TAG)
deploy: guard-ENV ## Promote TAG to ENV via GitOps (ENV=staging|prod)
@if [ "$(ENV)" = "prod" ]; then read -p "Deploy $(TAG) to PROD? [y/N] " a && [ "$$a" = y ]; fi
./scripts/gitops-promote.sh $(APP) $(ENV) $(TAG)
clean: ## Remove build outputs
rm -rf bin dist coverage.out
guard-%: # Fail if a variable is empty: make guard-ENV
@if [ -z '$($*)' ]; then echo "ERROR: $* is required" >&2; exit 1; fi
print-%: ## Print a variable: make print-IMAGE
@echo '$*=$($*)'
What to point out in an interview:
- Self-documenting —
## commentsplus ahelpdefault goal: new engineers typemakeand see every task. Great for onboarding. - Strict shell — bash with
-eu -o pipefail; undefined-variable warnings. - Real file target for the binary (incremental), phony targets for tasks.
- Overridable defaults with
?=so CI passesTAG=$GIT_COMMITand developers don't have to. - Guards —
deployrefuses to run withoutENV, never defaults to production, and asks for confirmation for prod (CI passes a non-interactive flag or uses a separate pipeline path). - No secrets — credentials come from the environment/IAM at runtime (
aws ecr get-login-passworduses the caller's role). - Debuggability —
make print-IMAGE.
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
Monorepos
Options: a top-level Makefile that loops or recurses into services/* with $(MAKE) -C, pattern targets (test-%), or generated targets with $(foreach ...). Combine with "changed services only" logic in CI (git diff --name-only origin/main...HEAD) to keep builds fast. At large scale, purpose-built tools (Bazel, Nx, Turborepo, Pants) replace make.