Skip to main content

Netflix Jenkins Pipeline

Published 12 min readMarkdown

Here's a detailed stage-by-stage breakdown of your Jenkins pipeline, explaining why each stage is used in real-world DevSecOps pipelines and how it helps with security, quality, and automation.


This is the root of a Declarative Jenkinsfile. It defines all the stages, environment variables, post-actions, etc.


pipeline {
    agent any

    environment {
        SCANNER_HOME = tool 'sonar-scanner'
    }

โœ… Stage: Clean Workspace

stage('Clean Workspace') {
    steps {
        cleanWs()
    }
}

๐Ÿ”น Purpose:

  • Ensures a clean working directory before starting the build.

  • Removes leftover files from previous runs.

๐Ÿ›ก๏ธ Real-World Security/Quality Reason:

  • Prevents cache poisoning, file conflict issues, or using old binaries.

  • Ensures the pipeline is idempotent (same input = same output).


โœ… Stage: Git Checkout

stage('Git Checkout') {
    steps {
        git branch: 'main', 
        url: 'https://github.com/AungAung-Drlinuxer/nextflix.git'
    }
}

๐Ÿ”น Purpose:

  • Fetches the source code from GitHub (main branch).

๐Ÿ›ก๏ธ Real-World Usage:

  • In real projects, you automate CI/CD based on commits to Git (push or PR triggers).

  • Always use verified repositories and SSH or tokens instead of open HTTPS for production.


โœ… Stage: SonarQube Analysis

stage('SonarQube Analysis') {
    steps {
        withSonarQubeEnv('sonar-server') {
            sh '''$SCANNER_HOME/bin/sonar-scanner \
                -Dsonar.projectName=Netflix \
                -Dsonar.projectKey=Netflix'''
        }
    }
}

We have used the SonarQube static code analysis process on our source code repository. It automatically scans the code to find issues related to quality, security, and maintainability. Nowadays, SonarQube is widely used in DevOps pipelines to ensure code quality before merging or deploying.

Scanning the source code for bugs, code smells, duplications, security hotspots, and OWASP Top 10 issues.

๐Ÿ” 1. Bugs

These are potential errors in the code that could cause the application to behave incorrectly or even crash.

  • โ— Example: Null pointer dereference, logic errors, unreachable code.

  • โœ… Goal: Prevent runtime failures and incorrect application behavior.

  • ๐Ÿง  SonarQube uses rules to detect patterns that indicate incorrect logic or operations.


๐Ÿ’จ 2. Code Smells

Code smells are indicators of poor design or coding practices that make the code harder to understand or maintain.

  • โ— Example: Long methods, duplicated logic, unused variables, magic numbers, deeply nested code.

  • โœ… Goal: Improve readability, maintainability, and reduce technical debt.

  • ๐Ÿง  Code smells usually wonโ€™t break your code but will make it harder to modify or scale.


๐Ÿ“„ 3. Duplications

SonarQube identifies duplicated blocks of code across files and modules.

  • โ— Example: Copy-pasted logic in different files or functions.

  • โœ… Goal: Encourage DRY (Donโ€™t Repeat Yourself) principle and easier maintenance.

  • ๐Ÿ” Helps in reducing bugs caused by updating one copy but missing others.


๐Ÿ” 4. Security Hotspots

These are sensitive parts of the code that require manual review to ensure they are secure.

  • โ— Example: Custom authentication mechanisms, use of eval(), insecure deserialization.

  • โœ… Goal: Alert developers to places where a security vulnerability might exist.

  • ๐Ÿ›ก๏ธ Not always a vulnerability, but a place where misuse could lead to security issues.


๐Ÿ›‘ 5. OWASP Top 10 Issues in Source

SonarQube can detect code that might be vulnerable to issues listed in the OWASP Top 10 โ€” the most critical web application security risks.

Examples from OWASP Top 10 include:

  • A01: Broken Access Control

  • A02: Cryptographic Failures

  • A03: Injection (e.g., SQL, OS, LDAP)

  • A05: Security Misconfiguration

  • A07: Identification and Authentication Failures

SonarQube rules map to these risks and try to flag:

  • Unsafe use of user inputs

  • Improper validation

  • Misconfigurations in security headers or auth flows

  • Use of outdated or insecure libraries

๐Ÿ› ๏ธ How It Works Technically:

  1. Scanner runs on our codebase (locally or in CI/CD).

  2. Source code is parsed and tokenized.

  3. Static analysis engines apply rules from quality profiles.

  4. Results are pushed to the SonarQube server (or SonarCloud).

  5. Developers get a dashboard view with metrics, hotspots, and suggestions.


โœ… Stage: Quality Gate

stage('Quality Gate') {
    steps {
        script {
            waitForQualityGate abortPipeline: false, credentialsId: 'SonarQube-Token'
        }
    }
}

A Quality Gate in SonarQube is a set of conditions or thresholds that your code must meet to be considered "healthy" or acceptable.

Think of it like a checkpoint or go/no-go decision in your CI/CD pipeline. If your code passes the gate, it's good to proceed (merge, deploy, etc). If it fails, the build/pipeline should stop โ€” just like a red traffic light.


๐Ÿ”ง What it does (in CI/CD):

๐Ÿ’ฌ "Waits for SonarQubeโ€™s Quality Gate result. Can pass or fail the build based on predefined rules."

This means that after SonarQube scans the code:

  • It calculates metrics (bugs, code smells, coverage, vulnerabilities, etc.).

  • It evaluates those metrics against the rules you set in the Quality Gate.

  • The pipeline waits for that evaluation result.

  • The build passes or fails depending on whether the gate is passed or failed.


โœ… Stage: OWASP Dependency Check

stage('OWASP Dependency Check') {
    steps {
        dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
        dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
    }
}

OWASP Dependency-Check is an open-source software composition analysis (SCA) tool that scans your projectโ€™s third-party dependencies (like libraries from npm, Maven, pip, etc.) for known security vulnerabilities โ€” specifically, CVEs (Common Vulnerabilities and Exposures).

It's part of the OWASP Project, aiming to help developers and DevOps teams identify risky libraries early.


๐Ÿ’ฌ Explanation of this line:

"Scans project dependencies (e.g., npm, pip, Maven) for known CVEs (Common Vulnerabilities and Exposures)."

This means:

  • It analyzes your dependency list (from package.json, requirements.txt, pom.xml, etc.).

  • Then it checks those libraries against public vulnerability databases like:

    • NVD (National Vulnerability Database)

    • GitHub Advisory Database

    • OSS Index (optional integrations)

  • If a dependency has a known CVE, it flags it, showing:

    • CVE ID (like CVE-2023-12345)

    • Severity (CVSS score)

    • Affected version

    • Fix recommendation (if available)


๐Ÿ“ฆ Supported Package Managers:

OWASP Dependency-Check supports many ecosystems, including:

EcosystemExample Files
Java (Maven, Gradle)pom.xml, build.gradle
JavaScript (npm)package.json, package-lock.json
Python (pip)requirements.txt, setup.py
.NET (NuGet).csproj, .sln
RubyGemfile.lock
PHP (Composer)composer.lock

โœ… Stage: Trivy File Scan

stage('Trivy File Scan') {
    steps {
        script {
            try {
                sh "trivy fs . > trivyfs.txt"
            } catch(Exception e) {
                input(message: "Are you sure to proceed?", ok: "Proceed")
            }
        }
    }
}

Absolutely! Let's break it down ๐Ÿ‘‡


๐Ÿ” What is Trivy File Scan?

Trivy (by Aqua Security) is an open-source security scanner. One of its features is file system scanning, also known as Trivy File Scan, where it scans a directory or a file on your system for secrets, vulnerabilities, and misconfigurations โ€” without needing to run a container.


๐Ÿ’ฌ Explaining This Line:

"Performs file system scanning for secrets, vulnerabilities, misconfigs, etc."

That means Trivy will:

โœ… 1. Detect Secrets

  • Scans files (like .env, config files, code) for hardcoded secrets such as:

    • AWS keys

    • API tokens

    • Private keys

    • Passwords

  • Example:

      AWS_SECRET_ACCESS_KEY=abcd1234...
    

โœ… 2. Detect Vulnerabilities

  • Scans for known vulnerable OS packages or libraries in the scanned directory.

  • For example, if it finds an old version of openssl, it will check for CVEs (like CVE-2023-0286).

  • Useful when scanning unpacked Docker images or a directory with dependencies.


โœ… 3. Detect Misconfigurations

  • Scans IaC (Infrastructure-as-Code) files:

    • Dockerfiles

    • Kubernetes YAML

    • Terraform (.tf)

    • Helm charts

  • Example findings:

    • containers running as root

    • no resource limits set

    • insecure file permissions


๐Ÿงช How to Use Trivy File Scan

Simple CLI command:

trivy fs ./my-app

That tells Trivy to scan everything under the my-app directory.


๐Ÿ“‚ Example Output:

Secrets:
  [AWS Secret] found in config/.env

Misconfigurations:
  [HIGH] Dockerfile - Container runs as root

Vulnerabilities:
  [CVE-2023-4567] openssl-1.0.2 (HIGH)

๐Ÿง  Use Cases:

  • โœ… Local development: check for secrets or misconfigs before committing.

  • โœ… CI/CD pipelines: block insecure code from being deployed.

  • โœ… Security audits: scan codebases or extracted Docker images.

  • โœ… SAST + IaC scanning in one tool.


๐Ÿ“Œ Summary Table

Scan TypeWhat It Checks
๐Ÿ” SecretsHardcoded secrets (AWS keys, tokens, passwords)
๐Ÿ›ก๏ธ VulnerabilitiesOS packages & libs for known CVEs
โš™๏ธ MisconfigurationsIaC, Dockerfiles, K8s, Terraform issues
๐Ÿ“ ScopeAny folder, file, or directory on your filesystem


โœ… Stage: Docker Build Image

stage('Docker Build Image') {
    steps {
        withCredentials([string(credentialsId: 'NETFLIX_API_KEY', variable: 'API_KEY')]) {
            sh "docker build --build-arg API_KEY=$API_KEY -t netflix ."
        }
    }
}

๐Ÿ”น Purpose:

  • Builds the application Docker image using the Dockerfile.

๐Ÿ›ก๏ธ Real-World Practice:

  • You may inject secrets securely (e.g., via Vault, not hardcoded).

  • Use multi-stage builds to reduce image size and attack surface.

  • Ensure Dockerfile is linted and has no latest tags in FROM.


โœ… Stage: Trivy Image Scan

stage('Trivy Image Scan') {
    steps {
        sh "trivy image netflix > trivyimage.txt"
        script {
            input(message: "Review Trivy Image scan. Proceed to Push?", ok: "Proceed")
        }
    }
}

๐Ÿ”น Purpose:

  • Scans the built Docker image for known vulnerabilities.

๐Ÿ›ก๏ธ DevSecOps Usage:

  • Container image scanning is vital.

  • Prevents publishing images with critical CVEs to public or private registries.


โœ… Stage: Docker Push

stage('Docker Push') {
    steps {
        script {
            withDockerRegistry(credentialsId: 'docker') {
                sh "docker tag netflix aungaungmyomyint/netflix:latest"
                sh "docker push aungaungmyomyint/netflix:latest"
            }
        }
    }
}

๐Ÿ”น Purpose:

  • Pushes Docker image to Docker Hub (or private registry).

๐Ÿ›ก๏ธ Real-World Practice:

  • Only signed and scanned images should be pushed.

  • Ensure image registry access is restricted and uses personal access tokens or service accounts.


โœ… Stage: Docker Scout Analysis

stage('Docker Scout Analysis') {
    steps {
        script {
            withDockerRegistry(credentialsId: 'docker', toolName: 'docker') {
                sh 'docker-scout quickview aungaungmyomyint/netflix:latest'
                sh 'docker-scout cves aungaungmyomyint/netflix:latest'
                sh 'docker-scout recommendations aungaungmyomyint/netflix:latest'
            }
        }
    }
}

Great! Let's break down what Docker Scout Analysis is, especially in the context of modern container and supply chain security ๐Ÿ‘‡


๐Ÿณ What is Docker Scout Analysis?

Docker Scout is a container image analysis tool built by Docker. It helps you:

  • Analyze your Docker images

  • Identify known CVEs (Common Vulnerabilities and Exposures)

  • Get remediation suggestions

  • Understand dependency chains

  • Improve your overall supply chain security posture

Think of it like an intelligent assistant that continuously evaluates your container images for security, trust, and compliance.


๐Ÿ’ฌ Explanation of this line:

"Advanced container analysis: suggestions, CVE reports, and recommendations. Scout gives runtime CVE analysis and upgrade recommendations. Useful for supply chain security and zero-trust environments."

Let's break it down:


โœ… 1. Advanced Container Analysis

Docker Scout deeply analyzes your Docker image to:

  • Inspect all software layers inside (OS packages, language libraries)

  • Map out the software bill of materials (SBOM)

  • Identify outdated packages or vulnerable versions

Itโ€™s more than just a CVE scanner โ€” it gives contextual insight into what needs to be fixed and why.


โœ… 2. CVE Reports

It generates detailed CVE reports for:

  • OS-level packages (e.g., Alpine, Ubuntu, Debian)

  • Application-level dependencies (e.g., pip, npm, Java, Go)

Each CVE entry includes:

  • CVE ID (e.g., CVE-2024-12345)

  • Severity (Low/Medium/High/Critical)

  • Affected package & version

  • Links to patches or CVE info


โœ… 3. Recommendations & Upgrade Guidance

Scout doesnโ€™t just say โ€œyou have a problemโ€ โ€” it also tells you:

  • What to upgrade

  • Which base image to switch to (e.g., node:18 โ†’ node:20)

  • If a newer image with fewer vulnerabilities is available

So you get actionable insights, not just alerts.


โœ… 4. Runtime CVE Analysis

Docker Scout can monitor running containers, not just static images. This allows:

  • Real-time CVE monitoring as containers run

  • Early detection if a CVE affects a live service


โœ… 5. Supply Chain Security & Zero Trust

Docker Scout aligns with modern security models like:

  • ๐Ÿ” Supply Chain Security: You see every piece of software that went into building your image โ€” reducing the risk of hidden dependencies or malicious packages.

  • ๐Ÿ›ก๏ธ Zero Trust: No component is blindly trusted. Scout verifies image provenance, trust policies, and more.


๐Ÿง  Use Case Example

  1. You build a Docker image.

  2. Docker Scout scans it and shows:

    • 5 critical vulnerabilities

    • 2 medium-level misconfigurations

  3. It suggests:

    • Use python:3.12.2-slim instead of python:3.9

    • Upgrade openssl to a secure version

  4. You take action, rebuild, and re-scan โ€” fewer/no CVEs ๐ŸŽ‰


๐Ÿš€ How to Use It

You can use it from:

  • Docker Desktop (integrated UI)

  • Docker CLI:

      docker scout quickview my-image:tag
    
  • CI/CD (GitHub Actions, GitLab CI, etc.)


๐Ÿ“Œ Summary

FeatureDescription
CVE DetectionIdentifies vulnerabilities in container images
Upgrade SuggestionsRecommends safer versions and base images
Runtime InsightsMonitors live containers for CVEs
SBOM SupportTracks what software is in your image
Supply Chain SecurityVerifies image origin and trust
CI/CD IntegrationAutomates security checks in pipelines

โœ… Stage: Deploy to Container

stage('Deploy to Container') {
    steps {
        sh 'docker run -d --name netflix -p 3000:3000 aungaungmyomyint/netflix:latest'
    }
}

๐Ÿ”น Purpose:

  • Runs the container locally or on a test host.

๐Ÿ›ก๏ธ DevOps Usage:

  • Can be extended to Kubernetes, ECS, or Helm deployments.

  • Add health checks, monitoring (Prometheus), and rollbacks here.


โœ… post { always { ... } }

post {
    always {
        emailext( ... )
    }
}

๐Ÿ”น Purpose:

  • Always sends a build notification email (pass or fail).

  • Includes logs and scan reports (trivyfs.txt, trivyimage.txt).

๐Ÿ›ก๏ธ Real-World Practice:

  • Keeps teams aware of pipeline results.

  • Logs can be parsed for audit and compliance.


๐Ÿ’ผ Summary You Can Say in Interview

"I designed a secure and automated Jenkins CI/CD pipeline using SonarQube for static analysis, OWASP Dependency Check for vulnerable packages, and Trivy for both filesystem and container scanning. I enforced quality gates, performed Docker Scout analysis for runtime CVEs, and managed secure image builds and registry pushes. Every stage had a purpose โ€” from ensuring clean workspaces, securing build artifacts, to deploying only verified containers. These practices are based on DevSecOps principles that shift security left and build trust into the delivery pipeline."


More from this blog