Scanning Container Images With Trivy in CI

A vulnerability scanner that runs on someone’s laptop once a quarter is theatre. The value only arrives when the scan is wired into the pipeline and can fail a build — but fail it on the right things, or your team will route around the gate within a fortnight.

Trivy is the scanner I reach for most: one binary, no daemon, and it will scan a built container image, a filesystem, an IaC directory or a Git repository with the same command surface. This is how to add it to a GitHub Actions pipeline so that it gates on fixable HIGH and CRITICAL CVEs, stays quiet about the noise you cannot action, and does not re-download its vulnerability database on every run. If you are still deciding between scanners, I have written separately on Trivy versus Grype; this piece assumes you have landed on Trivy.

The gate that actually holds

The single decision that determines whether a scan gate survives contact with a delivery team is what you fail on. Fail on every severity and every finding, and the first build to hit a MEDIUM in a base image with no upstream fix will be red through no fault of the author. The pull request gets merged with the check overridden, and from then on a red Trivy step means nothing. The gate is dead.

Free · 4 minutes

Would you survive contact with a determined attacker — or an auditor?

Fourteen questions on access, patching, detection, and recovery — the basics that prevent most real incidents, and the ones most often assumed rather than verified. Banded finding on screen, full sheet by email.

Two flags do the heavy lifting. --severity HIGH,CRITICAL restricts the gate to the severities worth blocking a release for. --ignore-unfixed drops any vulnerability that has no fixed version available upstream — the ones a developer cannot resolve by bumping a package, only by waiting for the distribution to ship a patch. Combined with --exit-code 1, Trivy returns a non-zero status only when there is a HIGH or CRITICAL that someone can actually fix today. That is a gate people trust, because when it goes red there is a real action to take.

Wiring it into GitHub Actions

The official aquasecurity/trivy-action wraps the CLI and, since the newer releases, caches the vulnerability database for you inside the workspace. Pin it to a tag rather than master so an upstream change cannot silently alter your gate’s behaviour. Here the workflow builds the image, loads it into the local Docker daemon, then scans that exact image reference — not a freshly pulled tag, the artefact you are about to ship.

name: build-and-scan
on:
  pull_request:
  push:
    branches: [main]

jobs:
  image-scan:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v4

      # Build locally so we scan the exact image we intend to ship
      - name: Build image
        run: docker build -t app:${{ github.sha }} .

      - name: Trivy image scan (gate)
        uses: aquasecurity/trivy-action@0.28.0
        with:
          scan-type: image
          image-ref: app:${{ github.sha }}
          severity: HIGH,CRITICAL
          ignore-unfixed: true
          exit-code: '1'
          # Fail on the build, not on the DB download
          format: table

Caching is on by default in recent versions of the action: it stores the vulnerability and Java databases under $GITHUB_WORKSPACE/.cache/trivy and restores them on the next run, so you are not pulling roughly a hundred megabytes of CVE data on every push. If your build already leans on Actions caching, the same discipline that makes dependency caching actually hit applies here — a cache that never restores is just slower cold starts with extra YAML.

Suppressing false positives without disabling the gate

Even with --ignore-unfixed, you will occasionally hit a fixable CRITICAL that genuinely does not apply — a CVE in a code path you do not invoke, or one a maintainer has assessed and accepted with a documented rationale. Blanket-lowering the severity gate to work around one finding is how gates rot. Suppress the specific CVE instead, in a .trivyignore file committed to the repository, with a comment and a date so the suppression is auditable rather than a mystery.

# .trivyignore
# Accepted 2026-06-30 (RK): vulnerable function not reachable in our build.
# Review before 2026-09-30.
CVE-2023-45853

# Fixed upstream in a release we cannot yet adopt; tracked in JIRA-4821.
CVE-2024-24790

The action picks this up automatically when the file sits at the repository root; pass trivyignores: ./.trivyignore explicitly if you keep it elsewhere. Treat every line as a standing risk decision. A suppression with a review date and an owner is governance; a suppression with neither is a hole you will have forgotten about by the next audit. If you want a hard expiry, Trivy also supports a .trivyignore.yaml form with an expired_at field, after which the CVE is scanned for again.

Scanning the filesystem and IaC too

The image scan catches vulnerable OS packages and language dependencies baked into layers. It does not catch a hardcoded secret in your source, or a misconfigured Terraform security group. Because Trivy is one binary with several scanners, you can add those in the same job cheaply. Run a filesystem scan for secrets and dependency issues, and a config scan for infrastructure-as-code misconfigurations, gating each on its own severity.

      - name: Trivy filesystem scan (deps + secrets)
        uses: aquasecurity/trivy-action@0.28.0
        with:
          scan-type: fs
          scan-ref: .
          scanners: vuln,secret
          severity: HIGH,CRITICAL
          ignore-unfixed: true
          exit-code: '1'

      - name: Trivy IaC misconfiguration scan
        uses: aquasecurity/trivy-action@0.28.0
        with:
          scan-type: config
          scan-ref: .
          severity: HIGH,CRITICAL
          exit-code: '1'

Note that --ignore-unfixed is meaningful for the vulnerability scanner but not for misconfigurations — an IaC finding is a fix you own, so there is nothing “unfixed” to skip. Keep the config gate separate so a Terraform misconfiguration does not hide behind an image result, and vice versa. The less you have in the image to begin with, the quieter all of this is: a minimal, chiselled container image carries fewer OS packages and therefore fewer CVEs to triage.

Surfacing results where developers look

A failed step with a wall of table output in the Actions log is better than nothing, but the results belong in the pull request. Run a second, non-failing Trivy step in sarif format and upload it to GitHub code scanning, which annotates the PR’s Files Changed tab and populates the Security tab. Give this step exit-code: '0' so it always produces a report; the gate above is what fails the build.

      - name: Trivy scan (SARIF for PR annotation)
        uses: aquasecurity/trivy-action@0.28.0
        with:
          scan-type: image
          image-ref: app:${{ github.sha }}
          format: sarif
          output: trivy-results.sarif
          severity: HIGH,CRITICAL
          exit-code: '0'

      - name: Upload SARIF to code scanning
        uses: github/codeql-action/upload-sarif@v3
        if: always()
        with:
          sarif_file: trivy-results.sarif

Uploading SARIF needs security-events: write in the job’s permissions block, and code scanning has to be enabled on the repository. The if: always() ensures the annotation is posted even when the gate step above has already failed — which is precisely when a developer most wants to see what tripped it.

Once this settles, lift the three steps into a composite action or a reusable workflow so every service inherits the same gate rather than each team copying a slightly different YAML — the case for reusable workflows and composite actions is strongest for exactly this kind of cross-cutting control. A security gate that drifts per repository is not a control; it is a suggestion. The point of wiring Trivy into CI was never the scan. It was making the failing build the cheapest place to find the problem.

Free interactive tool

Website compliance checklist

What your site has to do, based on what it actually does

Answer as much or as little as you like — the list builds as you go. Nothing is stored against your name and no email is required.

Most technology problems are not technology problems. They are control problems.

The systems exist. The investment has been made. The question is whether leadership can understand, direct, evidence, and sustain what those systems produce. Find out where control exists — and where it only appears to.