Jenkins 3-Tier DevSecOps Architecture
This repository is the CI/CD control repo, not the application. It holds the Jenkinsfile pipeline, an AWS setup guide, an architecture document, a one-off patch script and screenshots. The 3-tier app lives in a separate GitHub repo that the pipeline references.
1 Architecture
A push to GitHub triggers Jenkins on EC2-1, which orchestrates scanning, building, pushing and deploying. SonarQube calls back to Jenkins with a quality-gate webhook, and the verified image runs on a target EC2 as three tiers.
End-to-end flow
SonarQube calls back to Jenkins through the quality-gate webhook, so the pipeline can block on
waitForQualityGate(). Trivy hard-fails the build on HIGH or CRITICAL findings.
Sizing: at least 4 GB RAM and 12 GB storage. SonarQube's Lucene indexer needs
vm.max_map_count=262144.
2. Components
| Component | Role |
|---|---|
| GitHub | Source of truth; triggers Jenkins via webhook/SCM polling |
| Jenkins (EC2-1) | Orchestrator: runs stages, enforces gates, archives reports, notifies Slack |
| SonarQube (Docker, :9000) | SAST + code quality/coverage; owns the Quality Gate webhook |
| OWASP Dependency-Check | SCA: known CVEs in declared dependencies (NVD) |
| Trivy | Container image scan (OS + library CVEs) and filesystem/IaC scan (misconfigurations, secrets) |
| Docker + BuildKit | Image build with --secret so credentials never land in image layers |
| Registry (ECR/Docker Hub) | Stores the verified image with an immutable :git-sha tag |
| Target EC2 | Runs docker-compose for the 3 tiers (web/app/db) |
| AWS security groups | 22 SSH, 8080 Jenkins, 9000 SonarQube, 80/443 app |
3. Pipeline workflow (Jenkinsfile)
Twelve declarative stages run in fail-fast order. Red nodes in the animation are hard gates.
| # | Stage | What happens | Gate |
|---|---|---|---|
| 1 | Checkout | Clone main, capture short GIT_COMMIT |
— |
| 2 | Compile & Test | npm ci / mvn clean test / pip install |
Test failure stops build |
| 3 | Build Docker image | BuildKit --secret id=azkey, tags :latest + :GIT_COMMIT |
— |
| 4 | SonarQube Analysis (SAST) | sonar-scanner, sonar.projectKey=jenkins-3tier |
— |
| 5 | OWASP Dependency-Check (SCA) | dependency-check --scan . --format ALL, reports archived |
— |
| 6 | Trivy: container scan | trivy image --severity HIGH,CRITICAL --exit-code 1 + SARIF |
Hard fail on HIGH/CRITICAL |
| 7 | Trivy: filesystem/IaC scan | trivy fs, HTML report archived |
— |
| 8 | Sonar Quality Gate | waitForQualityGate(); non-OK raises error |
Blocks pipeline |
| 9 | Push to registry | docker login + push :GIT_COMMIT |
Only reached if 1–8 pass |
| 10 | Deploy via SSH | ssh ubuntu@EC2_HOST → docker-compose pull && up -d |
— |
| 11 | Smoke test | Poll http://EC2_HOST/health up to 12×5 s |
Rollback signal |
| 12 | Post | Always archive reports + cleanWs(); Slack on success/failure/unstable |
— |
Flow with gates
4. Key security properties
Pipeline guarantees
- Scan before push: no unscanned image ever reaches the registry.
- Gate before deploy: the SonarQube Quality Gate and Trivy severity threshold are hard stops.
- No baked secrets: BuildKit
--secret, neverCOPYof.env.
Audit trail (archived per build)
dependency-check-report.*trivy-*.html/.sarifsonar-report.json
5. Repo files
| File | Purpose |
|---|---|
Jenkinsfile |
The declarative 12-stage pipeline (the core artifact) |
README.md |
Step-by-step EC2/Docker/Jenkins/SonarQube/Trivy/OWASP setup, plus a simpler inline pipeline variant |
docs/DEVSECOPS_ARCHITECTURE.md |
Full written architecture, security strategy, AWS networking/IAM notes |
fix_jenkins.py |
One-off patch script fixing credential handling in the Jenkinsfile (withCredentials,
GString interpolation) |
*.png |
Screenshots: workflow, Trivy, OWASP, quality gate, results |