Skip to content
Latchkey

How to Set Environment-Specific Feature Flag Values

Scope flag values per environment so staging can preview a feature while production stays off.

Most flag tools model separate environments (dev, staging, production) each with their own values. CI selects the environment key for the target so the same code reads the right state.

Steps

  • Define per-environment values for the flag.
  • Store one environment key per environment as a secret.
  • Select the key in CI based on the deploy target.

Per-environment values

flags.yaml
flags:
  new-checkout:
    environments:
      staging:    { defaultVariation: true }
      production: { defaultVariation: false }

Pick the environment in CI

.github/workflows/deploy.yml
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: ${{ inputs.target }}
    env:
      FLAG_ENV_KEY: ${{ secrets[format('FLAG_KEY_{0}', inputs.target)] }}
    steps:
      - run: ./deploy.sh

Gotchas

  • A staging-only flag left on in the shared code path can leak into production if the key is wrong.
  • Keep environment names consistent between the flag tool and your CI environments.

Related guides

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