How do you fail a CI build when Trivy finds HIGH or CRITICAL vulnerabilities?
BeginnerHands-onTrivy
Answer
Short answer
Use --exit-code 1 together with --severity HIGH,CRITICAL. Trivy then returns exit code 1 only when it finds vulnerabilities at those severities, which fails the CI step. Add --ignore-unfixed so you don't block on issues nobody can fix yet.
Command
trivy image --exit-code 1 --severity HIGH,CRITICAL --ignore-unfixed --no-progress shop-api:$GIT_SHA
GitHub Actions
- name: Build image
run: docker build -t shop-api:${{ github.sha }} .
- name: Trivy scan
uses: aquasecurity/[email protected]
with:
image-ref: shop-api:${{ github.sha }}
severity: HIGH,CRITICAL
ignore-unfixed: true
exit-code: '1'
format: sarif
output: trivy.sarif
- uses: github/codeql-action/upload-sarif@v3
if: always()
with: { sarif_file: trivy.sarif }
Jenkins
stage('Image scan') {
steps {
sh 'trivy image --exit-code 1 --severity CRITICAL --ignore-unfixed shop-api:${GIT_COMMIT}'
}
}
Rolling it out gradually
Start with CRITICAL only (or report-only with --exit-code 0), fix the backlog, then tighten to HIGH,CRITICAL.
What to say in the interview
"--exit-code 1 with --severity HIGH,CRITICAL and --ignore-unfixed turns Trivy into a gate. We scan the image before pushing it, upload SARIF for visibility, and tighten severities gradually as the backlog is cleared."