Prompt
What is the best way to keep secrets out of code and repos?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
The best way is to never put secrets in source code or version control at all. Use a combination of secret storage, runtime injection, and process controls.
Best practices
-
Use a secrets manager
- Examples: AWS Secrets Manager, Azure Key Vault, GCP Secret Manager, HashiCorp Vault, Doppler, 1Password Secrets Automation.
- Store API keys, DB passwords, tokens there—not in
.envcommitted to git.
-
Inject secrets at runtime
- Load secrets from environment variables, mounted files, or the app/platform secret store when the app starts.
- Use CI/CD or orchestration tools (Kubernetes Secrets, ECS task secrets, GitHub Actions secrets) to supply them securely.
-
Keep config separate from code
- Code should read
process.env,os.environ, etc. - Commit only non-sensitive defaults or example files like
.env.example.
- Code should read
-
Use least privilege
- Secrets should only grant the minimum access needed.
- Prefer short-lived credentials, OAuth tokens, or workload identity over long-lived static keys.
-
Rotate and scope secrets
- Rotate regularly and immediately if exposed.
- Use separate secrets per environment (dev/staging/prod) and per service.
-
Prevent accidental leaks
- Add secret scanning in CI and pre-commit hooks.
- Tools: GitHub secret scanning, gitleaks, trufflehog, detect-secrets.
- Add
.gitignorefor local secret files, but don’t rely on it alone.
-
Don’t log secrets
- Redact tokens/passwords in app logs, error messages, and crash reports.
-
Treat
.envas local-only- Fine for developer machines, but don’t commit real
.envfiles. - Commit only
.env.examplewith placeholder values.
- Fine for developer machines, but don’t commit real
A good default pattern
- App code reads from environment variables.
- Local dev uses a local
.envfile ignored by git. - Production uses a real secrets manager or platform secret store.
- CI uses encrypted secrets and secret scanning.
If a secret already got into a repo
- Revoke/rotate it immediately
- Remove it from the repo history if needed
- Assume it is compromised once committed
If you want, I can give you a practical setup for a specific stack like Node.js, Python, Docker, Kubernetes, or GitHub Actions.