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."

All Security Tools questions Roadmap: Gitleaks — secret scanning

More on Gitleaks