Deployment Automation with Claude Code
Your team just merged a big feature branch. The staging deploy went fine, but production requires a different Docker tag, three environment variables you can never remember, a database migration that has to run before the new code starts, and a Slack notification when it’s done. Last time somebody fat-fingered the image tag and rolled out last Tuesday’s build. The time before that, the migration ran after the deploy and users saw 500 errors for eleven minutes. Deployments should not require a checklist taped to the monitor.
What You’ll Walk Away With
Section titled “What You’ll Walk Away With”- A repeatable prompt workflow for generating deploy scripts, Dockerfiles, and GitHub Actions pipelines with Claude Code
- Copy-paste prompts that produce environment-specific configuration, blue-green rollout steps, and automated rollback triggers
- A
CLAUDE.md-driven deployment standard that Claude Code follows every time, so new engineers get the same quality output on day one
Generating a Production Dockerfile
Section titled “Generating a Production Dockerfile”Most Node.js Dockerfiles start as a copy-paste from Stack Overflow and never improve. Claude Code can generate an optimized, multi-stage build directly from your package.json and source layout.
Claude Code reads your project files, detects whether you use npm, yarn, or pnpm based on the lockfile present, and produces a Dockerfile tailored to your actual stack — not a generic template. If you have a prisma/ directory it will add the Prisma generate step; if you have a public/ folder it will copy static assets into the right place.
After Claude generates the file, verify it locally:
docker build -t myapp:test . && docker run --rm -p 3000:3000 myapp:testThen ask Claude Code to iterate:
The image is 340 MB. Reduce the size by switching the runtime stage to distroless or alpine with only the minimal packages needed.This loop — generate, build, measure, refine — is where Claude Code shines. Each iteration takes seconds because Claude already has the full context.
Scripting Multi-Environment Deploys
Section titled “Scripting Multi-Environment Deploys”Real projects have at least three environments: development, staging, production. The configuration differences between them are a constant source of bugs.
Claude Code produces a shell script that handles the full lifecycle. Crucially, it exits early on failure rather than plowing through and creating a half-deployed state. You can pipe the output of this script into Slack or your CI log.
For teams using infrastructure-as-code, ask Claude Code to generate the Terraform or Pulumi equivalent:
Convert this deploy.sh approach into a Pulumi TypeScript program that manages the same three environments as stacks. Use the aws and kubernetes providers.GitHub Actions Pipeline from Scratch
Section titled “GitHub Actions Pipeline from Scratch”Claude Code has deep knowledge of GitHub Actions syntax, including reusable workflows, environment protection rules, and concurrency controls. Rather than copying a YAML file from another repo and hoping it works, describe what you need.
Claude Code generates the complete workflow file. A few things to verify after generation:
- Check that the
permissionsblock includesid-token: writefor OIDC andpackages: writefor GHCR. - Confirm the
environment: productionblock is present on the production deploy job — this is what enables the manual approval gate in GitHub. - Verify that the concurrency group uses
${{ github.workflow }}-${{ github.ref }}so that PR deploys and main deploys do not block each other.
Blue-Green Deployments
Section titled “Blue-Green Deployments”Zero-downtime deployment sounds great until you have to implement the traffic switch yourself. Claude Code can generate the full blue-green orchestration script including health validation.
We run two Kubernetes deployments: myapp-blue and myapp-green. Write a blue-green deploy script that determines which deployment is currently receiving traffic from the myapp Service, deploys the new image to the inactive deployment, waits for all pods to pass readiness checks, switches the Service selector to the new deployment, then monitors error rates for 2 minutes via our Prometheus endpoint. If the error rate exceeds 1%, switch traffic back to the old deployment and exit 1.The key insight here is telling Claude Code about your monitoring endpoint. Without that, the script would just switch traffic and hope. With it, you get automatic rollback based on real data.
Rollback Procedures
Section titled “Rollback Procedures”Every deploy script should have a corresponding rollback. Claude Code can generate both at the same time.
This pattern — recording state before changing it — is something experienced engineers build instinctively but juniors often forget. Claude Code includes it when you ask for a rollback pair because the style guide in your CLAUDE.md (or the prompt itself) makes the requirement explicit.
Encoding Standards in CLAUDE.md
Section titled “Encoding Standards in CLAUDE.md”The real power of Claude Code for deployment is consistency. Add deployment standards to your project’s CLAUDE.md:
## Deployment Standards
- All Docker images must use multi-stage builds with a non-root user- Production deploys require a health check validation step- Every deploy script must have a corresponding rollback script- Database migrations run BEFORE the new code is deployed- All deploy scripts exit on first error (set -euo pipefail)- Image tags use git SHA, never 'latest' in production- Secrets come from environment variables, never hardcodedWith these rules in place, every prompt to Claude Code about deployment automatically inherits these constraints. A new engineer asking “create a deploy script for the payments service” gets the same quality output as a senior engineer would write.
When This Breaks
Section titled “When This Breaks”Claude generates a Dockerfile that works locally but fails in CI. This usually happens because the CI runner has a different architecture (ARM vs x86). Add --platform linux/amd64 to the build step or ask Claude Code to add a docker buildx multi-platform build.
The deploy script succeeds but the app crashes on startup. Claude Code cannot see runtime behavior. After generating a deploy script, always run it against a non-production environment first. Add a smoke test step (curl the health endpoint, check the exit code) and ask Claude to include it if it did not.
The GitHub Actions workflow burns too many minutes. Claude Code tends to be thorough, which sometimes means running tests sequentially when they could be parallel. Tell Claude your constraint: “Our GitHub Actions budget is limited, so use a matrix strategy and run lint/type-check/test as separate jobs that start simultaneously.”
Environment variables are missing in production. Claude Code generates references to environment variables but cannot verify they exist in your deployment target. After generating a deploy script, run grep -oP 'DOLLAR_SIGN{?\K[A-Z_]+' deploy.sh (replacing DOLLAR_SIGN with the actual character) to list all referenced variables and confirm each one is set.