Skip to content
About Me
----

SonarQube & Trivy Security Quality Gates: Production DevSecOps Pipelines

Architect Key Takeaways

Master automated DevSecOps security gates in GitHub Actions using SonarQube for Static Application Security Testing (SAST) and Aqua Trivy for Software Composition Analysis (SCA) and container vulnerability scanning.

SonarQube & Trivy Security Quality Gates: Production DevSecOps Pipelines

Section titled β€œSonarQube & Trivy Security Quality Gates: Production DevSecOps Pipelines”

In high-velocity engineering organizations, pushing code without automated security gates creates critical vulnerabilities that reach production.

Traditional organizations rely on manual quarterly audits. Modern DevSecOps embeds automated security gates into every single pull request, breaking the build before vulnerable code or infected container images can merge into main.

This handbook provides the complete, production-grade GitHub Actions pipeline integrating:

  1. SonarQube: SAST (Static Application Security Testing), code smell analysis, test coverage enforcement, and OWASP Top 10 code checks.
  2. Aqua Trivy: SCA (Software Composition Analysis) for vulnerable open-source dependencies and comprehensive Docker container image scanning.

[ Developer Pull Request ]
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ GitHub Actions CI Pipeline β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Step 1: SonarQube SAST Scanner β”‚ β”‚
β”‚ β”‚ β€’ Code Smells, Bugs, Security Hotspots β”‚ β”‚
β”‚ β”‚ β€’ Quality Gate Webhook Wait (Exit 1 on failure) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ Passed β”‚
β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Step 2: Trivy Dependency (SCA) Scanner β”‚ β”‚
β”‚ β”‚ β€’ Scans lockfiles (package-lock.json, pom.xml) β”‚ β”‚
β”‚ β”‚ β€’ Blocks Critical / High CVEs with fixes β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ Passed β”‚
β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Step 3: Docker Build & Trivy Image Scanner β”‚ β”‚
β”‚ β”‚ β€’ Multi-stage minimal scratch/distroless build β”‚ β”‚
β”‚ β”‚ β€’ Scans OS packages & secrets in image layers β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ Passed β”‚
β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Step 4: Sign & Push to Registry (Cosign + ECR) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

2. Production GitHub Actions Workflow (devsecops-pipeline.yml)

Section titled β€œ2. Production GitHub Actions Workflow (devsecops-pipeline.yml)”
name: Production DevSecOps Security Gate
on:
pull_request:
branches: [main]
push:
branches: [main]
jobs:
sast-and-sca:
name: SonarQube SAST & Trivy Dependency Scan
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history required for SonarQube blame analysis
# ---------------------------------------------------------
# 1. SonarQube Static Application Security Testing (SAST)
# ---------------------------------------------------------
- name: Run SonarQube Scan
uses: sonarsource/sonarqube-scan-action@v3
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
with:
args: >
-Dsonar.projectKey=enterprise-payments-api
-Dsonar.sources=src
-Dsonar.exclusions=**/*.spec.ts,**/*.test.js
-Dsonar.qualitygate.wait=true
# ---------------------------------------------------------
# 2. Aqua Trivy Dependency (SCA) File Scan
# ---------------------------------------------------------
- name: Scan Filesystem & Dependencies with Trivy
uses: aquasecurity/trivy-action@master
with:
scan-type: 'fs'
scan-ref: '.'
format: 'table'
severity: 'CRITICAL,HIGH'
ignore-unfixed: true
exit-code: '1' # Fails the PR if High/Critical CVEs exist
container-security:
name: Container Build & Image Vulnerability Scan
runs-on: ubuntu-latest
needs: sast-and-sca
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build Container Image Locally
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: internal/payments-api:${{ github.sha }}
# ---------------------------------------------------------
# 3. Trivy Container Image Vulnerability Scan
# ---------------------------------------------------------
- name: Scan Docker Image for Known CVEs & Exposed Secrets
uses: aquasecurity/trivy-action@master
with:
image-ref: 'internal/payments-api:${{ github.sha }}'
format: 'table'
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
ignore-unfixed: true
exit-code: '1' # Break build if container image has exploitable CVEs

A Quality Gate is a set of Boolean conditions a project must meet before it can be merged into production:

  • 0 Security Vulnerabilities: Any vulnerability with severity High or Blocker fails the gate immediately.
  • 0 Security Hotspots: All security-sensitive APIs (e.g., hardcoded cryptographic ciphers, raw SQL queries) must be reviewed by a human engineer.
  • New Code Coverage > 80%: Unit tests must cover at least 80% of lines modified in the pull request.
  • Duplicated Lines < 3%: Rejects copy-paste debt before it multiplies.

4. Trivy Hardening: Ignoring Unfixed CVEs & Policy Enforcement

Section titled β€œ4. Trivy Hardening: Ignoring Unfixed CVEs & Policy Enforcement”

In real-world operations, zero vulnerabilities is impossible because some third-party packages have discovered CVEs without an upstream patch yet.

Never disable security scanners. Instead, audit and record justifiable exceptions using a tracked .trivyignore file:

.trivyignore
# CVE-2023-XXXX: Only impacts Windows kernel, our container runs on Alpine Linux
CVE-2023-12345 exp:2026-12-31
# CVE-2024-YYYY: Upstream maintainer has not released patch, compensating WAF rule active
CVE-2024-98765 exp:2026-06-30

🧠 Production Architecture Scenario: The Pre-Holiday Log4j Nightmare

Section titled β€œπŸ§  Production Architecture Scenario: The Pre-Holiday Log4j Nightmare”
Scenario: Zero-Day RCE Blocked at the PR Gate

When the critical Log4Shell (CVE-2021-44228) zero-day was announced, dozens of microservices in a retail company contained nested Java dependencies running vulnerable versions.

Because the team had Trivy integrated into their GitHub Actions CI with severity: 'CRITICAL' and exit-code: '1', automated Dependabot pull requests attempting to deploy updates were validated immediately. Any engineer attempting to merge a hotfix that missed a transitive dependency was stopped automatically by the Trivy check. The team remediated all 40 services in under 3 hours without a single compromised pod.