Files
ci-actions/trivy-gate/action.yml
T
renovate-bot 371eedc0ef
pr-check / docker (pull_request) Successful in 11s
pr-check / binary (pull_request) Successful in 42s
Update trivy to v0.76.1
2026-10-26 16:39:59 +00:00

225 lines
11 KiB
YAML

name: trivy-gate
description: >
Scan a pushed image (or a source tree's lockfiles) 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 or release.
inputs:
image:
description: Full image reference, ideally the immutable sha tag (scan-type image).
default: ""
scan-type:
description: "image = pushed container image, fs = files in the workspace (lockfiles)."
default: image
scan-ref:
description: Directory for scan-type fs (git-tracked files plus untracked lockfiles).
default: .
scanners:
description: >
Trivy scanners. vuln and secret findings block (per mode); misconfig
(Dockerfile, compose, k8s, ...) is only reported, never blocks.
default: vuln,secret
runtime:
description: >
auto = Trivy container when a Docker daemon is reachable, else the
pinned Trivy binary (checksum-verified); docker / binary force one.
default: auto
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.76.1@sha256:790fd66d1697395d8693d1acaa1ea702406a20f109b6fc7bac8db623eca6c06c
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 }}
SCAN_TYPE: ${{ inputs.scan-type }}
SCAN_REF: ${{ inputs.scan-ref }}
RUNTIME: ${{ inputs.runtime }}
SCANNERS: ${{ inputs.scanners }}
TRIVY_VERSION: "0.76.1"
TRIVY_SHA256_AMD64: c7cbe3137fb8a48a17574bede3dd6beb16742d1e9772929940fc90082c39d9c0
TRIVY_SHA256_ARM64: e174dff2a6dba5e3ae41f8d0537a6324405bd08132497340304c6b20e0f16a1f
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:-$SCAN_REF} (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"
case "$SCAN_TYPE" in
image) [ -n "$IMAGE" ] || { echo "::error::image is required"; exit 1; }; target="$IMAGE" ;;
fs) target="fs:$(cd "$SCAN_REF" && pwd)" ;;
*) echo "::error::scan-type must be image or fs"; exit 1 ;;
esac
report="$work/$(printf '%s' "$target" | tr -c 'A-Za-z0-9._-' '_').json"
if [ "$RUNTIME" = auto ]; then
if command -v docker >/dev/null && docker info >/dev/null 2>&1; then RUNTIME=docker; else RUNTIME=binary; fi
fi
if ! command -v jq >/dev/null; then
echo "jq missing, installing"; (apt-get update -qq && apt-get install -y -qq jq) >/dev/null 2>&1 \
|| { echo "::error::jq is required"; exit 1; }
fi
common="--skip-java-db-update --scanners $SCANNERS --list-all-pkgs --format json --quiet"
ignore_args=""
if [ -n "$IGNOREFILE" ] && [ -f "$IGNOREFILE" ]; then
echo "ignore file: $IGNOREFILE ($(grep -c -- '- id:' "$IGNOREFILE" || true) entries)"
fi
platform_args=""
[ -z "$PLATFORM" ] || platform_args="--platform $PLATFORM"
# fs scans see the git-tracked files plus lockfiles generated in the
# job even when .gitignore lists them (Flutter ignores pubspec.lock).
fs_list() {
(cd "$SCAN_REF" && { git ls-files 2>/dev/null || find . -type f -not -path './.git/*'; \
git ls-files --others 2>/dev/null \
| grep -E '(^|/)([^/]*\.lock|[^/]*lock\.json|[^/]*\.lockfile)$' \
| grep -vE '(^|/)(node_modules|build|\.dart_tool|\.pub-cache|\.gradle)/' || true; } \
| sort -u)
}
if [ "$RUNTIME" = docker ]; then
# 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'
if [ "$SCAN_TYPE" = image ]; then
[ -z "$IGNOREFILE" ] || [ ! -f "$IGNOREFILE" ] || ignore_args="--ignorefile /tmp/ignore.yaml"
# 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 --cache-backend memory \
--image-src remote $common $ignore_args $platform_args \"\$0\"" "$IMAGE" \
< <( [ -n "$ignore_args" ] && cat "$IGNOREFILE" || true ) >"$report"
else
# The job's paths do not exist for the host daemon: the tree goes
# in as a tar stream on stdin.
[ -z "$IGNOREFILE" ] || [ ! -f "$IGNOREFILE" ] || ignore_args="--ignorefile /src/$IGNOREFILE"
fs_list | (cd "$SCAN_REF" && tar -c -T -) | docker run -i --rm -v "$VOLUME:/cache:ro" \
--entrypoint sh "$TRIVY" -c "mkdir /src && tar -x -C /src && flock -s /cache/.lock \
trivy fs --cache-dir /cache --skip-db-update --cache-backend memory \
$common $ignore_args /src" >"$report"
fi
else
bin="$work/bin/trivy"
if [ ! -x "$bin" ]; then
case "$(uname -m)" in
x86_64) arch=64bit; sum="$TRIVY_SHA256_AMD64" ;;
aarch64|arm64) arch=ARM64; sum="$TRIVY_SHA256_ARM64" ;;
*) echo "::error::no pinned Trivy binary for $(uname -m)"; exit 1 ;;
esac
mkdir -p "$work/bin"
curl -fsSL -o "$work/trivy.tgz" \
"https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-${arch}.tar.gz"
echo "$sum $work/trivy.tgz" | sha256sum -c --quiet
tar -xzf "$work/trivy.tgz" -C "$work/bin" trivy
fi
[ -z "$IGNOREFILE" ] || [ ! -f "$IGNOREFILE" ] || ignore_args="--ignorefile $IGNOREFILE"
if [ "$SCAN_TYPE" = image ]; then
"$bin" image --cache-dir "$work/cache" --cache-backend memory --image-src remote \
$common $ignore_args $platform_args "$IMAGE" >"$report"
else
mkdir -p "$work/src"; rm -rf "${work:?}/src/"*
fs_list | (cd "$SCAN_REF" && tar -c -T -) | tar -x -C "$work/src"
"$bin" fs --cache-dir "$work/cache" --cache-backend memory $common $ignore_args "$work/src" >"$report"
fi
fi
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 $target: $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
# Misconfigurations: reported, never blocking.
jq -r --argjson sev "$sev_json" '[.Results[]? | .Target as $t | .Misconfigurations[]?
| select(.Status == "FAIL" and (.Severity as $x | $sev | index($x)))
| [.Severity, .ID, .Title, $t]] | unique | .[] | @tsv' "$report" >"$work/misconf.tsv"
nm=$(wc -l <"$work/misconf.tsv")
if [ "$nm" -gt 0 ]; then
echo "::warning::Trivy config: $nm $SEVERITY misconfigurations in $target (not blocking)"
{ printf 'SEVERITY\tID\tTITLE\tFILE\n'; cat "$work/misconf.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 $target"
exit 1
fi
echo "::warning::Trivy gate (warn mode): $blocking findings in $target would block"
fi