Files
ci-actions/trivy-gate/action.yml
T
2026-10-25 18:14:04 +01:00

134 lines
5.9 KiB
YAML

name: trivy-gate
description: >
Scan a pushed image with Trivy and fail when it has fixable vulnerabilities
of the given severities or embedded secrets. Run it on the immutable tag,
before the floating tag (latest / stable-*) moves and before any deploy.
inputs:
image:
description: Full image reference, ideally the immutable sha tag.
required: true
username:
description: Registry user (private images).
default: ""
password:
description: Registry token (private images).
default: ""
platform:
description: Platform of a multi-platform image, e.g. linux/arm64.
default: ""
severity:
description: Severities that block, comma separated.
default: HIGH,CRITICAL
mode:
description: "block = fail the step on findings, warn = report only."
default: block
ignorefile:
description: >
Trivy ignore file in the repo (YAML, each entry with a reason and
expired_at). Used when it exists.
default: .trivyignore.yaml
skip:
description: Emergency bypass (workflow_dispatch input); logged loudly.
default: "false"
trivy-image:
description: Trivy container image, pinned by digest.
default: aquasec/trivy:0.75.0@sha256:af6acf9a6b85dfe389a1941505c0ce9efef52a4719635e1a962f022a3d855daa
cache-volume:
description: >
Docker named volume for the vulnerability DB. A named volume, not a
path: the job talks to the host's Docker daemon, which cannot see
paths inside the job container.
default: trivy-cache
outputs:
report:
description: Path of the Trivy JSON report (with all packages, for SBOM).
value: ${{ steps.scan.outputs.report }}
blocking:
description: Number of blocking findings (vulnerabilities + secrets).
value: ${{ steps.scan.outputs.blocking }}
runs:
using: composite
steps:
- id: scan
shell: bash
env:
IMAGE: ${{ inputs.image }}
TRIVY_USERNAME: ${{ inputs.username }}
TRIVY_PASSWORD: ${{ inputs.password }}
PLATFORM: ${{ inputs.platform }}
SEVERITY: ${{ inputs.severity }}
MODE: ${{ inputs.mode }}
IGNOREFILE: ${{ inputs.ignorefile }}
SKIP: ${{ inputs.skip }}
TRIVY: ${{ inputs.trivy-image }}
VOLUME: ${{ inputs.cache-volume }}
run: |
set -euo pipefail
if [ "$SKIP" = "true" ]; then
echo "::warning::TRIVY GATE SKIPPED for $IMAGE (manual bypass)"
echo "report=" >>"$GITHUB_OUTPUT"; echo "blocking=0" >>"$GITHUB_OUTPUT"
exit 0
fi
case "$MODE" in block|warn) ;; *) echo "::error::mode must be block or warn"; exit 1 ;; esac
work="${RUNNER_TEMP:-/tmp}/trivy-gate"; mkdir -p "$work"
report="$work/$(printf '%s' "$IMAGE" | tr -c 'A-Za-z0-9._-' '_').json"
# DB refresh under an exclusive lock (parallel matrix jobs share the
# volume); Trivy only downloads when the DB is older than its
# NextUpdate. Scans then read the volume read-only under a shared lock.
docker run --rm -v "$VOLUME:/cache" --entrypoint sh "$TRIVY" -c \
'flock /cache/.lock trivy image --download-db-only --cache-dir /cache --quiet'
ignore_args=""
if [ -n "$IGNOREFILE" ] && [ -f "$IGNOREFILE" ]; then
ignore_args="--ignorefile /tmp/ignore.yaml"
echo "ignore file: $IGNOREFILE ($(grep -c -- '- id:' "$IGNOREFILE" || true) entries)"
fi
platform_args=""
[ -z "$PLATFORM" ] || platform_args="--platform $PLATFORM"
# Image is pulled by Trivy from the registry (no daemon socket). The
# ignore file goes in on stdin, outputs come back on stdout.
docker run -i --rm -e TRIVY_USERNAME -e TRIVY_PASSWORD -v "$VOLUME:/cache:ro" \
--entrypoint sh "$TRIVY" -c "cat >/tmp/ignore.yaml; flock -s /cache/.lock \
trivy image --cache-dir /cache --skip-db-update --skip-java-db-update \
--cache-backend memory --image-src remote --scanners vuln,secret --list-all-pkgs \
--format json --quiet $ignore_args $platform_args \"\$0\"" "$IMAGE" \
< <( [ -n "$ignore_args" ] && cat "$IGNOREFILE" || true ) >"$report"
table() { if command -v column >/dev/null; then column -t -s $'\t'; else cat; fi; }
sev_json=$(jq -cn --arg s "$SEVERITY" '$s | split(",") | map(ascii_upcase | gsub(" "; ""))')
jq -r --argjson sev "$sev_json" '
[.Results[]? | .Target as $t | .Vulnerabilities[]?
| select((.Severity as $x | $sev | index($x)) and ((.FixedVersion // "") != ""))
| [.Severity, .VulnerabilityID, .PkgName, .InstalledVersion, (.FixedVersion | split(", ")[0]), $t]]
| unique | .[] | @tsv' "$report" >"$work/vulns.tsv"
jq -r '[.Results[]? | .Target as $t | .Secrets[]? | ["SECRET", .RuleID, .Title, $t, (.StartLine | tostring)]] | .[] | @tsv' \
"$report" >"$work/secrets.tsv"
nv=$(wc -l <"$work/vulns.tsv"); ns=$(wc -l <"$work/secrets.tsv")
unfixed=$(jq --argjson sev "$sev_json" '[.Results[]?.Vulnerabilities[]?
| select((.Severity as $x | $sev | index($x)) and ((.FixedVersion // "") == ""))] | length' "$report")
echo "Trivy $IMAGE: $nv fixable $SEVERITY, $ns secrets ($unfixed $SEVERITY without a fix, not blocking)"
if [ "$nv" -gt 0 ]; then
{ printf 'SEVERITY\tID\tPACKAGE\tINSTALLED\tFIXED\tTARGET\n'; cat "$work/vulns.tsv"; } | table
fi
if [ "$ns" -gt 0 ]; then
{ printf 'SECRET\tRULE\tTITLE\tFILE\tLINE\n'; cat "$work/secrets.tsv"; } | table
fi
blocking=$((nv + ns))
echo "report=$report" >>"$GITHUB_OUTPUT"
echo "blocking=$blocking" >>"$GITHUB_OUTPUT"
if [ "$blocking" -gt 0 ]; then
if [ "$MODE" = block ]; then
echo "::error::Trivy gate: $blocking blocking findings in $IMAGE"
exit 1
fi
echo "::warning::Trivy gate (warn mode): $blocking findings in $IMAGE would block"
fi