10. Multi-Environment Pipelines: Dev, Staging, Production
Most applications require multiple environments: development, staging, and production. Each environment serves a different purpose. Development is for testing new features. Staging is for integration testing and QA. Production is for live users. Managing multiple environments in CI/CD requires careful planning.
The key insight is that different environments have different requirements. Development should be fast and frequent. Staging should mirror production. Production should be stable and secure. Understanding environment-specific requirements is essential for designing effective multi-environment pipelines.
This module covers environment strategies, promotion workflows, and best practices for managing multiple environments.
Environment Strategies
There are several strategies for managing multiple environments. Manual promotion requires manual approval to promote between environments. Automatic promotion automatically promotes changes that pass tests. GitFlow uses release branches for promotions. GitOps uses Git as the source of truth for all environments. Each strategy has trade-offs between speed, control, and reliability.
For most teams, a hybrid approach works best: automatic deployment to development, automatic deployment to staging after tests pass, and manual approval for production. This balances speed with safety. Understanding these strategies is essential for designing effective pipelines.
Step-by-Step: Multi-Environment Pipeline
Follow these steps to build a multi-environment CI/CD pipeline.
Step 1: Define environment-specific configurations# .github/environments/dev.env<br>ENVIRONMENT=development<br>API_URL=https://dev-api.example.com<br><br># .github/environments/staging.env<br>ENVIRONMENT=staging<br>API_URL=https://staging-api.example.com<br><br># .github/environments/prod.env<br>ENVIRONMENT=production<br>API_URL=https://api.example.com
Step 2: Create promotion workflowname: Deploy to Environment<br><br>on:<br> workflow_dispatch:<br> inputs:<br> environment:<br> description: <strong><code>Environment to deploy
required: true
type: choice
options: [dev, staging, production]
jobs:
deploy:
runs-on: ubuntu-latest
environment: ${{ github.event.inputs.environment }}
steps:
- uses: actions/checkout@v4
- name: Deploy
run: |
echo "Deploying to ${{ github.event.inputs.environment }}"
# Deployment script
Step 3: Add automatic promotionon:<br> push:<br> branches: [ develop ] # Deploys to dev<br> pull_request:<br> branches: [ main ] # Deploys to staging<br> workflow_dispatch: # Manual for production
Try It Yourself: Create a multi-environment pipeline with dev, staging, and production environments. Implement promotion rules between environments.
Knowledge Check
Ready to test your understanding of 10. Multi-Environment Pipelines: Dev, Staging, Production?