Claude Skills Guide

Claude Code for Blue-Green Deployment Workflow

Blue-green deployment is a release strategy that maintains two identical production environments—called “blue” (current live version) and “green” (new version)—allowing you to deploy with zero downtime and instant rollback capabilities. This guide shows you how to use Claude Code to implement, automate, and manage blue-green deployment workflows effectively.

Understanding Blue-Green Deployment Fundamentals

In a blue-green deployment, you run two identical production environments. At any given time, one environment (let’s say “blue”) serves live traffic while the “green” environment remains idle or runs the new version. When you’re ready to release, you switch traffic from blue to green. If issues arise, you can immediately roll back by switching back to blue.

This approach provides several key benefits: instant rollback capability, zero downtime deployments, and the ability to test in production-like environments before releasing to users. Claude Code can help you implement this pattern across various platforms and tooling.

Setting Up Your First Blue-Green Workflow

The first step is establishing two identical environments. Claude Code can generate the necessary infrastructure code and configuration files for your chosen platform.

Infrastructure Configuration

For Kubernetes-based deployments, you need to create identical deployments with different names:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-blue
  labels:
    app: myapp
    version: blue
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      version: blue
  template:
    metadata:
      labels:
        app: myapp
        version: blue
    spec:
      containers:
      - name: myapp
        image: myapp:1.0.0
        ports:
        - containerPort: 8080

Ask Claude Code to generate the corresponding green deployment by prompting: “Create a Kubernetes deployment YAML for the green version with the same configuration but using image myapp:1.1.0 and version green label.”

Service Routing Configuration

Your Kubernetes Service needs to route traffic to the active environment. Claude Code can help you manage this with labels:

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: myapp
    version: blue  # Change to 'green' to switch traffic
  ports:
  - port: 80
    targetPort: 8080
  type: ClusterIP

Automating the Deployment with Claude Code

Manual switching between environments is error-prone. Claude Code can help you create scripts that automate the entire blue-green deployment process.

Deployment Script

Create a deployment script that Claude Code can help you maintain:

#!/bin/bash

# Blue-Green Deployment Script
set -e

APP_NAME="myapp"
NEW_VERSION="${1}"
NAMESPACE="production"

echo "Starting blue-green deployment for version $NEW_VERSION"

# Get current active color
ACTIVE_COLOR=$(kubectl get service $APP_NAME-service -n $NAMESPACE \
  -o jsonpath='{.spec.selector.version}')

# Determine target color
if [ "$ACTIVE_COLOR" = "blue" ]; then
    TARGET_COLOR="green"
else
    TARGET_COLOR="blue"
fi

echo "Current: $ACTIVE_COLOR -> Deploying: $TARGET_COLOR"

# Deploy new version
kubectl set image deployment/$APP_NAME-$TARGET_COLOR \
  $APP_NAME=$APP_NAME:$NEW_VERSION -n $NAMESPACE

# Wait for rollout
kubectl rollout status deployment/$APP_NAME-$TARGET_COLOR -n $NAMESPACE

# Run smoke tests (you define these)
./scripts/smoke-tests.sh $TARGET_COLOR

# Switch traffic
kubectl patch service $APP_NAME-service -n $NAMESPACE \
  -p "{\"spec\":{\"selector\":{\"app\":\"$APP_NAME\",\"version\":\"$TARGET_COLOR\"}}}"

echo "Deployment complete. Active color is now $TARGET_COLOR"

Implementing Rollback Strategies

One of the biggest advantages of blue-green deployment is instant rollback. Claude Code can help you implement robust rollback mechanisms.

Quick Rollback Script

#!/bin/bash

NAMESPACE="production"
APP_NAME="myapp"

# Get current active version
ACTIVE_COLOR=$(kubectl get service $APP_NAME-service -n $NAMESPACE \
  -o jsonpath='{.spec.selector.version}')

# Calculate previous color
if [ "$ACTIVE_COLOR" = "blue" ]; then
    PREVIOUS_COLOR="green"
else
    PREVIOUS_COLOR="blue"
fi

echo "Rolling back to $PREVIOUS_COLOR environment..."

# Switch traffic back immediately
kubectl patch service $APP_NAME-service -n $NAMESPACE \
  -p "{\"spec\":{\"selector\":{\"app\":\"$APP_NAME\",\"version\":\"$PREVIOUS_COLOR\"}}}"

# Notify team
curl -X POST "$SLACK_WEBHOOK_URL" \
  -H 'Content-Type: application/json' \
  -d "{\"text\": \"🚨 Rollback completed: switched to $PREVIOUS_COLOR\"}"

echo "Rollback complete in $(($DIFF_TIME / 60)) seconds"

Integrating with CI/CD Pipelines

Claude Code excels at generating CI/CD pipeline configurations. Here’s how to integrate blue-green deployment into your GitHub Actions workflow:

name: Blue-Green Deployment

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Deploy to Green Environment
        run: |
          kubectl apply -f k8s/green-deployment.yaml
          kubectl rollout status deployment/myapp-green -n production
      
      - name: Run Integration Tests
        run: |
          ./scripts/integration-tests.sh green
      
      - name: Switch Traffic to Green
        run: |
          kubectl patch service myapp-service -n production \
            -p '{"spec":{"selector":{"app":"myapp","version":"green"}}}'
      
      - name: Monitor and Alert
        run: |
          ./scripts/monitor-deployment.sh green
          if [ $? -ne 0 ]; then
            ./scripts/rollback.sh
            exit 1
          fi

Database Considerations for Blue-Green Deployment

Database changes can complicate blue-green deployments. Claude Code can help you implement strategies to handle schema migrations safely.

The Expand-Contract Pattern

For database migrations, use the expand-contract pattern:

  1. Expand: Add new fields/tables without affecting existing code
  2. Migrate: Update application code to use new structure
  3. Contract: Remove old fields after full deployment

Claude Code can generate migration scripts following this pattern:

-- Expansion: Add new column (non-breaking)
ALTER TABLE users ADD COLUMN subscription_tier VARCHAR(20) DEFAULT 'free';

-- After deployment, run code migration
-- Then contraction: Remove old column
-- ALTER TABLE users DROP COLUMN legacy_tier;

Health Checks and Traffic Validation

Before switching traffic, validate that your green environment is healthy. Claude Code can help you implement comprehensive health checks.

Health Check Configuration

livenessProbe:
  httpGet:
    path: /health
    port: 8080
  initialDelaySeconds: 30
  periodSeconds: 10

readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 5

Best Practices and Actionable Advice

Here are key recommendations for successful blue-green deployments with Claude Code:

Always keep environments synchronized. Use infrastructure-as-code and ensure both environments have identical configurations. Claude Code can audit your configurations for parity.

Implement proper observability. Before switching traffic, ensure logging and monitoring are working in both environments. Claude Code can help set up dashboards that track both blue and green metrics simultaneously.

Test your rollback procedure regularly. Don’t wait for a crisis to discover your rollback script doesn’t work. Run a “practice rollback” in staging regularly.

Use feature flags for database changes. When possible, make database changes backward-compatible so you can deploy without the blue-green pattern for simple updates.

Automate everything. Manual deployments introduce human error. Use Claude Code to generate automation scripts and CI/CD pipelines.

Conclusion

Blue-green deployment provides a safe, zero-downtime release strategy that’s essential for production applications. Claude Code can significantly accelerate your implementation by generating infrastructure code, automation scripts, and CI/CD pipeline configurations. Start with a simple two-environment setup, automate your deployment and rollback processes, and gradually add health checks and validation to build a robust blue-green deployment workflow.

The key is to start simple, test thoroughly, and iterate. Claude Code is particularly effective at helping you understand the tradeoffs between different deployment strategies and choose the right approach for your specific use case.

Built by theluckystrike — More at zovo.one