All guides

Scan container images with Trivy in CI

By default, Trivy exits with code 0 even when it detects security issues. The exit code, the severity filter and an ignore file decide which findings fail a build.

Close-up of code on a screen in many colours

The command and three flags

Shell

trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 my-app:1.4.2

trivy image lists the known vulnerabilities in the operating system packages and language packages of an image. It also runs a secret scanner by default. Pass --scanners vuln to turn that off.

  • --severity filters the report. Without it, Trivy reports every level from UNKNOWN to CRITICAL.
  • --exit-code 1 sets the exit code for a run that finds security issues. By default Trivy exits with code 0 even when it detects them.
  • --ignore-unfixed skips vulnerabilities for which the distribution has not provided a patch. It is shorthand for --ignore-status affected,will_not_fix,fix_deferred,end_of_life.

Trivy's documentation shows a split into two runs: one reports MEDIUM,HIGH with --exit-code 0, and the other uses --exit-code 1 --severity CRITICAL, so the job fails only when a critical vulnerability is found.

An ignore file with reasons

Trivy reads the IDs of findings to ignore from a file. The --ignorefile flag defaults to .trivyignore. This example follows the one in Trivy's documentation, with a date that is yours to choose:

Text

# No impact in our settings
CVE-2019-1543

# Accept the risk until the date below
CVE-2019-14697 exp:2027-01-31

The lines that start with # hold the reason, as in the documented example, and exp: ends the entry on that date. --show-suppressed shows the suppressed results. A YAML form, .trivyignore.yaml, adds fields such as paths, statement and expired_at to each entry. It is experimental, and you must name the file with --ignorefile.

The GitHub Action, pinned

On March 19, 2026 an attacker force-pushed 76 of the 77 version tags of aquasecurity/trivy-action to malicious commits. The maintainers' advisory tells users to pin GitHub Actions to full commit SHA hashes and to stop using mutable version tags. The original tags 0.0.1 to 0.34.2 were deleted, and the new tags carry a v prefix.

YAML

jobs:
  scan:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      security-events: write
    steps:
      - uses: actions/checkout@v7
      - run: docker build -t my-app:${{ github.sha }} .
      - uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
        with:
          image-ref: my-app:${{ github.sha }}
          format: sarif
          output: trivy-results.sarif
          severity: CRITICAL,HIGH
          limit-severities-for-sarif: true
          ignore-unfixed: true
          exit-code: '1'
      - uses: github/codeql-action/upload-sarif@v4
        if: always()
        with:
          sarif_file: trivy-results.sarif

The hash in this example is the commit that the v0.36.0 tag points to in the action's repository. Look it up there yourself before you use it. The severity list and the exit code are choices to adjust. The trivyignores input takes the path of one or more .trivyignore files.

Results in code scanning

format: sarif writes a SARIF file, and upload-sarif sends it to GitHub code scanning. Three details matter:

  • The job needs security-events: write.
  • if: always() uploads the file even when Trivy returns a non-zero exit code.
  • In SARIF format the action outputs every severity unless limit-severities-for-sarif is true.

A private or internal repository gets code scanning once GitHub Code Security is enabled for it. For repositories with no code scanning, the action's README shows another route: write the result to a file with the output input and append that file to the workflow summary.

The vulnerability database cache

Trivy fetches its vulnerability database automatically when it needs it. The action caches that database, the Java database and the checks bundle with actions/cache, and the cache is on by default. Its README recommends keeping it enabled to avoid rate limiting issues.

A workflow can restore a cache created on its own branch or on the default branch. To share a cache across branches, the README says to create it on the default branch, and it shows a scheduled workflow that updates the cache there once a day.

Outside the action, trivy image --download-db-only updates the database with no scan and populates the cache for the scans that follow.

What an image scan cannot see

Trivy's documentation states these limits itself:

  • Advisory sources. Trivy detects known vulnerabilities, and it only consumes security advisories from the sources its documentation lists.
  • Software that no package manager installed. Trivy detects vulnerabilities in it in some cases only, such as Go and Rust binaries with embedded module information.
  • Third-party repositories. Trivy skips packages installed from third-party repositories such as EPEL, because official OS security advisories do not cover them.
  • Old images. You can get 0 vulnerabilities from an image whose packages have no fixed versions when --ignore-unfixed is set, or from an outdated OS. --exit-on-eol 1 fails the scan when the OS has reached end of life.
  • The image configuration. Misconfiguration and secret checks on the image configuration are disabled by default. --image-config-scanners enables them.

The same documentation says Trivy prioritizes precision and accepts some false negatives to keep false positives low.

Where to start

  1. Run the scan with the default exit code of 0 and read the report.
  2. Add --exit-code 1 --severity CRITICAL, the gate that Trivy's own example uses.
  3. Record each accepted finding in .trivyignore with a comment and an exp: date.
  4. Pin the action to a commit SHA, as the maintainers' advisory asks, and keep its cache on.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides