SonarQube & Trivy Security Quality Gates: Production DevSecOps Pipelines
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:
- SonarQube: SAST (Static Application Security Testing), code smell analysis, test coverage enforcement, and OWASP Top 10 code checks.
- Aqua Trivy: SCA (Software Composition Analysis) for vulnerable open-source dependencies and comprehensive Docker container image scanning.
1. The DevSecOps Shift-Left Pipeline Architecture
Section titled β1. The DevSecOps Shift-Left Pipeline Architectureβ[ 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 CVEs3. Configuring SonarQube Enterprise Quality Gates
Section titled β3. Configuring SonarQube Enterprise Quality GatesβA Quality Gate is a set of Boolean conditions a project must meet before it can be merged into production:
Recommended Strict Production Standards:
Section titled βRecommended Strict Production Standards:β- 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.
Best Practice .trivyignore Management
Section titled βBest Practice .trivyignore ManagementβNever disable security scanners. Instead, audit and record justifiable exceptions using a tracked .trivyignore file:
# CVE-2023-XXXX: Only impacts Windows kernel, our container runs on Alpine LinuxCVE-2023-12345 exp:2026-12-31
# CVE-2024-YYYY: Upstream maintainer has not released patch, compensating WAF rule activeCVE-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
The Incident:
Section titled βThe Incident:β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.
The Automated Defense:
Section titled βThe Automated Defense:β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.