Safeguard
Developer Tools•

How do you write your own CI security gate, and what will it cost to keep?

Writing a CI security gate is a few lines of script. Keeping it fair, fast and trusted takes more. Steps, exit codes, exceptions and the upkeep to expect.

Safeguard Security Team
3 min read

A CI security gate is a step that runs a scanner and fails the build when results break a rule. The script takes an hour. Keeping the gate fair, fast and trusted, with exceptions and a fallback when the scanner is down, is the real work.

Build a minimal gate

  1. Run the scanner on the pull request.
  2. Compare results against a baseline so old findings do not block new work. Gitleaks supports this with a baseline path, reporting just new findings.
  3. Use exit codes. Gitleaks returns 0 for no leaks and 1 for leaks or an error, and the leak code can be changed with an exit code flag.
  4. Fail on a narrow rule first, such as new critical findings with a known fix.
  5. Print a message that tells the developer what to do next.
gitleaks git --baseline-path baseline.json --report-path report.json

What you must maintain

ItemWhy it needs care
BaselineGoes stale as code changes
AllowlistsGrow without review unless owned
Rule thresholdsNeed tuning as noise changes
RuntimeSlow gates get skipped
FallbackDecide what happens when the scanner or its data source fails

Exceptions are the hard part

Developers will hit false positives. Give them a documented route: an inline marker or ignore file with a reason, reviewed by a named person, with an expiry date. Without it, they will find their own route around the gate.

Test the gate itself

Add a deliberately bad test change to a sandbox repository and confirm the gate blocks it. Run that check on a schedule so a broken gate is noticed.

Compare with a managed gate

A platform gate brings policy as configuration, exception workflow, and a fallback mode. You trade control for upkeep. If your rules are unusual, a custom script may fit better, and the two can coexist.

Safeguard covers this ground with SBOM and dependency scanning, reachability analysis, policies and gates, and compliance evidence automation. Autonomous fix PRs are off by default, with every category set to Manual. You can try a free scan at gold.safeguard.sh/scan.

Measure whether the gate works

Track three numbers: how often the gate blocks, how often a block is overridden, and how long the gate adds to a build. A gate that never blocks may be too lax. One that is overridden often is probably noisy. One that adds minutes will be bypassed. Review the numbers monthly and adjust the rules rather than leaving them as first written.

Try it yourself

Click through it at your own pace, or open it full screen. Open a failing pull request and see what a developer needs from the message to fix it.

Quick check

What should a first gate block?

A narrow rule such as new critical findings with a fix available, so developers learn to trust it.

What if the scanner is down?

Decide in advance whether to warn or block, and record the choice in your runbook.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.