Skip to content

GitHub Actions Multi-Environment Deployment Guide: Complete Dev/Staging/Prod Automation

Target Audience

  • Intermediate developers who understand CI/CD basics (with GitHub Actions experience)

Key Points

  1. Automate progressive Dev→Staging→Prod deployment workflow
  2. Set up environment-specific secret management and approval processes
  3. Implement automatic rollback functionality for deployment failures

Why This Problem is Critical Now

Modern application development requires quality assurance across multiple environments. Manual deployments lead to human errors, increased deployment time, and configuration drift between environments causing frequent issues. GitHub Actions provides a fundamental solution to these challenges.

Solution Steps Overview

StepContentSuccess Metric
1Environment setup and secret management3 environment secrets configured
2Progressive deployment workflow creationAutomated deployment execution success
3Approval and rollback configurationManual approval + auto-recovery verification

Step 1: Environment Setup and Secret Management

Configure dev, staging, and production environments in repository Environments and register environment-specific secrets.

# .github/workflows/multi-env-deploy.yml
name: Multi-Environment Deployment

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  deploy-dev:
    if: github.ref == 'refs/heads/develop'
    runs-on: ubuntu-latest
    environment: dev
    steps:
      - uses: actions/checkout@v4
      - name: Deploy to Dev
        run: |
          echo "Deploying to Dev environment"
          echo "${{ secrets.DEV_API_KEY }}" | head -c 8

Step 2: Progressive Deployment Workflow Creation

Add configuration to deploy staging → production in sequence when pushing to main branch.

  deploy-staging:
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - uses: actions/checkout@v4
      - name: Run Tests
        run: npm test
      - name: Deploy to Staging
        run: |
          echo "Deploying to Staging"
          # Actual deployment commands

  deploy-production:
    needs: deploy-staging
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v4
      - name: Deploy to Production
        run: echo "Production deployment complete"

Step 3: Approval and Rollback Configuration

Require manual approval for Production and Staging with Protection Rules settings and implement post-deployment health checks.

  health-check:
    needs: deploy-production
    runs-on: ubuntu-latest
    steps:
      - name: Health Check
        run: |
          curl -f ${{ secrets.PROD_URL }}/health || exit 1
      - name: Rollback on Failure
        if: failure()
        run: |
          echo "Health check failed, initiating rollback"
          # Automatic recovery to previous version

Common Pitfalls and Solutions

SymptomCauseImmediate Fix
Secret retrieval errorEnvironment not configuredCreate environment in Settings > Environments
Infinite waiting stateRequired reviewers not setSpecify reviewers in Protection rules
Deployment target connection failureNetwork configuration issueCheck VPN settings or IP restrictions
Advanced Configuration (High-Level Optimization) ### Conditional Branch Deployment
deploy-feature:
  if: startsWith(github.ref, 'refs/heads/feature/')
  environment: dev
  runs-on: ubuntu-latest
  steps:
    - name: Feature Branch Deploy
      run: echo "Feature environment deployment"
### Environment Variable Templates
env:
  NODE_ENV: ${{ github.event.repository.environment == 'production' && 'production' || 'staging' }}
  API_URL: ${{ secrets.API_URL }}
### Detailed Slack Notifications
- name: Notify Slack
  uses: 8398a7/action-slack@v3
  with:
    status: ${{ job.status }}
    text: 'Deployment to ${{ github.event.repository.environment }} completed'
  env:
    SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }}