Skip to main content

Command Palette

Search for a command to run...

DevSecOps CI Pipeline for a Python Application Using Jenkins, SonarQube, Trivy, and Docker

Updated
โ€ข4 min read
P
Welcome! Iโ€™m Prajwal P. I stand at the intersection of technology and efficiency, exploring the dynamic world of DevOps โš™๏ธ. From mastering Cloud infrastructure to orchestrating containers, I am passionate about automating the complex to create the simple. Join me as I document my learning curve, share technical insights, and navigate the ever-evolving landscape of software deployment.

A hands-on DevSecOps project demonstrating security-first CI/CD with Jenkins, SonarQube, and Trivy


Introduction

DevSecOps is no longer optional in modern software development. Organizations expect security, code quality, and automation to be part of the CI/CD pipeline from day one.

In this post, I share how I built a DevSecOps CI pipeline for a Python application using Jenkins, SonarQube, Trivy, and Docker. This project focuses on shift-left security, automated quality checks, and real-world DevSecOps practices used in production environments.

๐Ÿ”— GitHub Repository
https://github.com/Prajwal8651/Sonar-Trivy-Python-DevSecOps.git


Why DevSecOps Matters in CI/CD Pipelines

Traditional CI/CD pipelines focus mainly on build and deployment speed. However, without integrated security, vulnerabilities often reach later stages โ€” or worse, production.

This DevSecOps pipeline ensures:

  • Security checks run on every commit

  • Code quality issues are detected early

  • Vulnerabilities are visible before deployment

  • Automation replaces manual security reviews

This approach aligns with industry-standard DevSecOps workflows.


Tools and Technologies Used

This project combines widely used open-source tools:

  • Python for application development

  • Docker for containerization and environment consistency

  • Jenkins for CI pipeline orchestration

  • SonarQube for static code analysis and SAST

  • Trivy for container and dependency vulnerability scanning

  • GitHub for source control and webhook-based automation

  • Ubuntu (EC2) as the CI server host

Each tool plays a specific role in building a secure CI pipeline.


DevSecOps Pipeline Architecture (High-Level)

The pipeline follows an event-driven CI/CD model:

  1. Code is pushed to GitHub

  2. A GitHub webhook triggers Jenkins

  3. Jenkins starts the CI pipeline automatically

  4. Static code analysis runs using SonarQube

  5. The Python application is containerized using Docker

  6. Trivy performs vulnerability scanning

  7. Results are logged and preserved per build

This architecture ensures fast feedback and continuous security enforcement.


Python Application Overview

The Python application is intentionally lightweight, allowing the focus to remain on DevSecOps concepts rather than application complexity.

The app was initially tested on Ubuntu to validate functionality. It was then containerized to:

  • Standardize execution across environments

  • Enable container security scanning

  • Integrate seamlessly with Jenkins pipelines


Containerization and Security with Docker

Docker plays a critical role in this project by packaging the application and its dependencies into a single, portable unit.

From a DevSecOps perspective, Docker enables:

  • Predictable runtime behavior

  • Simplified CI execution

  • Image-level vulnerability scanning

  • Better isolation and dependency control


Jenkins Pipeline as Code (CI Automation)

The entire CI workflow is defined using Pipeline as Code, stored directly in the GitHub repository.

Benefits of this approach include:

  • Version-controlled CI logic

  • Reproducible pipeline executions

  • Easy collaboration and review

  • Clear audit trail for changes

This mirrors how CI/CD pipelines are managed in real DevSecOps teams.


Static Application Security Testing with SonarQube

SonarQube is integrated into the pipeline to perform static application security testing (SAST) for the Python codebase.

It continuously checks for:

  • Bugs and logic errors

  • Security vulnerabilities

  • Code smells

  • Maintainability and reliability issues

Results are reviewed through the SonarQube dashboard after each pipeline run, allowing early remediation.


Vulnerability Scanning with Trivy (CLI-Based)

Trivy is used as a CLI-first vulnerability scanner, making it ideal for automated CI environments.

In this pipeline, Trivy scans:

  • Application dependencies

  • Docker image layers

  • Base operating system components

Vulnerabilities are categorized by severity levels such as LOW, MEDIUM, HIGH, and CRITICAL, and are visible directly in Jenkins build logs.


Jenkins Build Logs and Security Traceability

Each Jenkins build stores:

  • Pipeline execution logs

  • SonarQube scan references

  • Trivy vulnerability findings

This provides:

  • Build-level traceability

  • Historical security insights

  • Audit-ready CI evidence

Such traceability is essential in security-conscious environments.


GitHub Webhook Integration for Event-Driven CI

Instead of relying on periodic polling, this pipeline uses a GitHub webhook to trigger Jenkins instantly on code changes.

This setup ensures:

  • Faster feedback loops

  • Efficient CI execution

  • Scalable automation

Event-driven CI is a core principle of modern DevSecOps pipelines.


Challenges Faced During Implementation

Some practical challenges encountered include:

  • Working with CLI-based security tools without dashboards

  • Managing Docker permissions inside Jenkins

  • Securely exposing Jenkins for webhook communication

Solving these issues helped reinforce real-world DevSecOps problem-solving skills.


Key DevSecOps Learnings

This project highlights several important DevSecOps principles:

  • Security must be integrated early (shift-left)

  • Automation is critical for consistency

  • CI pipelines should be version-controlled

  • CLI-based tools scale better in CI/CD environments

  • Traceability is essential for security auditing


Conclusion

This project demonstrates how a DevSecOps CI pipeline for a Python application can be built using Jenkins, SonarQube, Trivy, and Docker. By embedding security and quality checks directly into CI, the pipeline ensures that every code change is validated before moving forward.

This approach reflects how modern organizations build secure, reliable, and scalable software systems.


GitHub Repository

๐Ÿ”— https://github.com/Prajwal8651/Sonar-Trivy-Python-DevSecOps.git


More from this blog

Terraform on AWS

29 posts

Stop clicking in the AWS console. Start coding your infrastructure.