How to add SAST to your CI/CD pipeline with quality gates that block risky builds
Put SAST in your CI/CD pipeline the right way: pre-commit vs pipeline scans, severity thresholds that block builds, baselines, exceptions and a rollout plan.
Putting SAST in your CI/CD pipeline means every change is scanned for security flaws automatically, and a quality gate stops the build when the results cross a threshold you have agreed — for example, any critical vulnerability. Done well, developers get feedback within minutes and vulnerable code never reaches production; done badly, the gate becomes noise that everyone learns to bypass.
This guide walks through where to scan, how to design gates that block the right things, how to handle the existing backlog and exceptions, and a rollout plan you can follow over a few sprints. If you are new to the topic, start with what SAST is and how it works.
Pre-commit or pipeline scanning: which do you need?
Both, because they do different jobs.
| Pre-commit scan | Pipeline scan | |
|---|---|---|
| Where it runs | Developer’s machine, before git commit |
CI server, on push or pull request |
| Speed of feedback | Seconds; the developer is still in context | Minutes; arrives with the pull request |
| Scope | Changed files | Whole project or the change set |
| Can it be skipped? | Yes (--no-verify, missing hook) |
Not without changing pipeline config, which is reviewed |
| Best at | Stopping secrets entering Git history; quick fixes | Enforcing policy; producing audit evidence |
Pre-commit scanning is the fastest loop. Its biggest win is secrets: once an API key or database password is committed and pushed, it lives in history and in every clone. Catching it before the commit is far cheaper than rotating the credential afterwards.
Pipeline scanning is the control. It runs whatever the developer’s laptop does, it is the same for everyone, and its results can be stored as evidence for each release.
What should a SAST quality gate block?
A gate is only useful if developers trust it. That means blocking on findings that are serious and likely to be real, and reporting everything else.
A sensible starting policy:
- Block on any new finding with severity critical.
- Block on any hard-coded secret, regardless of severity score.
- Warn (comment on the pull request, do not block) on new high findings.
- Report medium and low findings and code-quality issues to the dashboard for planned clean-up.
Over time, as the team gets used to the gate and false positives are tuned out, move high findings from warn to block.
Two details matter more than they appear:
- “New” findings. Gate on what the change introduces, compared with a baseline. Otherwise one legacy module with 300 old issues blocks every pull request in the repository.
- The fix, not just the failure. A failed build that says only “SQL injection, line 214” sends the developer to a search engine. A gate that explains the issue and suggests the patch gets it fixed in the same sitting.
A worked example: one pull request through the gate
Consider a developer at a Mumbai NBFC, working on loan-origination in a repository on git.contoso.in.
- She adds an endpoint that looks up an applicant by PAN and, in a hurry, builds the SQL query by concatenating strings. She also pastes a test API key into a config file.
- Pre-commit: the local scan flags the API key. She moves it to the secrets manager before committing. The SQL issue is reported as well, but she commits anyway to share work in progress.
- Pipeline: on push, the SAST stage scans the change, finds a new critical SQL injection, and fails the build. The pull request shows the finding, the line, and a suggested parameterised query.
- She applies the fix, pushes again, and the gate passes. Two medium code-quality findings are reported but do not block.
- The scan results for the merged commit are stored with the build, so an auditor can later see that the release was scanned and what the gate decided.
Total delay: one extra push. The vulnerability never reached the main branch.
How to wire SAST into the pipeline
The exact syntax depends on your CI system and your SAST tool, but the shape is the same everywhere. An illustrative stage, written in generic pipeline YAML:
stages: [test, sast, build, deploy]
sast:
stage: sast
script:
- sast-scan --project loan-origination --baseline main --report sast-report.json
- sast-gate --report sast-report.json --fail-on new:critical,secret
artifacts:
paths: [sast-report.json]
rules:
- if: merge_request
- if: branch == "main"
(sast-scan and sast-gate stand in for your tool’s own commands.) Whatever tool you use, check that the stage:
- Runs before the build artefact is produced, so a failing scan never yields a deployable package.
- Exits non-zero when the gate fails, so the CI system actually stops.
- Keeps the report as a build artefact for evidence and trend reporting.
- Runs on both pull requests and the main branch, so nothing is merged unscanned and the main branch always has a current result.
Handling the backlog without stopping all work
Nearly every first scan of an existing codebase returns a large number of findings. Do not try to clear them before turning on the gate.
- Run a baseline scan of the main branch and record the result.
- Triage the criticals with a security engineer and the code owner. Each is either fixed, scheduled, or accepted as a risk.
- Gate on new findings only, measured against the baseline.
- Plan backlog work like any other technical debt — a fixed share of each sprint, tracked per repository.
- Watch the trend. The number of open critical and high findings per repository should fall release by release.
How should exceptions work?
Some findings will be false positives or accepted risks. If the only way to get past the gate is to switch it off, people will switch it off. Build a proper exception path:
- Exceptions are raised against a specific finding, not a whole rule or repository
- Each exception records a reason and an approver from security, not only the developer
- Exceptions have an expiry or review date
- False positives feed back into rule tuning, so the same noise does not recur
- Changes to the gate configuration itself go through code review
This also gives auditors exactly what they ask for: evidence that the control exists, that it operates, and that deviations are approved and recorded.
How often should you scan?
Every push to a branch with an open pull request, and every merge to main. Scheduled full scans (nightly or weekly) are useful on top, because rules improve and a new rule may find an old issue.
The real-world obstacle is often not technology but licensing. When a SAST tool is priced per scan, per lines of code scanned or per seat, teams start rationing: scanning only before a release, or only some repositories. That defeats the purpose of a pipeline gate. When you evaluate tools, check that the licence lets you scan as often as your pipeline runs. Our SAST tools buyer’s guide covers this in more detail.
What regulators and auditors expect
You do not need a regulation to justify a SAST gate, but it helps to know the context. NIST’s Secure Software Development Framework (SP 800-218) includes reviewing and analysing code to identify vulnerabilities as a core practice. In India, SEBI’s Cybersecurity and Cyber Resilience Framework asks regulated entities to obtain SBOMs for software used in core and critical activities, and bank and insurer audits routinely ask how application code is tested before release. A pipeline gate with stored reports answers that question with evidence rather than assurances.
Rollout plan: four sprints
- Sprint 1 — measure. Add the SAST stage in report-only mode to two or three repositories. Add pre-commit hooks for volunteers.
- Sprint 2 — tune. Review false positives, adjust rules, set baselines, agree the exception process.
- Sprint 3 — enforce. Block on new critical findings and secrets. Communicate the change and the exception path.
- Sprint 4 — expand. Roll out to remaining repositories, start backlog work, and move high findings to warn.
Where Ayati Code fits
Ayati Code was built for exactly this workflow. It scans before code enters the pipeline or as it is pushed, lets you set the severity threshold that blocks the build, explains each issue and suggests the patch, and reports code quality alongside security in the same scan. Its licence allows unlimited scans with no caps on scan frequency, so the gate can run on every push. It can also generate SBOM, CBOM and QBOM reports for audit files.
To see a quality gate running on one of your own repositories, request a demo.
Frequently asked questions
Where should SAST run in a CI/CD pipeline?
Run it on every push or pull request, before the build artefact is produced and long before deployment. Add a lighter pre-commit scan on developer machines so most issues are fixed before they reach the pipeline at all.
What is a SAST quality gate?
A quality gate is a rule that decides whether a build can proceed based on scan results — for example, fail the build if there is any new critical finding. It turns a report that people may ignore into a control that cannot be skipped by accident.
Should SAST block the build or only report?
Start in report-only mode for a short period to measure noise, then block on new critical findings. Blocking only on new issues avoids stopping all work because of an old backlog, while still preventing fresh vulnerabilities from being merged.
Is pre-commit scanning enough on its own?
No. Pre-commit hooks run on developer machines and can be skipped or misconfigured, so they are a convenience, not a control. The pipeline scan is the enforceable gate; pre-commit simply makes it pass more often.