Prompt
What's the best way to keep database credentials and API keys out of code and CI logs?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
Best practice is to treat credentials as secrets and keep them out of both source code and build output by using environment variables + a secret manager.
Recommended approach
1) Never hardcode secrets in code
Avoid putting database passwords, API keys, tokens, or private keys in:
- application source files
- config files committed to git
- Dockerfiles
- CI YAML
- shell scripts
2) Inject secrets at runtime
Use one of these:
- Environment variables for local dev and simple deployments
- Secret manager for production and CI/CD
Examples of secret managers:
- AWS Secrets Manager / SSM Parameter Store
- Azure Key Vault
- Google Secret Manager
- HashiCorp Vault
- Kubernetes Secrets
- GitHub Actions Secrets / GitLab CI Variables / CircleCI Contexts
3) Load secrets from the environment in your app
Your app should read values like:
DATABASE_URLAPI_KEYJWT_SECRET
Example pattern:
import os
db_url = os.environ["DATABASE_URL"]
api_key = os.environ["API_KEY"]
4) Keep secrets out of CI logs
In CI:
- store secrets in the platform’s secret store
- pass them as masked environment variables
- never
echothem - disable verbose/debug output when secrets might appear
- avoid printing full command lines that include secrets
- make sure scripts don’t dump environment variables
Also:
- use secret masking features where available
- be careful with tools that print config, env, or stack traces
- avoid command arguments containing secrets, since some systems log them
5) Use .env only for local development
For local dev, a .env file is fine, but:
- add it to
.gitignore - commit only
.env.examplewith placeholder values - don’t use
.envin production unless it’s injected securely by your runtime
6) Rotate and scope credentials
If a secret leaks:
- rotate it immediately
- use least privilege
- use short-lived credentials where possible
- separate dev/staging/prod credentials
Good workflow
- Local dev:
.envfile +.gitignore - CI/CD: masked CI secrets or OIDC to fetch temporary credentials
- Production: secret manager + runtime injection
Extra protections
- Use OIDC/federated identity instead of long-lived cloud keys in CI when possible
- Scan repos for leaked secrets with tools like:
- GitHub secret scanning
- TruffleHog
- Gitleaks
- Set up pre-commit hooks to block accidental commits
Rule of thumb
If a secret is:
- in code, it can be committed
- in a command line, it can be logged
- in a file on disk, it can be copied
So the safest pattern is: store secrets centrally, inject them at runtime, and never print them.
If you want, I can also give you a best-practice setup for GitHub Actions, Docker, or Kubernetes specifically.