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¶
- Automate progressive Dev→Staging→Prod deployment workflow
- Set up environment-specific secret management and approval processes
- 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¶
| Step | Content | Success Metric |
|---|---|---|
| 1 | Environment setup and secret management | 3 environment secrets configured |
| 2 | Progressive deployment workflow creation | Automated deployment execution success |
| 3 | Approval and rollback configuration | Manual 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¶
| Symptom | Cause | Immediate Fix |
|---|---|---|
| Secret retrieval error | Environment not configured | Create environment in Settings > Environments |
| Infinite waiting state | Required reviewers not set | Specify reviewers in Protection rules |
| Deployment target connection failure | Network configuration issue | Check VPN settings or IP restrictions |
Advanced Configuration (High-Level Optimization)
### Conditional Branch Deploymentdeploy-feature:
if: startsWith(github.ref, 'refs/heads/feature/')
environment: dev
runs-on: ubuntu-latest
steps:
- name: Feature Branch Deploy
run: echo "Feature environment deployment"
env:
NODE_ENV: ${{ github.event.repository.environment == 'production' && 'production' || 'staging' }}
API_URL: ${{ secrets.API_URL }}
- 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 }}