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:

  1. Never commit secrets to Git — even in private repos. Git history keeps them forever.
  2. 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.
  3. Inject at runtime as environment variables or files, only into the jobs that need them.
  4. Mask them in logs — most CI tools hide known secret values automatically; avoid echo $PASSWORD or set -x when secrets are present.
  5. Least privilege — each pipeline gets only the permissions it needs, and separate credentials for dev and prod.
  6. Prefer short-lived credentials — use OIDC federation so the pipeline gets a temporary cloud token instead of a long-lived access key.
  7. Rotate regularly, and immediately if a secret leaks.
  8. 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.

All DevOps Fundamentals questions

More on CI/CD practices