DevSecOps pipeline with Jenkins
A Jenkins pipeline on AWS EC2 that stops on a failed SonarQube quality gate, scans source and image with Trivy, then deploys the container and health-checks it.
- Type
- Personal project
- Year
- 2026
- Status
- Finished exercise
- Stack
The problem
A structured DevOps exercise: take a small Express API from a commit to a running container, with security checks built into the release path rather than run as a separate report.
Make code quality, vulnerability scanning and a post-deploy check part of every build, so problems are visible before — not after — the container is running.
My role
The repository holds my Jenkinsfile, Dockerfile and a step-by-step guide to setting up the Jenkins and SonarQube servers on EC2.
Approach
The pipeline is the release gate: every check runs in the same declarative pipeline and can stop it, in a fixed order — tool checks, quality gate, file-system scan, image build and scan, then deploy and health check.
Architecture
Two Ubuntu EC2 instances: one runs Jenkins with Docker and Trivy, the other runs SonarQube. Jenkins sends analysis to SonarQube and waits for its webhook with the quality-gate result before continuing.
- GitHub repository
- Jenkins
- Trivy
- Docker
- SonarQube
- NodeAPI container
Request flow
Technical decisions
- SonarQube quality gate
- Turns code-quality findings into a pass/fail decision on the release.
- Trivy
- One scanner for vulnerable dependencies, secrets and misconfiguration in both source and image.
- Separate SonarQube server
- Keeps SonarQube’s memory needs away from the build agent.
- node:22-alpine, non-root
- A small base image and an unprivileged user reduce what an attacker gets from a compromised process.
Implementation
The Jenkinsfile is a declarative pipeline of twelve stages. Trivy runs as a container with the vulnerability database cached on the agent, so scans don’t download it every time.
The Dockerfile installs production dependencies with npm ci, runs as an unprivileged user and declares a HEALTHCHECK against /health.
Challenges and solutions
Challenge
SonarQube analyses asynchronously, so a pipeline can move on before the quality-gate result exists.
Solution
The pipeline waits for SonarQube’s webhook with waitForQualityGate and abortPipeline: true.
Challenge
Trivy downloads its vulnerability database on every run.
Solution
Trivy runs as a container with the database cached on the agent.
Challenge
A container can start and still not serve requests.
Solution
The Dockerfile declares a HEALTHCHECK, and the pipeline calls /health after deploying and fails the build if it does not pass.
Challenge
SonarQube’s memory needs compete with builds on the same machine.
Solution
SonarQube runs on its own EC2 instance and reports back to Jenkins.
Security and reliability
- waitForQualityGate abortPipeline: true stops the release when SonarQube’s gate fails.
- The container runs as a non-root user.
- Security groups limit SSH and the Jenkins and SonarQube interfaces to known addresses, as documented in the setup guide.
Testing
The health-check stage is the automated verification: the build fails if the deployed container does not answer on /health. The sample API has no unit tests.
Trade-offs
Choice
A separate SonarQube server
What it costs
Two instances to provision, secure and pay for instead of one.
Choice
Deploying with docker run on the Jenkins server
What it costs
Simple to follow, but there is no separate environment and no rollback step.
Choice
Scanner images on the latest tag
What it costs
Always the newest rules, but results can change between runs without a code change.
Results
An end-to-end pipeline in which code quality and runtime health gate the deployment and both source and image are scanned on every build.
Lessons and next steps
Known limitations
- Trivy reports findings of every severity but is not configured with --exit-code, so vulnerabilities are visible in the log without failing the build.
- Deployment is a docker run on the Jenkins server itself; there is no separate environment or rollback step.
- Trivy and SonarQube images use the latest tag, so results can change between runs without a code change.
What I would change next
- Make Trivy fail the build on high and critical findings with --exit-code.
- Deploy to a separate environment with a rollback step.
- Pin scanner image versions so results only change when the pipeline does.
Next projectRetail microservices platform