Trivy Image Scanning in GitHub Actions: A Gate That Blocks Only Fixable Issues

Container images carry more than your application: a base operating system, language runtimes, and every package your dependencies pulled in. Any of them can have known vulnerabilities (CVEs). Scanning images in CI catches the fixable ones before an image ships.
The hard part isn't running a scanner, it's making the result useful. A gate that fails on every unfixable CVE gets disabled within a week. This guide sets up Trivy in GitHub Actions with a policy that blocks fixable high-severity issues, records the full findings, and keeps scanning images after they've shipped.
Where Image Vulnerabilities Come From
An image is built from layers: the base OS packages, a language runtime, package manager tooling, and your application's dependencies. Vulnerabilities in any layer are published as CVEs and matched by scanners against the package versions they find in the image.
The problem isn't just malicious code. It's often outdated or unpatched versions of common utilities like curl, openssl, or specific language packages (e.g., npm, pip, Maven). These vulnerabilities can be exploited for remote code execution, privilege escalation, or data exfiltration. Most findings in a typical image come from the base image, not your code. That's good news: switching to a smaller or newer base image often removes more findings at once than any other fix.
About Trivy
Trivy is an open-source scanner from Aqua Security for container images, filesystems, Git repositories and infrastructure-as-code files. It runs as a single binary with no server. On each run, it downloads (or reuses a cached copy of) a compiled vulnerability database built from distribution security advisories and language ecosystem advisory databases, then matches it against the packages it finds.
Key features of Trivy include:
OS Package Vulnerabilities: Scans for vulnerabilities in OS packages (Alpine, Debian, Ubuntu, CentOS, RHEL, etc.).
Application Dependencies: Detects vulnerabilities in language-specific packages (Bundler, Composer, Go Modules, Maven, npm, pip, Poetry, Yarn, etc.).
IaC Misconfigurations: Can also scan configuration files (Terraform, Kubernetes, Dockerfile) for security misconfigurations, though our focus today is image vulnerabilities.
Secrets and SBOMs: It can also detect hard-coded secrets and generate a software bill of materials (CycloneDX or SPDX).
Step-by-Step: Integrating Trivy into Your CI/CD Workflow
Let's dive into integrating Trivy into a practical development pipeline. We'll use a simple Dockerfile and a GitHub Actions workflow as an example.
Step 1: Install Trivy (Local Setup for Testing)
Before integrating into CI/CD, it's good practice to get comfortable with Trivy locally. Installation is straightforward across different operating systems.
macOS:
brew install aquasecurity/trivy/trivyAny OS with Docker:
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock aquasec/trivy image alpine:3.20For Linux packages, follow the installation guide, which sets up the APT or RPM repository with a signed keyring.
Step 2: Perform a Basic Image Scan
Once installed, you can scan any public Docker image. Let's start with a common base image.
trivy image alpine:3.15This downloads the vulnerability database on the first run, then scans alpine:3.15, an Alpine release that's past end of life, reporting any found vulnerabilities categorized by severity. Compare it with a current release (trivy image alpine:3.20) to see how much simply staying on a supported base image matters.
Step 3: Scan a Custom Dockerfile
To see findings from both the OS and application layers, build an image with an end-of-life Node.js base and an old Express version:
Create a file named Dockerfile:
FROM node:14-alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]And a simple package.json:
{
"name": "my-vulnerable-app",
"version": "1.0.0",
"description": "A simple Node.js app",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"express": "4.17.1"
}
}Build this image locally:
docker build -t my-app:v1 .Then, scan your custom-built image with Trivy:
trivy image my-app:v1The report has separate sections for the OS packages and for node_modules (Trivy reads package-lock.json and installed package metadata). Each finding shows the installed version and, if one exists, the fixed version. Moving to node:22-alpine and a current Express release clears most of them.
Step 4: Integrating Trivy into Your CI/CD Pipeline
The real power of Trivy comes from automating scans within your CI/CD pipeline. For this example, we'll use GitHub Actions, but the concepts are transferable to GitLab CI, Jenkins, Azure DevOps, or others. Our goal is to build our Docker image, scan it with Trivy, and fail the pipeline if critical vulnerabilities are detected.
Create .github/workflows/docker-scan.yaml:
name: Image scan
on:
push:
branches: [main]
pull_request:
schedule:
- cron: "0 3 * * *" # rescan nightly: new CVEs appear for unchanged images
permissions:
contents: read
security-events: write # needed to upload SARIF to code scanning
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build image
run: docker build -t my-app:${{ github.sha }} .
# Pin third-party actions to a release tag (or better, a commit SHA), never @master.
- name: Report all findings (SARIF)
uses: aquasecurity/trivy-action@0.33.1
with:
image-ref: my-app:${{ github.sha }}
format: sarif
output: trivy.sarif
exit-code: "0"
- name: Upload to GitHub code scanning
uses: github/codeql-action/upload-sarif@v3
if: always()
with:
sarif_file: trivy.sarif
- name: Gate on fixable HIGH/CRITICAL
uses: aquasecurity/trivy-action@0.33.1
with:
image-ref: my-app:${{ github.sha }}
format: table
exit-code: "1"
ignore-unfixed: true
severity: CRITICAL,HIGHThe workflow scans twice, for two purposes:
- The report includes every finding, in SARIF format, which GitHub displays under Security → Code scanning. Findings are tracked over time there, and you can dismiss them with a reason.
- The gate fails the job only for
HIGHandCRITICALvulnerabilities that have a fix available (ignore-unfixed). These are the ones a developer can act on today by upgrading a package or base image.
The second scan reuses the vulnerability database downloaded by the first, so it adds only a few seconds. The schedule trigger rescans every night: an image that passed last week can have new CVEs today without any code change.
💡 Tip: Check the tag you pin against the action's releases page. Pinning to a full commit SHA, with the version in a comment, protects you if a tag is ever moved, which is how several supply-chain attacks on popular GitHub Actions have worked. Dependabot can keep pinned SHAs up to date.
Step 5: Keep the Gate Useful
- Start from smaller base images: distroless or
-slim/Alpine variants contain fewer packages, so there's less to be vulnerable. Use multi-stage builds so compilers and build tools stay out of the final image. - Rebuild regularly: a base image tag like
node:22-alpinereceives security updates, but your image only gets them when you rebuild. Rebuild on a schedule, and let Dependabot or Renovate propose base image and dependency updates. - Record accepted risks with an expiry: when a finding can't be fixed yet or doesn't apply (for example, the vulnerable function is never called), add it to
.trivyignorewith a comment explaining why, and review the file regularly. Trivy also accepts a YAML ignore file (.trivyignore.yaml) where each entry can have anexpired_atdate, so exceptions don't become permanent. - Cache the database in busy pipelines: the vulnerability database is downloaded from a public registry, which can rate-limit heavy users. The Trivy action caches it by default. In other CI systems, persist Trivy's cache directory between runs.
⚠️ Common mistake: Failing the build on every finding, including unfixed and low-severity ones. Builds turn red for reasons nobody can fix, developers learn to rerun or bypass the check, and the gate stops meaning anything. Gate on what's fixable and severe, and send everything else to a report that someone reviews.
Summary
- Scan every image build in CI, and rescan on a schedule because new CVEs are published for images that haven't changed.
- Separate the report from the gate: report everything (SARIF to code scanning), and fail the build only on fixable
HIGH/CRITICALfindings. - Fix at the base image first: supported, minimal base images and multi-stage builds remove most findings.
- Pin third-party actions to a version or commit SHA.
- Document accepted risks in an ignore file with reasons and expiry dates.
