Certified Jenkins Engineer

Code Quality and Testing

SonarQube Intro

In this lesson, we explore Static Application Security Testing (SAST)—commonly referred to as static analysis—and demonstrate how SonarQube empowers developers to identify security flaws and code-quality issues early in the software development lifecycle (SDLC).

What Is Static Analysis?

Static analysis inspects your application’s source code without executing it. By scanning for vulnerabilities, code smells, and structural issues, it acts as an automated gatekeeper that flags potential problems before they reach production.

Note

Static analysis works across multiple programming languages and frameworks, providing instant feedback within your IDE or CI/CD pipeline.

Introducing SonarQube

SonarQube is an open-source code quality and security inspection platform from SonarSource. It integrates seamlessly with popular build tools and continuous integration systems, offering:

  • Automated code reviews with actionable guidance
  • Live feedback on new code in pull requests
  • Customizable rule sets to enforce best practices

Key Benefits of Static Analysis with SonarQube

BenefitDescription
Early Defect DetectionCatch bugs and security flaws on commit, slashing remediation time and cost.
Consistent Coding StandardsApply team-specific rules automatically to maintain code uniformity and readability.
Code Structure InsightsIdentify hotspots for refactoring, improve maintainability, and reduce technical debt.
Security Vulnerability ScanReveal common security issues—such as SQL injection or cross-site scripting—before deployment.

By regularly analyzing your code, SonarQube helps you prioritize and address issues, driving better software quality.

Pinpointing Security Issues

SonarQube drills down to the exact line of code where risks appear, providing clear remediation steps.

The image shows a SonarQube interface highlighting a security issue in an HTML file, suggesting the addition of "lang" and/or "xml:lang" attributes to the `<html>` element.

Quality Gates & Key Metrics

Quality Gates define pass/fail conditions that help you enforce quality and security thresholds automatically in your CI pipeline.

MetricPurposeExample Threshold
Code SmellsFlags maintainability issues (e.g., unused code)< 5%
Security HotspotsHighlights fragments requiring security review0 unresolved
Code CoveragePercentage of code exercised by automated tests≥ 80%
Duplications (%)Measures duplicated code blocks to reduce redundancy< 3%

Warning

If any Quality Gate condition fails—such as coverage dropping below the defined threshold—the build will be marked as failed. Ensure you resolve highlighted issues to keep your pipeline green.

The image outlines quality standards for code with a table of conditions on new code, including metrics like coverage and maintainability, and descriptions of code smells, security hotspots, and code coverage.

Enforcing Quality Gates

When a Quality Gate is violated, SonarQube prevents deployments until violations are resolved, integrating with CI servers like Jenkins, GitLab CI, or GitHub Actions.

The image shows a notification about enforcing quality standards with quality gates, indicating a failed status due to unmet conditions, specifically mentioning condition coverage being less than 80%.

Conclusion

Integrating SonarQube into your CI/CD pipeline delivers continuous code quality and security insights. By automating checks and enforcing Quality Gates, you can:

  • Prevent critical issues from reaching production
  • Maintain high standards across your codebase
  • Streamline development with real-time feedback

By adopting SonarQube as part of your SDLC, you drive more reliable, secure, and maintainable software.

Watch Video

Watch video content

Previous
Demo String Interpolation