fix(ci): retry transient Trivy database failures
This commit is contained in:
1 parent
63216de07e
commit
7aeeb7552a
1 file changed
+28
-8
+28
-8
@@ -6,22 +6,42 @@ status=0
|
||||
python3 scripts/ci/check-advisories.py artifacts/security/nuget.json || status=1
|
||||
sha=$(git rev-parse HEAD)
|
||||
scanner=aquasec/trivy:0.69.3@sha256:bcc376de8d77cfe086a917230e818dc9f8528e3c852f7b1aff648949b6258d1c
|
||||
cache_volume="heimdall-trivy-cache-$sha"
|
||||
docker volume create "$cache_volume" >/dev/null
|
||||
trap 'docker volume rm "$cache_volume" >/dev/null 2>&1 || true' EXIT
|
||||
scan_image() (
|
||||
set -euo pipefail
|
||||
component=$1
|
||||
archive=$(mktemp)
|
||||
scanner_container=""
|
||||
trap 'rm -f "$archive"; if [[ -n "$scanner_container" ]]; then docker rm -f "$scanner_container" >/dev/null; fi' EXIT
|
||||
trap 'rm -f "$archive"; if [[ -n "$scanner_container" ]]; then docker rm -f "$scanner_container" >/dev/null 2>&1 || true; fi' EXIT
|
||||
docker save "heimdall-$component:ci-$sha" -o "$archive" || exit 1
|
||||
report="artifacts/security/$component.json"
|
||||
# Trivy reopens archive entries: stdin cannot serve as a seekable image tar.
|
||||
# Docker cp streams from the job container, without daemon-host bind paths.
|
||||
scanner_container=$(docker create "$scanner" image --input /tmp/image.tar --scanners vuln \
|
||||
--severity HIGH,CRITICAL --exit-code 1 --format json) || exit 1
|
||||
docker cp "$archive" "$scanner_container:/tmp/image.tar" >/dev/null || exit 1
|
||||
cli_status=0
|
||||
docker start --attach "$scanner_container" > "artifacts/security/$component.json" || cli_status=$?
|
||||
state=$(docker inspect "$scanner_container" --format '{{.State.Status}} {{.State.ExitCode}}') || exit 1
|
||||
[[ "$cli_status" == 0 && "$state" == "exited 0" ]]
|
||||
# Share the database between scans and retry an infrastructure failure once;
|
||||
# a completed report with findings still fails the gate immediately.
|
||||
for attempt in 1 2; do
|
||||
scanner_container=$(docker create --mount "source=$cache_volume,target=/root/.cache/trivy" \
|
||||
"$scanner" image --input /tmp/image.tar --scanners vuln \
|
||||
--severity HIGH,CRITICAL --exit-code 1 --format json) || exit 1
|
||||
docker cp "$archive" "$scanner_container:/tmp/image.tar" >/dev/null || exit 1
|
||||
cli_status=0
|
||||
docker start --attach "$scanner_container" > "$report" || cli_status=$?
|
||||
state=$(docker inspect "$scanner_container" --format '{{.State.Status}} {{.State.ExitCode}}') || exit 1
|
||||
if [[ -s "$report" ]]; then
|
||||
[[ "$cli_status" == 0 && "$state" == "exited 0" ]]
|
||||
exit $?
|
||||
fi
|
||||
docker rm -f "$scanner_container" >/dev/null 2>&1 || true
|
||||
scanner_container=""
|
||||
if [[ "$attempt" == 1 ]]; then
|
||||
echo "Trivy infrastructure failure for $component; retrying once" >&2
|
||||
sleep 2
|
||||
fi
|
||||
done
|
||||
echo "Trivy returned no report for $component after retry" >&2
|
||||
exit 1
|
||||
)
|
||||
for component in server agent; do
|
||||
if ! docker image inspect "heimdall-$component:ci-$sha" >/dev/null 2>&1; then
|
||||
|
||||
Reference in new issue
Block a user