Skip to main content
This guide shows how to integrate Trivy image vulnerability scanning into a Jenkins pipeline that:
  • Builds a Docker image,
  • Pushes it to Docker Hub,
  • Runs multiple Kubernetes security checks (OPA, Kubesec) in parallel with Trivy,
  • Deploys the image to a Kubernetes cluster only if scan thresholds pass.
The same approach works for scanning images pulled from registries prior to deployment.

Quick overview

  • Build and push Docker image.
  • Run parallel security scans:
    • OPA (conftest)
    • Kubesec
    • Trivy (image vulnerability scanner)
  • Fail the pipeline if configured thresholds (e.g., CRITICAL) are met; otherwise continue to deployment.

Pipeline snippet — where Trivy fits

Below is the relevant part of the Jenkinsfile. The Trivy scan stage runs in parallel with other Kubernetes checks and is placed after Docker build/push and before Kubernetes deployment.

Trivy wrapper script (trivy-k8s-scan.sh)

Create a single script in your repository so Jenkins can call it as part of the pipeline. The example below:
  • Uses Trivy inside Docker (no local install required).
  • Runs two scans:
    • A non-failing scan for LOW/MEDIUM/HIGH (exit code 0).
    • A failing scan for CRITICAL (exit code 1) so the pipeline fails when criticals exist.
  • Mounts $WORKSPACE as the Trivy cache to speed up subsequent scans.
Make sure imageName is exported in the pipeline environment and that the Jenkins agent has permissions to run Docker commands. The second Trivy run intentionally returns exit code 1 when CRITICAL vulnerabilities are found to stop the pipeline.

Example environment block in Jenkinsfile

Define imageName (and other deployment variables) so the scan script can access the built image reference:

Example Trivy output and what it shows

When Trivy finds vulnerabilities, it reports:
  • Affected file or component (e.g., app.jar),
  • Vulnerability ID (CVE),
  • Severity level,
  • Installed version and suggested fixed versions (when available).
Sample scan output (extracted from a CI run):
A Jenkins Dependency-Check Results screen showing a severity distribution bar and a table listing vulnerable JAR files with CVE IDs and severity levels. The left sidebar shows Jenkins project navigation and a small user webcam thumbnail appears in the top-right.

Remediation example — update Tomcat in Spring Boot POM

If Trivy points to Tomcat vulnerabilities originating from a Spring Boot-managed dependency, update the Tomcat version in your pom.xml properties (or upgrade the Spring Boot parent). Example:
When updating dependency versions, always run your unit/integration tests and validate application behavior. Some upgrades may require code changes—verify in a non-production environment first.
After committing the POM change and rerunning the pipeline, Trivy output should reflect the updated artifact and (when compatible) show vulnerabilities resolved.
A Jenkins Blue Ocean pipeline activity page for "devsecops-numeric-application" showing a list of recent build runs with status icons, run numbers, commit messages and durations. The screenshot also shows the browser tabs/bookmarks bar and a circular user photo overlay in the top-right.
Successful re-scan example (no vulnerabilities):

Best practices and final notes

  • Different scanners (Trivy, Dependency-Check, etc.) may return different results due to varying vulnerability databases and heuristics. Treat each finding as an investigation item.
  • Tune severity thresholds for Trivy to match your risk policy. Use exit codes to enforce fail/allow rules.
  • Use the workspace cache (-v "$WORKSPACE":/root/.cache/trivy) to speed repeated scans.
  • Consider scanning both the base image and application artifacts (JAR/WAR) included in the image to get a full picture.
  • Store credentials (Docker Hub, kubeconfig) securely in Jenkins Credentials and reference them in the pipeline with the proper wrappers.
That’s it — integrate Trivy into your Jenkins pipeline to block risky deployments and make vulnerability remediation actionable in CI.

Watch Video

Practice Lab