Skip to main content
Secret Detection is a critical security practice in a GitLab CI/CD pipeline. It scans your repository for sensitive information such as API tokens, SSH keys, and passwords, preventing accidental exposure. In this guide, we’ll walk through configuring Secret Detection using the GitLab Components catalog and Gitleaks under the hood.

Initial Pipeline Setup

Start with a basic pipeline that prepares environments, runs code quality checks, SAST, and unit tests:

Add the Secret Detection Component

Pull in the official component from the GitLab Components catalog and conditionally disable it via a variable:
This component leverages Gitleaks to scan commits and code for secrets.
For full details, visit the Secret Detection documentation.

Ignoring Specific Lines

If you need to keep placeholder secrets or test tokens in your code, annotate those lines so Gitleaks will skip them:
Use # gitleaks:allow sparingly—never suppress detection of real or production secrets.

Enabling Full-History Scans

By default, Secret Detection inspects only the diff of each commit. To scan the entire repository history, set:
The image shows a GitLab documentation page about "Full history Secret Detection," explaining how to enable it and customize rulesets. The sidebar lists related topics.

Customizing Detection Rules

Gitleaks ships with over 100 built-in rules. You can override or extend these by providing a custom TOML file:
The image shows a GitLab documentation page about customizing secret detection rules, with highlighted text and a sidebar menu.

Configuring the Secret Detection Job

Integrate the secret_detection job into your .gitlab-ci.yml:
You can also edit this in the GitLab Pipeline Editor:
The image shows a GitLab Pipeline Editor interface with YAML code for configuring CI/CD pipelines. The code includes sections for preparing environments and unit testing, with a dropdown menu suggesting code options.

Running a Test Secret Scan

Commit a dummy secret (e.g., an id_rsa SSH private key) to a feature branch and watch the Secret Detection job:

Viewing Artifacts

After the job finishes, download the JSON report from the artifacts list:
The image shows a GitLab interface displaying a list of artifacts from different jobs, including unit testing and secret detection, with details like file size and creation time.

Interpreting the JSON Report

The gl-secret-detection-report.json includes each finding’s details:
Additional scan metadata:

Next Steps

  1. Remove exposed secrets from the current commit.
  2. Rewrite history to purge sensitive data.
  3. Rotate any compromised keys or tokens.

References

Watch Video