When Should You Add Quality Gates to a CI Pipeline?
When to add each CI quality gate: signals it is time, staged order, SonarQube Sonar way thresholds on new code, and rollout without bypasses.
Introduce a quality gate in CI when a problem keeps reaching the main branch and a machine can check for it reliably. In practice that means once several people merge to a shared branch, once code ships to users, or once a compliance rule applies. Start with fast blocking checks, then add stricter ones only on new code.
A quality gate is a pass or fail decision in the pipeline: if the code does not meet the condition, it cannot move forward. This post is about timing, meaning when each type of gate is worth adding and when it does more harm than good. If you want a catalogue of specific gates for production pipelines, read our companion post on 10 CI/CD quality gates for production-level reliability. For the bigger picture of where CI sits in delivery, see what DevOps is.
Key takeaways
- A gate earns its place when the defect it catches is costly, recurring and detectable by a tool with few false alarms.
- Add gates in stages: build and unit tests first, then linting and security basics, then coverage and analysis on new code, then deployment-time policies.
- Gate new code, not the whole legacy codebase. SonarQube's default "Sonar way" gate works this way, with conditions such as 80% coverage and no more than 3% duplication on new code.
- A gate that people bypass every week is worse than no gate. Measure how often each gate blocks and why.
- The platform has to enforce gates. On GitHub, required status checks on protected branches or rulesets stop merges until checks pass.
What counts as a quality gate
SonarSource defines a quality gate as a set of conditions that code is measured against during analysis, with a pass or fail result telling you whether the project is ready to merge or release. The idea is broader than one tool. Any automated check whose failure stops the pipeline is a gate: a failing unit test, a lint error, a critical vulnerability, a Terraform plan that would delete a database, or a Kubernetes manifest that runs a privileged container.
What makes it a gate rather than a report is enforcement. A report says "coverage dropped". A gate says "this pull request cannot merge until coverage on new code is back above the threshold". Enforcement lives in the CI system and the Git platform. On GitHub, protected branches can require named status checks to pass before merging, with a strict mode that also requires the branch to be up to date with the base branch.
Signals that it is time to add a gate
Teams often add gates because a blog post said so. A better trigger is evidence from your own work. These signals suggest a gate would pay for itself.
The same kind of defect keeps coming back
If broken builds on main happen every week, a "build and unit tests must pass" gate is overdue. If the last three incidents came from a missing null check or an unhandled error, a static analysis rule for that pattern is worth trying. Look at your incident reviews and bug tracker: recurring categories are gate candidates.
More than a few people merge to the same branch
With two developers who talk all day, informal checks work. With ten across two time zones, nobody knows what the others merged. Shared branches need automated agreement on what "ready" means, and gates are that agreement written down.
Code reaches real users
Before launch, a broken build costs an afternoon. After launch, a broken release costs customers. The move to production is the natural moment to add gates for tests, security scans and deployment safety.
A compliance or contractual rule applies
If an audit asks you to prove that every change was reviewed and scanned, a gate is the simplest evidence. The pipeline log shows that the check ran and passed for each change.
Reviewers spend time on things a machine can check
If code review comments are mostly about formatting, naming or obvious bugs, automate those checks so reviewers can focus on design and logic.
Signals that it is too early, or the wrong gate
Gates have costs. Each one adds pipeline time, and every false failure teaches people to distrust the pipeline. Hold off when:
- The check is slow and flaky. A 40-minute end-to-end suite that fails randomly one run in five should not block merges until it is stable.
- The threshold is arbitrary. Demanding 90% coverage on a legacy codebase at 20% will be bypassed on day one.
- Nobody owns the gate. When it fails, someone must know how to read the result and fix it or tune it.
- You are in early prototyping where the code may be thrown away next week. Keep basic build checks and skip the rest.
A staged order for adding gates
| Stage | When to add | Gates | Typical time cost |
|---|---|---|---|
| 1. Basics | From the first shared repository | Compile or build, unit tests, required review | Minutes |
| 2. Hygiene | Once more than a few people contribute | Linting and formatting, secret scanning, dependency vulnerability check | Seconds to minutes |
| 3. Quality on new code | Before or at first production release | Static analysis gate, coverage on new code, duplication limits | A few minutes |
| 4. Infrastructure | As soon as infrastructure is in code | IaC scanning, plan review, destructive change detection | Minutes |
| 5. Deployment | When deploying to shared or production environments | Admission policies, smoke tests, canary metrics | Minutes, during rollout |
The order matters because early gates make later ones trustworthy. A static analysis gate is meaningless if the code does not build, and a canary analysis gate is noise if there are no smoke tests to show the service starts at all.
Stage 1: build, tests and review
These are the gates every shared repository needs. The build must succeed, unit tests must pass and at least one other person must approve the change. On GitHub you configure the required checks on the protected branch. GitLab, Bitbucket and Azure Repos have equivalent settings. Without these three, other gates rest on sand.
Keep this stage fast. If unit tests take more than ten minutes, people start batching changes to avoid the wait, which defeats the point of small, frequent merges. Split slow tests out or run them in parallel.
Stage 2: hygiene checks
Linting and formatting checks are cheap and remove a whole class of review comments. Secret scanning stops credentials from reaching the repository; GitHub's push protection, for example, blocks pushes that contain detected secrets. Dependency vulnerability checks warn when a library has a known issue.
A practical tip for dependency gates: block only on high or critical issues with an available fix. Blocking on every low-severity advisory, including ones with no fix, trains people to ignore the gate.
Stage 3: quality gates on new code
This is where tools like SonarQube come in. SonarQube's built-in "Sonar way" gate focuses on new code. According to SonarSource's documentation, it fails when new code introduces any new issues or gets a reliability, security or maintainability rating worse than A, when less than 100% of new security hotspots are reviewed, when coverage on new code is below 80%, or when duplicated lines in new code exceed 3%. (The docs note that security hotspots are being phased out, so expect that condition to change.)
The focus on new code is the reason this works on legacy projects. You do not have to fix ten years of debt before the gate turns green. You only have to keep each change clean, and the overall numbers improve over time as old code is touched.
To make the gate actually stop the pipeline, the CI job has to wait for the analysis result and fail when the gate fails. SonarSource documents how to do this per CI system (Jenkins, GitHub Actions, GitLab CI/CD, Azure Pipelines and others). Our scenario-based SonarQube questions cover common problems, such as a gate that passes locally but fails in CI.
Stage 4: infrastructure gates
Once infrastructure lives in Terraform, CloudFormation or Kubernetes manifests, it deserves its own gates. An IaC scanner can block public storage buckets or wide-open security groups. A plan check can stop a merge that would destroy or replace a stateful resource unexpectedly. Terraform's plan -detailed-exitcode returns 0 for no changes, 1 for an error and 2 for a non-empty diff, which a script can use to decide whether human review is needed.
terraform plan -detailed-exitcode -out=tfplan
code=$?
if [ "$code" -eq 1 ]; then echo "plan failed"; exit 1; fi
if [ "$code" -eq 2 ]; then echo "changes found, review required"; fi
Encryption and data handling checks also belong here. Our post on encryption methods that protect data in DevOps pipelines lists what to verify for secrets, artifacts and storage.
Stage 5: deployment gates
The last gates sit at the boundary with production. In Kubernetes, admission policies reject manifests that break your rules. ValidatingAdmissionPolicy has been stable since Kubernetes 1.30 and uses CEL expressions, for example to cap replicas or forbid privileged containers. After the rollout starts, smoke tests and canary metrics decide whether it continues.
Manual approval steps are also gates, but they are the weakest kind when used alone, because a busy approver tends to click through. Our post on the pitfalls of relying on manual approvals in CI/CD explains how to pair approvals with automated evidence.
How to roll out a new gate without a revolt
- Run it in report-only mode for one or two weeks. Collect how often it would have failed and why.
- Fix or tune the rules behind the most common false failures.
- Announce the date it becomes blocking, with a short guide on how to read and fix failures.
- Make it blocking on new code or new changes only.
- Give a documented, logged way to override in emergencies, and review every override.
- Check monthly: how many merges did it block, how many were real problems, and how much time did it add?
Step 6 is the one teams skip. A gate that blocks rarely and catches real problems when it does is healthy. A gate that blocks often and is overridden most of the time needs fixing or removing.
Common mistakes
Too many gates at once
Turning on eight new checks in one sprint produces a wall of red builds and an angry team. Add one or two at a time.
Gates that only run on the main branch
If gates run after merging, the bad change is already in. Run them on pull requests so authors fix problems before they land.
Gates nobody can run locally
Developers should be able to run the same linters, tests and scanners on their machine. Otherwise every fix needs a full pipeline round trip.
Thresholds that never change
Revisit thresholds as the codebase improves. A coverage threshold that was ambitious two years ago may now be too easy, or a duplication rule may flag generated code you should exclude.
Gate timing for different kinds of projects
The staged order above fits a typical web service, but the right moment for each gate shifts with the type of project.
Libraries and shared packages
Other teams depend on a library, so a mistake spreads quickly. Add API compatibility checks and tests across supported language versions early, even before the library has many users. Coverage gates matter more here than in an internal script, because callers use paths you may never exercise yourself.
Infrastructure repositories
For Terraform or Kubernetes configuration repositories, infrastructure scanning and plan review are the first gates after the basic syntax check. A wrong change can delete data or expose a network, and unit tests rarely exist, so the plan output and the scanner become your test suite.
Data and machine learning pipelines
Code quality gates still apply, but the most valuable gates often check data: schema validation, row counts within expected ranges and model evaluation scores that must not drop below the previous version. Add them when the pipeline starts feeding a product or a report people rely on.
Regulated systems
Where audits apply, gates are part of the evidence trail from the start. Keep pipeline logs and scan results for as long as your retention rules require, and make sure overrides are recorded with a reason and an approver.
Where to learn this hands-on
Pipelines with real gates are easiest to understand by building one. Our Jenkins training covers pipeline stages, test reporting and integrating scanners, in the Kharadi, Pune classroom and live online across India. For the test tools that feed many of these gates, see top DevOps tools for testing automation.
Frequently asked questions
What is a quality gate in a CI pipeline?
A quality gate is an automated pass or fail check in the pipeline. If the code does not meet the condition, such as passing tests or a minimum coverage on new code, the pipeline stops and the change cannot merge or deploy.
When should a team add its first quality gate?
As soon as more than one person merges to a shared branch. The first gates should be a successful build, passing unit tests and a required review.
What are the default SonarQube quality gate conditions?
The built-in Sonar way gate checks new code for no new issues, ratings of A, 100% of new security hotspots reviewed, at least 80% coverage and no more than 3% duplicated lines.
Should quality gates apply to legacy code?
Usually only to new or changed code. Gating the whole legacy codebase blocks every change until years of debt are fixed, so teams bypass the gate.
How do I make a gate block merges on GitHub?
Add the check as a required status check on a protected branch or in a ruleset. Merges are then blocked until the check reports success, or a skipped or neutral status.
Are manual approvals quality gates?
They are gates, but weak ones on their own. Pair approvals with automated evidence such as test results and scan reports so approvers judge facts rather than guess.
How many quality gates are too many?
There is no fixed number. If gates make the pipeline slow or are frequently overridden, you have too many or badly tuned ones. Track blocks, overrides and added time for each gate.
References
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0