Sub topic 14
Adding a vulnerability gate to CI
A vulnerability gate is a pipeline step that fails the build when a scan finds problems above an agreed severity. This chapter assumes you can run trivy image and docker scout cves locally, and that you know how severity filters and --ignore-unfixed work from earlier chapters. It also assumes you know that a CI job fails when a command exits with a non-zero status.
Why scanners need an exit code
By default, both tools print a report and exit with status 0, even when they find critical vulnerabilities. A CI system reads only the exit status, so the job passes and the report goes unread. A gate turns findings into a failing status.
- Trivy uses
--exit-code 1. Trivy exits with 1 when it finds at least one vulnerability that matches your filters. - Docker Scout uses
--exit-code.docker scout cvesthen exits with a non-zero status when it finds vulnerabilities that match its filters.
The filters decide what counts as a failure. Gate on a narrow set, such as fixable high and critical findings. A gate that fails on every low-severity finding gets disabled by frustrated teams.
A gate script
The script below runs both scanners against one image. The first failing command stops the script because of set -e.
#!/bin/sh
set -e
IMAGE="$1"
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 "$IMAGE"
docker scout cves --only-severity critical,high --exit-code "$IMAGE"Save it as gate.sh and call it after the build step:
docker build -t myapp:ci .
sh gate.sh myapp:ciBecause --ignore-unfixed is set on the Trivy line, the gate fails only on problems a developer can fix by upgrading something. Findings with no fix yet do not block the build.
Where the gate sits in the pipeline
Scan the image you built, before you push it to a registry that others pull from. That way a failing image never becomes available for deployment.
The loop back to the build matches the triage and remediation chapters. A developer either fixes the finding by updating the base image, or records an accepted finding in .trivyignore with a comment explaining why.
Common errors
- Passing gate, unread findings. If you forget the exit-code option, the step always succeeds. Test your gate by scanning a known-vulnerable old image once. The job should fail.
- A flaky gate. Vulnerability databases update daily. An image that passed yesterday can fail today with no code change. Treat this as correct behavior, and schedule a nightly scan of released images as well as scanning on each commit.
- Pipes hide failures. Writing
trivy image ... | tee report.txtreturns the exit status oftee, not Trivy's. Write the report to a file with--output, or enableset -o pipefailin a shell that supports it. - Scout authentication. Docker Scout needs a logged-in Docker account in the CI runner. A missing login makes the step fail for a reason unrelated to vulnerabilities.
Exercise
Your pipeline runs trivy image --severity CRITICAL myapp:ci | tee report.txt and always passes, even when the image has critical vulnerabilities. Name two problems with this step.
Show answer
First, --exit-code 1 is missing, so Trivy exits with 0 even when it finds critical vulnerabilities. Second, the pipe to tee makes the step return tee's exit status, which hides any failure from Trivy. Fix it by adding --exit-code 1 and writing the report with --output report.txt instead of piping.