Sub topic 14
Adding a vulnerability gate to CI
A scan you run by hand finds problems only when someone remembers to run it. A vulnerability gate runs the scan on every build and fails the pipeline when the image breaks a rule you set. This chapter covers how scanner exit codes work, how to write a gate script, how to keep the gate stable, and how to connect it to a CI workflow.
It assumes you can run trivy image and docker scout cves, filter by severity and fix status, and keep a .trivyignore file. It also assumes you know basic shell and CI concepts such as jobs, steps, and a step failing on a nonzero exit code.
How a gate works
A CI job step passes when its command exits with code 0. Any other code fails the step and, by default, the job. A gate is a scanner command that exits nonzero when it finds something you refuse to ship.
Both scanners are informational by default. trivy image prints its table and exits 0 even when it finds hundreds of critical issues. You must opt in to failing.
| Tool | Option | Exit code on findings | Exit code on no findings |
|---|---|---|---|
| Trivy | `--exit-code 1` (any value you pick) | The value you chose | 0 |
| Docker Scout | `--exit-code` (a flag, no value) | 2 | 0 |
Only findings that survive your filters count toward the exit code. If you pass --severity HIGH,CRITICAL, a medium finding cannot fail the build. If you pass --ignore-unfixed, a vulnerability with no fix cannot fail it either. Your filters therefore define the policy, so choose them deliberately.
Choosing the policy
Start from the triage rules you used earlier and write them down as scanner options.
- Severity threshold.
HIGH,CRITICALis a common starting point. Gating on every severity produces constant failures, and teams learn to bypass the gate. - Fix availability.
--ignore-unfixedin Trivy keeps the gate to problems you can act on. A build that fails because of a vulnerability nobody has patched cannot be repaired by the developer who owns the pipeline. - Accepted risk. Findings you reviewed and accepted live in
.trivyignore, so they do not fail the build.
A strict policy that people route around is worse than a modest policy that people respect. Begin with critical and high findings that have fixes, then tighten the policy as the backlog shrinks.
The gate script
The script from the topic plan runs both scanners in sequence.
#!/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"Two behaviors are easy to miss.
set -estops at the first failure. If Trivy fails the gate, Scout never runs. This is fine for a gate, but you only see one tool's report per run. If you want both reports every time, capture each exit code and combine them (shown below).- A failed scan and a failed gate look the same. If Trivy cannot download its vulnerability database, or Scout cannot authenticate, the command also exits nonzero. Developers then see a red build that has nothing to do with their image.
You can separate the two cases by choosing a distinctive Trivy exit code. Real findings return 10, while scanner errors keep the usual code 1.
#!/bin/sh
IMAGE="$1"
status=0
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 10 "$IMAGE"
rc=$?
if [ "$rc" -eq 10 ]; then
echo "Trivy: gate failed, serious fixable vulnerabilities found."
status=1
elif [ "$rc" -ne 0 ]; then
echo "Trivy: scanner error (exit code $rc), not a policy failure."
exit "$rc"
fi
docker scout cves --only-severity critical,high --exit-code "$IMAGE"
rc=$?
if [ "$rc" -eq 2 ]; then
echo "Docker Scout: gate failed, critical or high vulnerabilities found."
status=1
elif [ "$rc" -ne 0 ]; then
echo "Docker Scout: scanner error (exit code $rc), not a policy failure."
exit "$rc"
fi
exit "$status"This script does not use set -e, because set -e would exit before the script could read $?. It runs both scanners, reports each result, and fails at the end if either tool found something.
Docker Scout does not mirror the Trivy options exactly. Trivy's --ignore-unfixed hides vulnerabilities with no fix. Scout has --only-fixed for the same purpose, which you can add to the Scout line if you want both tools to apply the same fix-availability rule. Without it, the two tools can disagree about whether the build should fail. Apply the same filters to both so the gate means one thing.
Keeping the gate stable
The most common complaint about scan gates is that they fail for reasons unrelated to the change under review. Three causes account for most of these cases.
The database moves under you
Scanners compare your image to a vulnerability database that is updated daily. Yesterday's passing commit can fail today with no code change, because a new CVE was published against a package already in your image. This is correct behavior, since the image is now known to be risky. But a gate on pull requests blocks unrelated work.
A common pattern is to gate the build and also schedule a nightly scan of your latest published image. The nightly job opens a ticket or notifies a channel instead of blocking someone's merge.
Scan the image you ship
Scan the exact image that will be pushed, and scan it before you push it. If the scanner reads a different tag than the one you build, the gate gives false assurance. Tag the build with the commit hash and pass that tag to the script, rather than scanning latest, which may point at an older image.
Treat ignore entries as reviewed decisions
A .trivyignore file removes findings from the gate. Every entry should carry a comment that says why it is accepted and when to review it, as in the earlier chapter on triage. An ignore with no comment turns the gate into a formality. Review the file as part of normal code review, and delete entries once the fix ships.
Gating on regressions with compare
A new project may carry hundreds of old findings. Failing every build on them blocks all work until the backlog is cleared. In that situation, a regression gate is more practical: fail only when the new image is worse than the one currently in production.
docker scout compare shows which vulnerabilities a new image adds and removes relative to a baseline.
docker scout compare --to myapp:1.0 myapp:2.0Use this in CI by comparing the candidate against the production tag. A reviewer can read the added vulnerabilities directly in the job log. This approach does not replace an absolute gate for critical findings, but it lets a team with a large backlog stop things from getting worse while it fixes the old issues.
A complete workflow
This GitHub Actions workflow builds the image, scans it with Trivy, and pushes only if the scan passes. Adjust the registry and image name for your project.
name: build-scan-push
on:
push:
branches: [main]
pull_request:
jobs:
image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- name: Build image
uses: docker/build-push-action@v6
with:
context: .
load: true
tags: myapp:${{ github.sha }}
- name: Trivy gate
uses: aquasecurity/[email protected]
with:
image-ref: myapp:${{ github.sha }}
severity: HIGH,CRITICAL
ignore-unfixed: true
exit-code: "1"
- name: Log in to Docker Hub
if: github.ref == 'refs/heads/main'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Push image
if: github.ref == 'refs/heads/main'
run: |
docker tag myapp:${{ github.sha }} ${{ secrets.DOCKERHUB_USERNAME }}/myapp:${{ github.sha }}
docker push ${{ secrets.DOCKERHUB_USERNAME }}/myapp:${{ github.sha }}The key details:
load: trueputs the built image into the runner's local Docker engine, so the scanner can read it without a registry.- The scan step comes before the login and push steps. If it fails, the later steps are skipped and nothing is published.
- The push steps run only on
main, so pull requests get the scan but never publish. - Credentials come from repository secrets, not from the workflow file.
- Pinning the action to a release tag (or better, a commit hash) keeps a third-party update from changing your gate unexpectedly.
To add Docker Scout, log in first, because Scout needs a Docker account. Then run the docker scout cves command from the script above in a later step.
Common mistakes
- Forgetting the exit option. Without
--exit-code, the scan step passes no matter what it finds. - Different filters in each tool. One tool passes while the other fails, and nobody can say what the policy is.
- Scanning
latest. The scanner checks an old image and the new one ships unchecked. - Making the gate too strict on day one. Start with fixable critical and high findings.
- Silent ignore entries. An uncommented
.trivyignorehides risk instead of recording a decision.
Exercise
Your pipeline runs trivy image --severity HIGH,CRITICAL python:3.12-slim and the step always passes, even when the report lists critical vulnerabilities. What is missing, and what extra option would keep the build from failing on vulnerabilities that have no patch yet?
Show answer
The --exit-code option is missing. Trivy exits 0 unless you pass --exit-code with a nonzero value, for example --exit-code 1. To ignore vulnerabilities that have no fixed version, add --ignore-unfixed.
Exercise
A developer's pull request fails the gate with a message that Trivy could not download its database. The code change is unrelated to dependencies. Explain how you could tell this failure apart from a real policy failure in the script, and name the Docker Scout exit code that signals real findings.
Show answer
Give Trivy a distinctive policy exit code such as --exit-code 10. A real finding returns 10, while a database or network error returns a different nonzero code, usually 1. The script reads $? and reports a scanner error separately. Docker Scout returns exit code 2 when --exit-code is set and it finds vulnerabilities.
Exercise
A team inherits an application with several hundred existing high-severity findings. Why would an absolute gate stall all development, and what approach lets the team stop new problems without clearing the backlog first?
Show answer
An absolute gate fails every build because the old findings are always present, so no change can merge. A regression gate avoids this. It compares the new image against the production image, for example with docker scout compare --to myapp:1.0 myapp:2.0, and blocks only when the new image adds vulnerabilities. The team can then fix the backlog gradually.