What are secrets in CI/CD, and how should they be handled?
BeginnerConceptCI/CD practices
Answer
In one line: Secrets are sensitive values a pipeline needs — passwords, API keys, tokens, certificates, cloud credentials — and they must be stored securely, injected only at runtime and never written into code or logs.
Golden rules:
- Never commit secrets to Git — even in private repos. Git history keeps them forever.
- Use a secret store:
- The CI tool's encrypted secrets (GitHub Actions secrets, GitLab CI variables, Jenkins Credentials).
- A dedicated vault: HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, GCP Secret Manager.
- Inject at runtime as environment variables or files, only into the jobs that need them.
- Mask them in logs — most CI tools hide known secret values automatically; avoid
echo $PASSWORDorset -xwhen secrets are present. - Least privilege — each pipeline gets only the permissions it needs, and separate credentials for dev and prod.
- Prefer short-lived credentials — use OIDC federation so the pipeline gets a temporary cloud token instead of a long-lived access key.
- Rotate regularly, and immediately if a secret leaks.
- Scan for leaks — Gitleaks or TruffleHog in pre-commit hooks and in CI.
Example — GitHub Actions to AWS with OIDC (no stored access keys):
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/gha-deploy
aws-region: ap-south-1
For apps in Kubernetes: use External Secrets Operator or the Secrets Store CSI driver to sync secrets from Vault or a cloud secret manager; don't put plain secrets in Git (or encrypt them with Sealed Secrets or SOPS).
If a secret leaks: revoke/rotate it first, then check logs for misuse, then clean the history.
Tip for interviews: mention OIDC short-lived credentials — it's the modern best practice and many candidates don't know it.
More on CI/CD practices
What is "pipeline as code", and why is it better than configuring pipelines in a UI?BeginnerWhat is a build agent or runner?BeginnerWhat are pipeline triggers?BeginnerWhat is a quality gate in a CI/CD pipeline?BeginnerHow do you make a slow CI/CD pipeline faster?BeginnerWhat is a monorepo vs a polyrepo, and how does it affect CI/CD?Beginner