Skip to content
Latchkey

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

Run this faster and cheaper on Latchkey managed runners. Start free →