Severity Filters & CI Gates
A scan that nobody reads protects nothing. In CI, a scanner becomes a gate: a step that fails the pipeline when findings cross a threshold. Both scanners do it through exit codes.
| Tool | Gate flag | Exit code on failure |
|---|---|---|
| Grype | --fail-on <severity> (negligible, low, medium, high, critical): fail if any finding is at or above it |
2 |
| Trivy | --exit-code N with --severity …: fail if any reported finding remains |
the N you chose (usually 1) |
Grype's --fail-on
cp -r /opt/samples/node-app . && cd node-app
for level in critical high; do
grype . -q --fail-on $level -o json > report.json
echo "--fail-on $level -> exit $? ($(jq '.matches | length' report.json) findings in the report)"
done
grype sbom:/opt/samples/sbom/legacy-service.cdx.json -q --fail-on critical --only-fixed > /dev/null
echo "legacy-service, critical + only-fixed -> exit $?"--fail-on critical -> exit 2 (7 findings in the report)
--fail-on high -> exit 2 (7 findings in the report)
legacy-service, critical + only-fixed -> exit 2
Two things to notice:
- The report still contains every finding;
--fail-ononly decides the exit code. Save the report as a build artifact, so the people who fix the failure can see why. - Filters apply before the gate: with
--only-fixed, an unfixable critical can't block the build. Whether that's what you want is a policy decision, not a tooling default.
Without -q, Grype also explains the failure on stderr:
cp -r /opt/samples/node-app . && cd node-app
grype . --fail-on critical -o json > report.json
echo "exit $?"[0000] WARN no explicit name and version provided for directory source, deriving artifact ID from the given path (which is not ideal) from=syft
[0001] ERROR discovered vulnerabilities at or above the severity threshold
exit 2
The WARN line is Grype asking for a name for the scanned directory (--name shop-web); the ERROR line is the gate.
Trivy's --exit-code
Trivy fails only if something is left after filtering, so the gate is the combination of --severity and --exit-code:
img=/opt/samples/images/alpine-3.18.0.tar
trivy image -q --input $img --severity CRITICAL --exit-code 1 -f json -o critical.json
echo "CRITICAL gate -> exit $? ($(jq '[.Results[].Vulnerabilities[]?] | length' critical.json) findings)"
trivy image -q --input $img --severity CRITICAL --exit-code 1 --ignore-unfixed -f json -o /dev/null
echo "+ --ignore-unfixed -> exit $?"
trivy fs -q /opt/samples/go-app --severity CRITICAL --exit-code 1 -f json -o go.json
echo "go-app CRITICAL -> exit $? ($(jq '[.Results[]?.Vulnerabilities[]?] | length' go.json) findings)"CRITICAL gate -> exit 1 (3 findings)
+ --ignore-unfixed -> exit 1
go-app CRITICAL -> exit 0 (0 findings)
The Go module passes the CRITICAL gate: at the time this image was built, golang.org/x/text v0.3.5 had known vulnerabilities, but none rated critical. A green gate means "nothing above the threshold", not "nothing at all" (Field Notes).
Custom gates with jq
Severity is a blunt instrument. A gate on known exploitation is often a better first policy: fail the build if anything is in CISA's KEV catalog, warn on the rest.
gate() { # gate <report.json>: fail on KEV entries, report counts by severity
local kev
kev=$(jq -r '[.matches[] | select((.vulnerability.knownExploited // []) | length > 0) | "\(.artifact.name)@\(.artifact.version) \(.vulnerability.id)"] | unique | .[]' "$1")
jq -r '" findings: " + ([.matches[].vulnerability.severity] | group_by(.) | map("\(.[0])=\(length)") | join(" "))' "$1"
if [ -n "$kev" ]; then echo " FAIL: known exploited:"; echo "$kev" | sed 's/^/ /'; return 1; fi
echo " ok: nothing known to be exploited"
}
grype /opt/samples/node-app -q -o json > node.json
grype sbom:/opt/samples/sbom/legacy-service.cdx.json -q --by-cve -o json > legacy.json
echo "node-app:"; gate node.json; echo " exit $?"
echo "legacy-service:"; gate legacy.json; echo " exit $?"node-app:
findings: Critical=1 High=3 Medium=3
ok: nothing known to be exploited
exit 0
legacy-service:
findings: Critical=10 High=38 Medium=9
FAIL: known exploited:
log4j-core@2.14.1 CVE-2021-44228
log4j-core@2.14.1 CVE-2021-45046
exit 1
Wiring it into CI
A typical job generates the SBOM once, scans the SBOM, uploads both, and gates last. In GitHub Actions (pin third-party actions by commit SHA; see Field Notes):
jobs:
scan:
runs-on: ubuntu-24.04
permissions:
contents: read
steps:
- uses: actions/checkout@<commit-sha> # v5
- name: Install syft and grype
run: |
# download the release binaries and check them against their published SHA-256 sums
./ci/install-scanners.sh syft 1.55.0 grype 0.121.0
- name: SBOM
run: syft scan . --source-name shop-web --source-version "${GITHUB_SHA}" -o cyclonedx-json=sbom.cdx.json
- name: Scan
run: grype sbom:sbom.cdx.json -o json --file grype.json --fail-on high
- name: Keep the evidence
if: always() # upload even when the gate failed
uses: actions/upload-artifact@<commit-sha> # v4
with:
name: supply-chain
path: |
sbom.cdx.json
grype.jsonThe official anchore/scan-action and aquasecurity/trivy-action wrap the same commands; whichever you use, the policy lives in the flags (--fail-on, --severity, ignore files), so keep them in version control and review changes to them like code.
Choosing a threshold
| Policy | Effect |
|---|---|
fail on critical, fixed only |
rarely blocks, catches the worst cheap fixes; a good starting point for an existing codebase |
fail on high |
strict; expect to need ignore rules and VEX quickly |
| fail on KEV | blocks what attackers actually use; pair with a weekly report for everything else |
| fail on new findings only | compare against the previous release's report; stops regressions without blocking on old debt |
Whatever you choose, a gate that's always red gets disabled. Start lenient, fix the backlog, then tighten.
Test yourself
grype . --fail-on high exits with 2. What is in its JSON report?
- Only the high and critical findings
- All findings; --fail-on only sets the exit code
- Nothing, the scan aborted
- Only fixed findings
When does trivy image --severity CRITICAL --exit-code 1 IMG exit with 1?
- Whenever any vulnerability exists
- When at least one CRITICAL finding remains after filtering
- Never; --exit-code needs --fail-on
- When the DB is old
Why upload the scan report with if: always()?
- To make the job faster
- So the evidence is available when the gate step fails the job
- Because artifacts are required
- To skip the gate