bashintermediate
Makefile for DevOps Automation
Makefile with common DevOps targets for building, testing, deploying, and managing Docker containers.
bashPress ⌘/Ctrl + Shift + C to copy
# Makefile
.PHONY: help build test deploy clean logs
APP_NAME := webapp
DOCKER_IMAGE := $(APP_NAME):latest
DOCKER_REGISTRY := ghcr.io/myorg
help: ## Show available targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | \
awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
install: ## Install dependencies
npm ci
lint: ## Run linter
npm run lint
test: ## Run tests with coverage
npm test -- --coverage
build: ## Build Docker image
docker build -t $(DOCKER_IMAGE) .
echo "Built $(DOCKER_IMAGE)"
push: build ## Push image to registry
docker tag $(DOCKER_IMAGE) $(DOCKER_REGISTRY)/$(DOCKER_IMAGE)
docker push $(DOCKER_REGISTRY)/$(DOCKER_IMAGE)
dev: ## Start development environment
docker-compose -f docker-compose.dev.yml up -d
down: ## Stop all containers
docker-compose down -v
logs: ## Tail application logs
docker-compose logs -f --tail=100 app
db-shell: ## Open database shell
docker-compose exec db psql -U appuser appdb
clean: ## Remove build artifacts and containers
docker-compose down -v --rmi local
rm -rf dist/ node_modules/.cache coverage/
deploy-staging: push ## Deploy to staging
ssh staging-web 'cd /opt/app && docker pull $(DOCKER_REGISTRY)/$(DOCKER_IMAGE) && docker-compose up -d'
deploy-prod: push ## Deploy to production (requires confirmation)
@read -p "Deploy to PRODUCTION? [y/N] " confirm && [[ $$confirm == y ]] || exit 1
ssh prod-web 'cd /opt/app && docker pull $(DOCKER_REGISTRY)/$(DOCKER_IMAGE) && docker-compose up -d'Use Cases
- Standardizing developer commands across teams
- Simplifying Docker build and deploy workflows
- Self-documenting project automation
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
bashintermediate
Log Rotation Management Script
Automate log rotation with compression, retention policies, and disk space monitoring in Bash.
Best for: Preventing disk space exhaustion from logs
#bash#devops
bashintermediate
GitHub Actions CI/CD Pipeline
Complete GitHub Actions workflow with test, build, and deploy stages for a Node.js application.
Best for: Automated testing and deployment on push
#github-actions#ci-cd
bashbeginner
Linux Cron Job Setup Examples
Common crontab entries for database backups, log cleanup, health checks, and certificate renewal.
Best for: Automated database backups
#cron#linux
bashadvanced
Ansible Playbook for Server Setup
Ansible playbook to provision a web server with Nginx, Node.js, and firewall configuration.
Best for: Automated server provisioning across environments
#ansible#automation