GitHub Actions matrix context not available in environment in CI
You can reference the matrix context in a job's environment to deploy each leg to a per-combination environment. When it appears to not resolve, the cause is usually a mis-typed vector key or the environment not existing, not a context-scope limitation.
What this error means
A matrix-driven deploy targets the wrong environment, or the environment name shows a literal or empty matrix reference, so protection rules and secrets do not apply as expected.
jobs:
deploy:
environment: ${{ matrix.env }}
strategy:
matrix:
env: [staging, production] # ensure these environments exist in repo settingsCommon causes
The referenced environment does not exist
A matrix value that names an environment which is not configured in repo settings resolves but has no protection rules or scoped secrets attached.
A vector name mismatch resolves to empty
Referencing matrix.environment when the vector is named env yields an empty string, so the environment is unset.
How to fix it
Match the vector name and create the environments
- Create each environment in repository settings ahead of time.
- Reference the exact vector key in the
environmentfield. - Confirm each leg maps to its intended environment and secrets.
jobs:
deploy:
environment: ${{ matrix.env }}
strategy:
matrix:
env: [staging, production]Scope secrets to the matrix environment
Store per-environment secrets on each environment so a matrix leg picks up the right values automatically.
How to prevent it
- Create every environment a matrix will target beforehand.
- Match the environment field to the exact vector key name.
- Attach protection rules and secrets to each environment.