Prompt
How should I manage API keys across local dev, CI, and Kubernetes?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
A good rule is: never hardcode API keys in code or images; inject them at runtime from the environment/platform you’re running on.
Recommended approach by environment
1) Local development
Use one of these:
- a local
.envfile for convenience - your shell environment
- a secrets manager wrapper if you have one
Best practice:
- keep
.envout of git - commit a
.env.examplewith placeholder names only - load keys via your app’s config layer, not directly in code
Example:
# .env (not committed)
API_KEY=...
DATABASE_URL=...
2) CI
Store secrets in the CI system’s secret store:
- GitHub Actions Secrets
- GitLab CI Variables
- CircleCI Contexts
- Jenkins Credentials Store
- etc.
Best practice:
- expose them only to jobs that need them
- use masked and protected variables where possible
- avoid printing env vars in logs
- prefer short-lived credentials or OIDC where supported instead of long-lived static API keys
For cloud access, prefer:
- GitHub Actions OIDC -> cloud IAM role
- GitLab OIDC -> cloud IAM role
- workload identity / federation
This reduces secret sprawl.
3) Kubernetes
Do not bake secrets into container images or ConfigMaps.
Use:
- Kubernetes Secrets for basic storage
- better: a secrets manager integration, such as:
- External Secrets Operator
- HashiCorp Vault
- AWS Secrets Manager / Parameter Store
- GCP Secret Manager
- Azure Key Vault
- optionally mount secrets as files instead of env vars if that fits your app better
Best practice:
- encrypt secrets at rest in etcd
- restrict access with RBAC
- scope secrets per namespace/app
- rotate regularly
- avoid sharing one secret across many workloads
Good overall pattern
Use environment-specific secret sources
- Local:
.env - CI: CI secret store
- Kubernetes: external secrets manager or K8s Secret
Use the same variable names across all environments
For example:
API_KEYDATABASE_URL
That keeps app code identical everywhere.
Centralize config loading
Your app should read from:
- environment variables
- optionally fallback to local
.envin dev only
Rotation and access
- rotate keys regularly
- use separate keys for dev/staging/prod
- give each service its own key
- revoke unused keys promptly
- log usage and detect anomalies
What to avoid
- committing secrets to git
- putting secrets in Docker images
- storing secrets in plain-text Kubernetes manifests in repo
- using production keys locally
- reusing one key everywhere
- printing secrets in build logs or app logs
Simple practical setup
- Local:
.env+.env.example - CI: secret variables in the CI platform + OIDC where possible
- Kubernetes: External Secrets Operator or Vault; fallback to Kubernetes Secret if needed
If you want, I can give you:
- a concrete example for Node/Python/Go,
- a GitHub Actions + Kubernetes setup, or
- a best-practice secret hierarchy for dev/staging/prod.