Skip to content
Latchkey

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.

Actions
jobs:
  deploy:
    environment: ${{ matrix.env }}
    strategy:
      matrix:
        env: [staging, production]   # ensure these environments exist in repo settings

Common 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

  1. Create each environment in repository settings ahead of time.
  2. Reference the exact vector key in the environment field.
  3. Confirm each leg maps to its intended environment and secrets.
.github/workflows/ci.yml
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.

Frequently asked questions

What causes "matrix not available in environment"?
A matrix value that names an environment which is not configured in repo settings resolves but has no protection rules or scoped secrets attached.
How do I fix matrix not available in environment?
Match the vector name and create the environments

Related guides

References

Run this faster and cheaper on Latchkey managed runners - self-healing included. Start free → 30-day trial · No credit card