How do you run Gitleaks in a CI pipeline and fail the build on findings?
BeginnerHands-onGitleaks
Answer
Short answer
Add a Gitleaks step that scans the repo (or just the PR's new commits) and exits non-zero when it finds a secret, which fails the job. Make that job a required status check so the PR can't merge.
GitHub Actions
name: secret-scan
on: [pull_request, push]
jobs:
gitleaks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 } # full history so all PR commits are scanned
- uses: gitleaks/gitleaks-action@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GitLab CI
gitleaks:
stage: test
image: { name: zricethezav/gitleaks:latest, entrypoint: [""] }
script:
- gitleaks git . --redact -v --report-format json --report-path gitleaks.json
artifacts: { when: always, paths: [gitleaks.json] }
Jenkins (declarative)
stage('Secret scan') {
steps {
sh 'docker run --rm -v $WORKSPACE:/repo zricethezav/gitleaks:latest git /repo --redact -v'
}
}
Tips
fetch-depth: 0— a shallow clone hides older commits in the PR.--redact— don't print the secret in the CI log you're trying to protect.- Upload SARIF to GitHub code scanning for a nice UI.
What to say in the interview
"A Gitleaks job on every PR and push with full history, redacted output and a report artifact; a finding exits non-zero, and branch protection makes the job required, so leaked secrets can't be merged."
More on Gitleaks
What is Gitleaks and how do you use it?BeginnerWhat is a secret leak and why is it dangerous?BeginnerWhat is Gitleaks?BeginnerHow do you scan a repository's full history with Gitleaks and create a report?BeginnerHow do you stop secrets before they're committed using a pre-commit hook?BeginnerGitleaks found a real AWS key in the repo. What do you do?Beginner