Makefile Guide
Understanding and Using Makefiles in DevOps Studio
DevOps Studio › Docs › Makefile Guide
What is a Makefile?
A Makefile is a build automation tool that uses a simple text file to define commands and their relationships. It's been a standard in software development for decades and is particularly useful for:
- Automating repetitive tasks (deploy, test, clean)
- Standardizing commands across different environments
- Documenting workflows in a single place
- Reducing errors by using predefined commands
Why Use Makefiles?
Instead of remembering complex commands like:
terraform init && terraform plan -var-file="environments/dev.tfvars" && terraform applyYou can simply run:
make apply ENV=devHow to Use Makefiles
Basic Usage
Makefiles are used with the make command:
make <target>Where <target> is the name of a command defined in the Makefile.
Viewing Available Commands
Every lab includes a Makefile with a help command:
make helpThis displays all available commands with descriptions.
Example Output
DevOps Studio - Lab 02: Kubernetes Platform
Available commands:
init Initialize Terraform
plan Create execution plan
apply Apply Terraform changes (deploy EKS cluster)
configure-kubectl Configure kubectl to connect to the cluster
deploy-app Deploy sample application
...Common Makefile Commands
Lab-Specific Commands
Each lab has its own Makefile with commands tailored to that lab's needs. Common patterns include:
Infrastructure Labs (Terraform)
make init # Initialize Terraform
make plan # Preview changes
make apply # Deploy infrastructure
make destroy # Remove infrastructure
make validate # Validate configurationKubernetes Labs
make configure-kubectl # Set up kubectl
make deploy-app # Deploy application
make install-helm-charts # Install Helm charts
make cluster-info # Show cluster detailsCI/CD Labs
make setup # Install dependencies
make test # Run tests
make build # Build application
make deploy # Deploy to environmentUnderstanding Makefile Syntax
Basic Structure
target: ## Description of what this does
command1
command2Example:
deploy: ## Deploy the application
kubectl apply -f manifests/
kubectl rollout status deployment/appVariables
Makefiles can use variables for configuration:
ENV ?= dev
IMAGE_TAG ?= latest
deploy:
helm upgrade --install app ./chart --set image.tag=$(IMAGE_TAG)Usage:
make deploy ENV=staging IMAGE_TAG=v1.2.3Dependencies
Commands can depend on other commands:
deploy: validate ## Deploy (runs validate first)
kubectl apply -f manifests/
validate: ## Validate configuration
terraform validateWhen you run make deploy, it automatically runs validate first.
Installation
macOS
# Using Homebrew
brew install make
# Verify installation
make --versionLinux
# Most distributions include make by default
make --version
# If not installed:
sudo apt-get install build-essential # Ubuntu/Debian
sudo yum install make # CentOS/RHELWindows
Option 1: WSL (Windows Subsystem for Linux)
# Install WSL, then use Linux commands aboveOption 2: Chocolatey
choco install makeOption 3: Git Bash
- Git for Windows includes make
- Use Git Bash terminal
Tips and Best Practices
1. Always Check Available Commands
make helpThis shows all available commands with descriptions.
2. Use Environment Variables
Many Makefiles support environment-specific configurations:
make apply ENV=dev # Development environment
make apply ENV=staging # Staging environment
make apply ENV=prod # Production environment3. Read the Makefile
If you're unsure what a command does, check the Makefile:
cat Makefile
# or
less Makefile4. Combine Commands
You can run multiple commands in sequence:
make init plan apply5. Use Tab Completion
Most shells support tab completion for make targets:
make <TAB><TAB> # Shows available commandsTroubleshooting
"make: command not found"
Solution: Install make (see Installation section above)
"No rule to make target 'xyz'"
Possible causes:
- Typo in command name
- Wrong directory (Makefile not in current directory)
- Command doesn't exist in this lab's Makefile
Solution:
# Check you're in the right directory
pwd
# Check available commands
make help
# Verify Makefile exists
ls -la Makefile"Permission denied"
Solution: Makefiles don't need execute permissions, but scripts they call might:
chmod +x scripts/*.shCommand Fails
Solution: Check the error message and the Makefile to understand what the command is doing:
# See what the command does
cat Makefile | grep -A 5 "target-name"
# Run commands manually to debugExamples from DevOps Studio Labs
Lab 01: Terraform Foundations
# Set up backend
make setup-backend
# Deploy infrastructure
make apply ENV=dev
# View outputs
make output
# Clean up
make destroy ENV=devLab 02: Kubernetes Platform
# Deploy EKS cluster
make apply
# Configure kubectl
make configure-kubectl
# Deploy application with Helm
make deploy-helm-chart
# Validate Helm charts
make lint-helm
make validate-helmLab 03: CI/CD Pipelines
# Set up environment
make setup
# Run tests
make test
# Build and deploy
make build
make deploy-stagingAdvanced Usage
Running Commands in Parallel
Some Makefiles support parallel execution:
make -j4 test # Run 4 test jobs in parallelDry Run
See what a command would do without executing:
make -n deploy # Dry run (no execution)Verbose Output
See all commands being executed:
make VERBOSE=1 deployLearning More
Official Documentation
Practice
The best way to learn Makefiles is to:
- Start with
make helpin any lab - Try the commands one by one
- Read the Makefile to understand what they do
- Modify commands to suit your needs
Summary
Key Takeaways:
- ✅ Makefiles simplify complex commands
- ✅ Use
make helpto see available commands - ✅ Commands are lab-specific
- ✅ Environment variables customize behavior
- ✅ Read Makefiles to understand what commands do
Next Steps:
- Navigate to any lab directory
- Run
make help - Try a few commands
- Read the Makefile to understand the structure
Questions? Check the lab-specific README or the Makefile comments for more details!