Skip
Arish's avatar

15. Environment Management


GitHub Environments

Configure in Settings → Environments.

yaml
1jobs:
2  deploy-staging:
3    runs-on: ubuntu-latest
4    environment: staging
5    steps:
6      - run: echo "Deploy to staging"
7
8  deploy-production:
9    runs-on: ubuntu-latest
10    environment: production  # Requires approval
11    needs: deploy-staging
12    steps:
13      - run: echo "Deploy to production"

Environment Secrets

Each environment can have its own secrets:

yaml
1jobs:
2  deploy:
3    environment: production
4    steps:
5      - run: ./deploy.sh
6        env:
7          # Uses production-specific secrets
8          DATABASE_URL: ${{ secrets.DATABASE_URL }}
9          API_KEY: ${{ secrets.API_KEY }}

Protection Rules

  • Required reviewers
  • Wait timer
  • Branch restrictions
  • Custom deployment branches

Environment Variables

yaml
1jobs:
2  deploy:
3    environment: 
4      name: production
5      url: https://myapp.com
6    steps:
7      - run: echo "Deployed to ${{ env.URL }}"

Multi-Environment Workflow

yaml
1jobs:
2  test:
3    runs-on: ubuntu-latest
4    steps: [...]
5
6  deploy-staging:
7    needs: test
8    if: github.ref == 'refs/heads/develop'
9    environment: staging
10    steps: [...]
11
12  deploy-production:
13    needs: test
14    if: github.ref == 'refs/heads/main'
15    environment: production
16    steps: [...]