Implementing Secrets Scanning in CI/CD
Overview
This skill covers implementing automated secrets scanning in CI/CD pipelines using gitleaks and trufflehog. It enables security teams to detect API keys, tokens, passwords, and other credentials that have been accidentally committed to source code repositories, providing a CI gate that blocks deployments containing high-severity findings.
Gitleaks scans git repositories and directories for hardcoded secrets using regex patterns and entropy analysis. TruffleHog performs filesystem and git history scans with optional secret verification against live services. Together they provide comprehensive coverage for secrets detection.
When to Use
- When deploying or configuring implementing secrets scanning in ci cd capabilities in your environment
- When establishing security controls aligned to compliance requirements
- When building or improving security architecture for this domain
- When conducting security assessments that require this implementation
Common Misconfigurations & Verification
The most common failure is a scanner that runs but never fails the pipeline:
- No
--exit-code, or exit code swallowed. gitleaks detect/dir without --exit-code 1 (or wrapped in || true, or a job with continue-on-error: true) reports findings while the stage stays green. The CI gate verdict must translate to a non-zero process exit.
- Shallow clone / HEAD-only scan. A default
fetch-depth: 1 checkout or scanning only the latest commit means secrets buried in history go undetected. Use fetch-depth: 0 and scan full history (no narrowing --log-opts).
- TruffleHog without verification or scoped too narrow.
trufflehog filesystem over a partial path, or ignoring --only-verified semantics, changes what counts as a finding — be explicit about scope and severity mapping.
- Threshold set too high. A gate that only fails on
critical lets high secrets through; confirm the parse-and-filter step's threshold matches policy.
- Over-broad
.gitleaksignore / allowlist silently suppresses real leaks.
- Pre-commit hook in report-only mode instead of
gitleaks protect --staged, so secrets still reach the repo.
Concrete verification: Add a known fake credential (e.g. AKIAIOSFODNN7EXAMPLE plus a fake secret key, or a ghp_ token) to a tracked file and run the pipeline. Confirm gitleaks/trufflehog report it, the CI gate verdict is FAIL, and the process exits non-zero so the deployment is blocked — then remove the test secret.
Prerequisites
- Python 3.9 or later
- gitleaks v8.x installed and available on PATH
- trufflehog v3.x installed and available on PATH
- A git repository or directory to scan
- Access to CI/CD platform (GitHub Actions, GitLab CI, Jenkins)
Steps
Install scanning tools: Install gitleaks via package manager or binary download. Install trufflehog via brew install trufflehog or download from GitHub releases.
Configure gitleaks: Create a .gitleaks.toml configuration file in the repository root to define custom rules, allowlists, and path exclusions. Use --config flag to point to custom configs.
Run gitleaks directory scan: Execute gitleaks dir --source . --report-format json --report-path gitleaks-report.json to scan the working directory and generate a JSON report.
Run trufflehog filesystem scan: Execute trufflehog filesystem /path/to/repo --json > trufflehog-report.json to scan files and output JSON findings to a report file.
Parse and filter findings: Use the agent script to parse both JSON reports, filter findings by severity (critical, high, medium, low), and determine whether the CI pipeline should pass or fail.
Integrate into CI pipeline: Add the scanning step to your GitHub Actions workflow, GitLab CI config, or Jenkins pipeline as a pre-deployment gate. Use --exit-code flag in gitleaks to control pipeline behavior.
Configure pre-commit hooks: Set up gitleaks as a pre-commit hook using gitleaks protect --staged to catch secrets before they are committed.
Review and triage findings: Examine the JSON output for false positives, add legitimate entries to .gitleaksignore, and rotate any confirmed leaked credentials immediately.
Expected Output
The agent script produces a JSON report containing:
- Total findings count from each scanner
- Findings grouped by severity level
- Individual finding details including file path, line number, rule ID, and redacted secret
- A CI gate verdict (pass/fail) based on the configured severity threshold
- Execution metadata including scan duration and tool versions
{
"scan_summary": {
"tool": "both",
"total_findings": 3,
"critical": 1,
"high": 1,
"medium": 1,
"low": 0,
"ci_gate": "FAIL",
"fail_reason": "Found 1 critical and 1 high severity findings"
},
"findings": [...]
}
1---2name: implementing-secrets-scanning-in-ci-cd3description: Integrate gitleaks and trufflehog into CI/CD pipelines to detect leaked secrets before deployment4license: Apache-2.05---6
7
8# Implementing Secrets Scanning in CI/CD
9
10## Overview
11
12This skill covers implementing automated secrets scanning in CI/CD pipelines using gitleaks and trufflehog. It enables security teams to detect API keys, tokens, passwords, and other credentials that have been accidentally committed to source code repositories, providing a CI gate that blocks deployments containing high-severity findings.
13
14Gitleaks scans git repositories and directories for hardcoded secrets using regex patterns and entropy analysis. TruffleHog performs filesystem and git history scans with optional secret verification against live services. Together they provide comprehensive coverage for secrets detection.
15
16
17## When to Use
18
19- When deploying or configuring implementing secrets scanning in ci cd capabilities in your environment
20- When establishing security controls aligned to compliance requirements
21- When building or improving security architecture for this domain
22- When conducting security assessments that require this implementation
23
24## Common Misconfigurations & Verification
25
26The most common failure is a scanner that runs but never fails the pipeline:
27
28- **No `--exit-code`, or exit code swallowed.** `gitleaks detect/dir` without `--exit-code 1` (or wrapped in `|| true`, or a job with `continue-on-error: true`) reports findings while the stage stays green. The CI gate verdict must translate to a non-zero process exit.
29- **Shallow clone / HEAD-only scan.** A default `fetch-depth: 1` checkout or scanning only the latest commit means secrets buried in history go undetected. Use `fetch-depth: 0` and scan full history (no narrowing `--log-opts`).
30- **TruffleHog without verification or scoped too narrow.** `trufflehog filesystem` over a partial path, or ignoring `--only-verified` semantics, changes what counts as a finding — be explicit about scope and severity mapping.
31- **Threshold set too high.** A gate that only fails on `critical` lets `high` secrets through; confirm the parse-and-filter step's threshold matches policy.
32- **Over-broad `.gitleaksignore` / allowlist** silently suppresses real leaks.
33- **Pre-commit hook in report-only mode** instead of `gitleaks protect --staged`, so secrets still reach the repo.
34
35**Concrete verification:** Add a known fake credential (e.g. `AKIAIOSFODNN7EXAMPLE` plus a fake secret key, or a `ghp_` token) to a tracked file and run the pipeline. Confirm gitleaks/trufflehog report it, the CI gate verdict is `FAIL`, and the **process exits non-zero so the deployment is blocked** — then remove the test secret.
36
37## Prerequisites
38
39- Python 3.9 or later
40- gitleaks v8.x installed and available on PATH
41- trufflehog v3.x installed and available on PATH
42- A git repository or directory to scan
43- Access to CI/CD platform (GitHub Actions, GitLab CI, Jenkins)
44
45## Steps
46
471. **Install scanning tools**: Install gitleaks via package manager or binary download. Install trufflehog via `brew install trufflehog` or download from GitHub releases.
48
492. **Configure gitleaks**: Create a `.gitleaks.toml` configuration file in the repository root to define custom rules, allowlists, and path exclusions. Use `--config` flag to point to custom configs.
50
513. **Run gitleaks directory scan**: Execute `gitleaks dir --source . --report-format json --report-path gitleaks-report.json` to scan the working directory and generate a JSON report.
52
534. **Run trufflehog filesystem scan**: Execute `trufflehog filesystem /path/to/repo --json > trufflehog-report.json` to scan files and output JSON findings to a report file.
54
555. **Parse and filter findings**: Use the agent script to parse both JSON reports, filter findings by severity (critical, high, medium, low), and determine whether the CI pipeline should pass or fail.
56
576. **Integrate into CI pipeline**: Add the scanning step to your GitHub Actions workflow, GitLab CI config, or Jenkins pipeline as a pre-deployment gate. Use `--exit-code` flag in gitleaks to control pipeline behavior.
58
597. **Configure pre-commit hooks**: Set up gitleaks as a pre-commit hook using `gitleaks protect --staged` to catch secrets before they are committed.
60
618. **Review and triage findings**: Examine the JSON output for false positives, add legitimate entries to `.gitleaksignore`, and rotate any confirmed leaked credentials immediately.
62
63## Expected Output
64
65The agent script produces a JSON report containing:
66- Total findings count from each scanner
67- Findings grouped by severity level
68- Individual finding details including file path, line number, rule ID, and redacted secret
69- A CI gate verdict (pass/fail) based on the configured severity threshold
70- Execution metadata including scan duration and tool versions
71
72```json
73{
74 "scan_summary": {
75 "tool": "both",
76 "total_findings": 3,
77 "critical": 1,
78 "high": 1,
79 "medium": 1,
80 "low": 0,
81 "ci_gate": "FAIL",
82 "fail_reason": "Found 1 critical and 1 high severity findings"
83 },
84 "findings": [...]
85}
86```