Secret Scanning in CI: Detecting Credential Leaks with Gitleaks
We’ve all seen it happen.

Catch credential leaks before they become security incidents — automate secret scanning directly in your CI pipeline.
Secret Scanning in CI: Detecting Credential Leaks with Gitleaks
We’ve all seen it happen.
A developer is testing locally, needs quick access to a cloud service, and temporarily hardcodes an API key into a configuration file. The change gets committed, pushed, and merged before anyone notices.
A few hours later, that credential is indexed, exposed, or worse — actively abused.
While organizations invest heavily in firewalls, IAM policies, and vulnerability scanners, one of the most common security incidents still comes down to something surprisingly simple: secrets accidentally committed to source control.
The good news? This is exactly the kind of problem that can be caught early in CI.
In this article, we’ll explore how to use Gitleaks as part of your CI pipeline to automatically detect credential leaks before they make their way into production.
Why Secret Scanning Matters
Modern applications rely on dozens of credentials:
- Cloud provider access keys
- GitHub tokens
- Database passwords
- Slack webhooks
- API keys
- SSH private keys
- Container registry credentials
Even a single leaked secret can have serious consequences.
Consider a developer accidentally committing an AWS access key:
AWS_ACCESS_KEY_ID=AKIA...
AWS_SECRET_ACCESS_KEY=abcd1234...
If this reaches a public repository — or even an internal repository with broad access — it creates an unnecessary security risk.
The challenge is that code reviews aren’t designed to catch every secret.
Humans miss things.
Automation doesn’t.
What Is Gitleaks?
Gitleaks is an open-source secret scanning tool designed to detect sensitive information in Git repositories.
It scans:
- Commit history
- Source code
- Configuration files
- Infrastructure-as-Code
- CI/CD definitions
and identifies patterns matching known credential formats.
Examples include:
- AWS keys
- GitHub Personal Access Tokens
- Google Cloud credentials
- Stripe API keys
- Slack tokens
- Private keys
It can also detect custom secret patterns defined by your organization.
Why Run Secret Scanning in CI?
Many teams perform security scans periodically.
The problem?
By the time a weekly scan finds a leaked credential:
- The secret may already be exposed.
- Multiple developers may have cloned it.
- The credential might have been used.
CI provides a much earlier checkpoint.
Instead of discovering issues days later:
Developer Push
↓
Secret Scan
↓
Pipeline Fails
↓
Developer Fixes Immediately
This significantly reduces the risk window.
Installing Gitleaks Locally
Before integrating it into CI, it’s worth trying locally.
Using Docker:
docker run --rm \
-v $(pwd):/path \
zricethezav/gitleaks:latest \
detect \
--source="/path"
Or using the binary:
gitleaks detect --source .
A successful scan returns:
✓ no leaks found
If secrets are detected:
Finding: GitHub Token
File: config.env
Line: 12
and exits with a non-zero status code.
That behavior makes it ideal for CI pipelines.
Adding Gitleaks to GitHub Actions
One of the easiest ways to introduce secret scanning is directly in GitHub Actions.
Here’s a simple workflow:
name: Secret Scan
on:
pull_request:
push:
branches:
- main
jobs:
gitleaks:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Run Gitleaks
uses: gitleaks/gitleaks-action@v2
Now every pull request and push triggers a secret scan.
If a secret is found:
Error: leaks found
The workflow fails immediately.
Real-World Example
Imagine a developer accidentally commits:
database:
username: admin
password: SuperSecretPassword123
When the pull request is opened:
Running Gitleaks...
Finding: Generic Credential
File: application.yaml
Line: 3
Leaks detected
The pull request receives a failed status check.
The code cannot proceed until the issue is fixed.
This is exactly the kind of guardrail that prevents accidental exposure.

A single leaked secret can compromise an entire environment. Let Gitleaks stop it before the merge.
Scanning Pull Requests Only
For larger repositories, scanning the entire commit history every run may be unnecessary.
Instead, scan only the changes introduced in a pull request.
name: Secret Scan
on:
pull_request:
jobs:
gitleaks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: gitleaks/gitleaks-action@v2
This keeps scans fast while still catching newly introduced secrets.
Customizing Detection Rules
Every organization has unique credentials.
For example:
COMPANY_API_KEY=corp-prod-xxxxxxxx
Gitleaks supports custom rule definitions.
Example configuration:
[[rules]]
id = "internal-api-key"
description = "Internal API Key"
regex = '''corp-prod-[a-zA-Z0-9]+'''
tags = ["internal"]
Store this configuration in:
.gitleaks.toml
Then run:
gitleaks detect --config .gitleaks.toml
This extends scanning beyond standard secret formats.
Handling False Positives
Every secret scanner occasionally flags values that aren’t actually sensitive.
For example:
example-token-12345
might resemble a credential pattern.
Rather than disabling scanning entirely, use allowlists.
Example:
[allowlist]
paths = [
'''testdata/'''
]
Or suppress specific findings after verification.
The goal is to reduce noise without weakening security.
Going Beyond GitHub Actions
Although GitHub Actions is a common integration point, Gitleaks works in virtually any CI platform:
- GitHub Actions
- GitLab CI
- Jenkins
- Azure DevOps
- CircleCI
- Bitbucket Pipelines
For example, in Jenkins:
stage('Secret Scan') {
steps {
sh 'gitleaks detect --source .'
}
}
The principle remains the same:
Build
↓
Secret Scan
↓
Test
↓
Deploy
Security validation becomes part of the standard delivery process.
What Happens If a Secret Is Already Committed?
Finding a secret in CI is good.
Finding one after it has been merged is still better than not finding it at all.
When a leak occurs:
- Revoke the credential immediately.
- Generate a replacement secret.
- Remove the secret from source control.
- Rotate dependent systems.
- Investigate potential exposure.
A common mistake is only removing the file while leaving the secret active.
Credential rotation should always be the first response.
Best Practices for Secret Scanning
1. Scan Every Pull Request
The earlier a secret is detected, the easier it is to fix.
2. Fail the Pipeline
Secret scanning should block merges.
Warnings are often ignored.
3. Maintain Custom Rules
Your organization’s proprietary credentials are often the most valuable assets.
Add custom detection patterns.
4. Combine with Secret Management
Scanning helps prevent leaks.
Secret managers help eliminate them.
Consider solutions such as:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
5. Scan Historical Repositories
Many organizations enable scanning only for new commits.
Running a one-time historical scan often uncovers years of forgotten credentials.
Final Thoughts
Security doesn’t always fail because of sophisticated attacks. More often, it fails because someone accidentally committed a credential that should never have been in source control.
That’s why secret scanning deserves a permanent place in modern CI pipelines.
By integrating Gitleaks into your workflow, you can:
- Detect secrets before they are merged
- Reduce credential exposure risks
- Enforce security automatically
- Build stronger DevSecOps practices
The best security controls are the ones developers don’t have to remember to run manually. Secret scanning in CI is one of those controls.
Have you integrated secret scanning into your pipelines yet? If so, what tools are you using — Gitleaks, GitHub Advanced Security, TruffleHog, or something else? Share your experience and lessons learned in the comments.
메타데이터
- post_id
- fe3ed815028c
- slug
- secret-scanning-in-ci-detecting-credential-leaks-with-gitleaks-fe3ed815028c
- url
- https://medium.com/@sharathkumarlokesh/secret-scanning-in-ci-detecting-credential-leaks-with-gitleaks-fe3ed815028c
- canonical_url
- https://medium.com/@sharathkumarlokesh/secret-scanning-in-ci-detecting-credential-leaks-with-gitleaks-fe3ed815028c
- author_url
- https://medium.com/@sharathkumarlokesh
- status
- ok
- fetched_at
- 2026-06-09 15:37:30