Skip to content
intermediate Phase 96 · Release Engineering

Deployment Pipelines for Magento

Deployment pipelines for Magento including staging, production, deployment strategies, approval gates, and rollback procedures

45m
0 problems
Topic Progress 0%

Deployment Pipeline Architecture

Pipeline Flow

Build -> Test -> Package -> Deploy Staging -> QA -> Approve -> Deploy Production
  |                                    |              |
  +-- Artifacts                        +-- Manual     +-- Manual
                                           Gate        Gate

GitHub Actions Deployment Pipeline

# .github/workflows/deploy.yml
defaults:
  run:
    shell: bash

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'
      - run: composer install --prefer-dist
      - run: bin/magento setup:di:compile
      - run: bin/magento setup:static-content:deploy -f
      - name: Package Build
        run: tar -czf magento-build.tar.gz app/code/Vendor/ pub/static/ generated/
      - uses: actions/upload-artifact@v3
        with:
          name: magento-build
          path: magento-build.tar.gz

  deploy-staging:
    needs: build
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - uses: actions/download-artifact@v3
        with:
          name: magento-build
      - name: Deploy to Staging
        run: |
          scp magento-build.tar.gz deployer@staging:/var/www/
          ssh deployer@staging "cd /var/www && tar -xzf magento-build.tar.gz"
          ssh deployer@staging "cd /var/www && bin/magento maintenance:enable"
          ssh deployer@staging "cd /var/www && bin/magento setup:upgrade"
          ssh deployer@staging "cd /var/www && bin/magento cache:flush"
          ssh deployer@staging "cd /var/www && bin/magento maintenance:disable"

  approve-production:
    needs: deploy-staging
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://www.example.com
    steps:
      - name: Wait for Approval
        run: echo "Waiting for production approval"

  deploy-production:
    needs: approve-production
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v3
        with:
          name: magento-build
      - name: Deploy to Production
        run: |
          scp magento-build.tar.gz deployer@production:/var/www/
          ssh deployer@production "cd /var/www && tar -xzf magento-build.tar.gz"
          ssh deployer@production "cd /var/www && bin/magento maintenance:enable"
          ssh deployer@production "cd /var/www && bin/magento setup:upgrade"
          ssh deployer@production "cd /var/www && bin/magento cache:flush"
          ssh deployer@production "cd /var/www && bin/magento maintenance:disable"

Key Takeaway

Deployment pipelines have build, staging, approval, and production stages. Each stage has environment-specific configuration and manual gates for production.

Deployment Strategies

Blue-Green Deployment

# Two identical environments
blue: current production (v1)
green: new version (v2)

# 1. Deploy to green
scp build.tar.gz green:/var/www/
ssh green "cd /var/www && tar -xzf build.tar.gz"
ssh green "bin/magento setup:upgrade && bin/magento cache:flush"

# 2. Test green
ssh green "curl -f http://localhost/health"

# 3. Switch traffic
# Update load balancer to point to green
# Blue becomes standby for rollback

Rolling Deployment

# Deploy to servers one at aiven
for server in web1 web2 web3 web4; do
    echo "Deploying to $server..."
    
    # Take out of load balancer
    ssh lb "disable_server $server"
    
    # Deploy
    scp build.tar.gz $server:/var/www/
    ssh $server "cd /var/www && tar -xzf build.tar.gz"
    ssh $server "bin/magento setup:upgrade && bin/magento cache:flush"
    
    # Health check
    ssh $server "curl -f http://localhost/health"
    
    # Put back in load balancer
    ssh lb "enable_server $server"
    
    echo "$server deployed successfully"
done

Maintenance Mode Deployment

#!/bin/bash
# deploy-production.sh

SERVER="production.example.com"
BUILD="magento-build.tar.gz"

# Enable maintenance mode
echo "Enabling maintenance mode..."
ssh $SERVER "cd /var/www && bin/magento maintenance:enable"

# Deploy build
echo "Deploying build..."
scp $BUILD $SERVER:/var/www/
ssh $SERVER "cd /var/www && tar -xzf $BUILD"

# Run setup
echo "Running setup:upgrade..."
ssh $SERVER "cd /var/www && bin/magento setup:upgrade"

# Deploy static content
echo "Deploying static content..."
ssh $SERVER "cd /var/www && bin/magento setup:static-content:deploy -f"

# Clear cache
echo "Clearing cache..."
ssh $SERVER "cd /var/www && bin/magento cache:flush"

# Disable maintenance mode
echo "Disabling maintenance mode..."
ssh $SERVER "cd /var/www && bin/magento maintenance:disable"

echo "Deployment complete!"

Key Takeaway

Blue-green maintains two environments for instant rollback. Rolling deploys servers one at a time. Maintenance mode prevents user impact during deployment.

Approval Gates and Safeguards

GitHub Environments with Protection Rules

# Repository Settings -> Environments -> production
# Required reviewers: 2
# Wait timer: 5 minutes
# Deployment branches: main only

jobs:
  deploy-production:
    runs-on: ubuntu-latest
    environment:
      name: production
      url: https://www.example.com
    # Waits for manual approval in GitHub UI

Deployment Checklist

## Pre-Deployment Checklist
- [ ] All tests passing in CI
- [ ] Code reviewed and approved
- [ ] Staging deployment verified
- [ ] Database backup completed
- [ ] Rollback plan documented
- [ ] Maintenance window scheduled (if needed)
- [ ] Team notified

## Post-Deployment Verification
- [ ] Health check endpoints responding
- [ ] Critical user flows tested
- [ ] Performance metrics normal
- [ ] Error rates acceptable
- [ ] Cache warmed
- [ ] Monitoring alerts reviewed

Automated Safety Checks

- name: Pre-deployment Safety Check
  run: |
    # Verify staging health
    STATUS=$(curl -s -o /dev/null -w '%{http_code}' https://staging.example.com/health)
    if [ $STATUS -ne 200 ]; then
      echo "Staging is unhealthy ($STATUS)"
      exit 1
    fi
    
    # Verify no critical issues
    # Check error tracking for recent spikes
    
    # Verify database backup exists
    ssh deployer@backup "ls -la /backups/magento/$(date +%Y%m%d)" || {
      echo "No backup found for today"
      exit 1
    }

Deployment Notifications

- name: Notify Deployment
  if: always()
  run: |
    if [ '${{ job.status }}' == 'success' ]; then
      curl -X POST ${{ secrets.SLACK_WEBHOOK }} \
        -d '{"text":"Production deployment succeeded"}'
    else
      curl -X POST ${{ secrets.SLACK_WEBHOOK }} \
        -d '{"text":"Production deployment FAILED"}'
    fi

Key Takeaway

Approval gates require manual confirmation before production deployment. Safety checks verify staging health and backups. Notifications keep the team informed.

Production Deployment Scripts

Complete Deployment Script

#!/bin/bash
# deploy.sh - Production deployment with rollback support

set -e  # Exit on error

DEPLOY_DIR="/var/www/magento"
BACKUP_DIR="/var/backups/magento"
BUILD_FILE="magento-build.tar.gz"
LOG_FILE="/var/log/magento-deploy.log"

deploy() {
    local timestamp=$(date +%Y%m%d_%H%M%S)
    local backup_path="$BACKUP_DIR/$timestamp"
    
    echo "[$timestamp] Starting deployment..." | tee -a $LOG_FILE
    
    # Create backup
    echo "Creating backup..."
    mkdir -p $backup_path
    cp -r $DEPLOY_DIR/app $backup_path/
    cp -r $DEPLOY_DIR/var $backup_path/
    cp -r $DEPLOY_DIR/generated $backup_path/
    cp $DEPLOY_DIR/app/etc/env.php $backup_path/
    
    # Enable maintenance mode
    echo "Enabling maintenance mode..."
    cd $DEPLOY_DIR && bin/magento maintenance:enable
    
    # Deploy build
    echo "Deploying build..."
    cp $BUILD_FILE $DEPLOY_DIR/
    cd $DEPLOY_DIR && tar -xzf $BUILD_FILE
    
    # Run setup
    echo "Running setup:upgrade..."
    cd $DEPLOY_DIR && bin/magento setup:upgrade --no-interaction
    
    # Deploy static content
    echo "Deploying static content..."
    cd $DEPLOY_DIR && bin/magento setup:static-content:deploy -f -j4
    
    # Clear cache
    echo "Clearing cache..."
    cd $DEPLOY_DIR && bin/magento cache:flush
    
    # Disable maintenance mode
    echo "Disabling maintenance mode..."
    cd $DEPLOY_DIR && bin/magento maintenance:disable
    
    # Health check
    echo "Running health check..."
    sleep 5
    HTTP_STATUS=$(curl -s -o /dev/null -w '%{http_code}' http://localhost/)
    if [ $HTTP_STATUS -ne 200 ]; then
        echo "Health check failed (HTTP $HTTP_STATUS)"
        rollback $backup_path
        exit 1
    fi
    
    echo "[$timestamp] Deployment successful!" | tee -a $LOG_FILE
}

rollback() {
    local backup_path=$1
    echo "Rolling back to $backup_path..."
    
    cd $DEPLOY_DIR && bin/magento maintenance:enable
    
    rm -rf $DEPLOY_DIR/app $DEPLOY_DIR/var $DEPLOY_DIR/generated
    cp -r $backup_path/app $DEPLOY_DIR/
    cp -r $backup_path/var $DEPLOY_DIR/
    cp -r $backup_path/generated $DEPLOY_DIR/
    cp $backup_path/env.php $DEPLOY_DIR/app/etc/
    
    cd $DEPLOY_DIR && bin/magento cache:flush
    cd $DEPLOY_DIR && bin/magento maintenance:disable
    
    echo "Rollback complete!"
}

case "${1:-deploy}" in
    deploy) deploy ;;
    rollback) rollback $2 ;;
    *) echo "Usage: $0 {deploy|rollback <backup-path>}" ;;
esac

Key Takeaway

Deployment scripts should include backup creation, maintenance mode, setup commands, health checks, and automatic rollback on failure.

Quiz

1. What is blue-green deployment?

Question 1 options

2. What does an approval gate do?

Question 2 options

3. Why enable maintenance mode during deployment?

Question 3 options

4. What should a deployment script include?

Question 4 options

5. What is the purpose of post-deployment health checks?

Question 5 options

Flashcards

Question

What is blue-green deployment?

Answer

Two identical environments; deploy to idle, test, switch traffic. Enables instant rollback.

Question

What is rolling deployment?

Answer

Deploy to servers one at a time, removing each from load balancer during deployment

Question

Why use approval gates?

Answer

Require manual confirmation before production deployment to prevent accidental releases

Question

What is maintenance mode?

Answer

Shows maintenance page to users while deployment changes are applied

Question

What does set -e do in deployment scripts?

Answer

Exit immediately if any command fails, preventing partial deployments

Question

Why create backups before deployment?

Answer

Enable rollback to previous state if deployment fails

Question

What is the deployment pipeline order?

Answer

Build -> Deploy Staging -> QA -> Approve -> Deploy Production

Question

Why wait after enabling maintenance mode?

Answer

Ensure all active requests complete before making changes

Revision Notes

Key Takeaways

  • 1. Deployment pipelines: build, staging, approval, production
  • 2. Blue-green for instant rollback, rolling for zero-downtime
  • 3. Approval gates require manual confirmation before production
  • 4. Maintenance mode prevents user impact during deployment
  • 5. Deployment scripts should include backup and rollback

Interview Tips

  • Compare deployment strategies and their tradeoffs
  • Explain approval gate implementation
  • Describe rollback procedures
  • Discuss zero-downtime deployment approaches

Cheat Sheet

Deployment Pipelines

Flow:
Build -> Staging -> Approve -> Production

Strategies:

  • Blue-green: Two environments, instant rollback
  • Rolling: Deploy server by server
  • Maintenance: Prevent user impact

Safeguards:

  • Approval gates
  • Health checks
  • Automatic rollback
  • Notifications

Deployment Steps:

  1. Backup
  2. Maintenance mode
  3. Deploy build
  4. setup:upgrade
  5. static-content:deploy
  6. cache:flush
  7. Disable maintenance
  8. Health check