← All Insights

Trivy in CI/CD: How to Add Vulnerability Scanning to Your Pipeline

Vulnerabilities discovered in production are significantly more expensive to fix than vulnerabilities caught during development. The gap in cost (time, effort, risk, and potential damage) makes a strong case for automated vulnerability scanning as a mandatory part of every CI/CD pipeline.

Trivy, maintained by Aqua Security, is one of the most capable open-source security scanners available. It is fast, comprehensive, and straightforward to integrate. This guide walks through adding it to your pipeline effectively.


What Trivy Scans

Trivy's scope is broad, which is one of its key advantages over more narrowly focused scanners. It can scan:

  • Container images: identifying vulnerable packages in base images and application layers
  • Filesystems and Git repositories
  • Infrastructure-as-code configurations (Terraform, CloudFormation, Kubernetes YAML)
  • Application dependencies (npm, pip, gem, Maven, etc.)
  • Secrets and hardcoded credentials
  • Software licenses

A single tool handling this range reduces the number of integrations you need to maintain.


Installation

On Ubuntu, install Trivy via Aqua Security's official repository:

sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo "deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | \
  sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy

For other platforms, Trivy provides a Docker image and pre-built binaries for direct download.


Basic Scanning

Scan a Docker image to identify vulnerable packages:

trivy image nginx:latest

The output lists each vulnerable package with its severity rating (CRITICAL, HIGH, MEDIUM, LOW), the CVE identifier, the installed version, and the version that fixes the vulnerability.


Filtering by Severity

A raw scan of a typical container image may return hundreds of results. Most of them are LOW or MEDIUM severity issues that do not require immediate action. Filtering to HIGH and CRITICAL keeps the output actionable:

trivy image --severity HIGH,CRITICAL nginx:latest

Start with this filter when adding Trivy to your pipeline. Addressing every LOW vulnerability immediately is often impractical and creates friction that leads teams to bypass the scan entirely.


Creating a Security Gate in Your Pipeline

The --exit-code 1 flag causes Trivy to return a non-zero exit code when vulnerabilities matching the specified severity are found. Most CI systems treat a non-zero exit code as a build failure:

trivy image --severity HIGH,CRITICAL --exit-code 1 your-app:latest

Adding --ignore-unfixed excludes vulnerabilities that have no available fix. This is often the right policy. You cannot fix a vulnerability if no patch exists yet, and blocking deployments for unfixable issues just creates noise without actionable outcomes:

trivy image \
  --severity HIGH,CRITICAL \
  --exit-code 1 \
  --ignore-unfixed \
  your-app:latest

Where to Add Trivy in Your Pipeline

The most effective approach is scanning at multiple points:

  1. Developer machines (pre-commit/pre-push): Catch issues before they reach the repository. Trivy's filesystem scan works well here: trivy fs --scanners secret,vuln .
  2. CI build stage: After building the Docker image and before pushing to the registry, scan the image. Fail the build if HIGH/CRITICAL vulnerabilities are found.
  3. Container registry: Many registries support continuous scanning of stored images. This catches vulnerabilities discovered after the initial build.

Scanning only at one point leaves gaps. The combination provides defense in depth.


Managing Exceptions with .trivyignore

Some vulnerabilities may be acceptable exceptions, such as a vulnerability in a component that is not reachable in your deployment, or one where the risk is understood and accepted. The .trivyignore file allows you to exclude specific CVEs from scan results:

# .trivyignore
# CVE-2023-12345 - Not exploitable in our configuration, reviewed 2026-08-30 by @hirendave
CVE-2023-12345

The comment is important. Every exception should document who approved it, why, and when it should be reviewed. Undocumented exceptions accumulate silently and become security debt.


Common Mistakes to Avoid

  • Scanning only after deployment: By then, a vulnerable image may already be running in production. Scan during the build.
  • Blocking on everything immediately: Introducing Trivy with zero tolerance for any vulnerability will surface dozens of issues on day one and create pressure to disable the scan. Start with HIGH/CRITICAL and unfixed-only.
  • Treating Trivy as the complete security program: Trivy scans for known vulnerabilities and secrets. It does not replace penetration testing, code review, or runtime security monitoring.

Conclusion

Trivy provides a pragmatic, comprehensive approach to vulnerability scanning that integrates cleanly into existing CI/CD pipelines. The investment in setup is low; the return in risk reduction is substantial.

Start with image scanning on your most critical services. Add the HIGH/CRITICAL filter and the unfixed exclusion. Build the habit of reviewing and remediating findings as part of the normal development cycle. Then expand coverage to filesystem scans, IaC scanning, and broader severity ranges as the team builds confidence with the tool.

The goal is not a perfect scan score. It is a process that catches real risks before they reach production.