Secrets in code repositories and CI/CD: never commit keys, scan for leaks and rotate on exposure

  1. Goal
  2. Preparation
  3. What a secret is and why a repository is a poor place to keep it
  4. Steps
  5. Step 1: create a test repository and `.gitignore`
  6. Step 2: add a pre-commit hook to block `.env` files
  7. Step 3: scan for secrets in CI and on the platform
  8. Step 4: when a secret has already entered the repository
  9. Step 5: use secrets in CI/CD correctly
  10. Verification
  11. Common mistakes
  12. Summary
  13. References

Goal

After this article you can:

This article follows on from the article on the secure software development lifecycle: there, secrets are one item on the acceptance checklist; here we see how to satisfy that item.

Preparation

What a secret is and why a repository is a poor place to keep it

A secret is any value that lets whoever holds it act in your place: a database password, an API key, an access token, an SSH or TLS private key, a webhook signing string.

A code repository is a poor place for them for three reasons:

Git history is practically permanent.

Deleting a line in a new commit does not delete it from old commits. Every clone, every branch and every fork carries that history.

Repositories are widely shared.

More people can read the code than need to know the production password.

Repositories are easily exposed.

A private repository accidentally made public, or a leaked backup, brings every secret inside with it.

The practical rule: code lives in the repository, secrets live outside it, and the application receives secrets at run time through environment variables, mounted files or a secrets manager.

The idea of separating configuration from the image is also what ConfigMaps and Secrets: separating configuration from your application image on Kubernetes presents for Kubernetes.

Steps

Steps

Step 1: create a test repository and `.gitignore`

mkdir demo-secrets && cd demo-secrets
git init
printf 'DB_PASSWORD=example-not-real\n' > .env
printf '.env\n.env.*\n!.env.example\n' > .gitignore
printf 'DB_PASSWORD=\n' > .env.example
git status --short

git status shows only .gitignore and .env.example; the .env file is ignored. The .env.example file contains variable names but no values, so newcomers know what to configure.

Check why a file is ignored:

git check-ignore -v .env

The command prints the rule in .gitignore that matched.

Important note: .gitignore only affects files that are not yet tracked. If .env was ever committed, adding it to .gitignore is not enough. To stop tracking it (keeping the file on disk):

git rm --cached .env

But this only stops tracking from new commits on; the old history still contains the secret, see Step 4.

Step 2: add a pre-commit hook to block `.env` files

A hook is a script git runs before creating a commit. Create .git/hooks/pre-commit:

cat > .git/hooks/pre-commit <<'HOOK'
#!/bin/sh
# Block the commit if a .env or .env.* file is staged (except .env.example)
if git diff --cached --name-only | grep -E '(^|/)\.env($|\.)' | grep -v '\.env\.example$'; then
  echo "Commit blocked: a .env file is staged." >&2
  exit 1
fi
HOOK
chmod +x .git/hooks/pre-commit

Try it: git add -f .env then git commit -m test. The commit is blocked and the hook prints the file name. Remove it from the staging area with git rm --cached .env.

A local hook is only the first safety net: anyone can bypass it (git commit --no-verify) and it does not travel with git clone. So you also need a check on the server side, see Step 3.

Step 3: scan for secrets in CI and on the platform

Two complementary approaches:

Scanning tools can raise false positives or miss things, so this is a detection layer, not a reason to be less careful at the other layers.

Step 4: when a secret has already entered the repository

Suppose you discover that a key was committed. The recommended order of work:

Revoke and rotate immediately.

Create a new key, update where it is used, then disable the old key at the provider. This is the most important step, because you cannot know who copied the key while it was exposed.

Check the old key's usage log

(if the provider has one) for unusual access.

Find where it appears

in the history:

git log --all --oneline -S'example-not-real'

-S finds commits that change the number of occurrences of that string.

Cleaning the history is secondary

, done after rotation, with a history-rewriting tool such as git filter-repo or BFG Repo-Cleaner.

Warning: rewriting history changes every later commit ID, forces others to fix their copies and requires git push --force, an operation that can overwrite other people's work. Agree with the whole team and back up the repository first. Even after cleaning, treat the old key as exposed.

Step 5: use secrets in CI/CD correctly

With GitHub Actions, secrets are stored in the repository or environment Secrets and accessed through the secrets context:

name: deploy
on:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  deploy:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4
      - name: Call the deployment API
        env:
          DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
        run: ./scripts/deploy.sh

In the example above, actions/checkout@v4 uses a version tag for brevity; in a real setup, pin it to a full commit hash as recommended below.

Accompanying principles:

Verification

Common mistakes

Summary

Keep secrets out of the code repository, block leaks with several layers (.gitignore, a hook, automated scanning), give CI/CD least privilege and always have a rotation procedure ready.

A leaked secret must be replaced, not hidden.

A team should also remember that third-party software in the project carries its own risk, the topic of Tracking vulnerabilities in dependencies and handling end-of-life (EOL) components.

References

Secrets in code repositories and CI/CD: never commit keys, scan for leaks and rotate on exposure