How to Use a Matrix of Environments in GitHub Actions
A matrix dimension feeding the environment key fans one deploy job out across every target.
List the environments in strategy.matrix and set environment: ${{ matrix.environment }}. Each environment runs as its own parallel job with its own scoped secrets.
Steps
- Add an
environment:dimension to the matrix. - Set
environment: ${{ matrix.environment }}on the job. - Each environment gets its own job and its own scoped secrets.
Workflow
.github/workflows/ci.yml
jobs:
deploy:
runs-on: ubuntu-latest
strategy:
matrix:
environment: [staging, canary]
environment: ${{ matrix.environment }}
steps:
- run: ./deploy.sh --env ${{ matrix.environment }}Gotchas
- Each matrix leg resolves the scoped secrets for its own environment.
- For ordered promotion (staging then prod) use chained
needs:jobs instead of a matrix.
Related guides
How to Promote a Build Through Dev, Staging, and Prod in GitHub ActionsPromote a single GitHub Actions build artifact through dev, staging, and production with chained jobs that sh…
How to Use a Reusable Workflow per Environment in GitHub ActionsCall one reusable GitHub Actions deploy workflow once per environment, passing the target name as an input an…