ci: Trivy 데이터베이스를 runner 캐시에서 복원한다

문제: scripts/ci/trivy-scan.sh가 job마다 취약점 DB(133 MB, 약 30초)를
내려받는다. release 이미지 job은 Java 색인(952 MB, 약 2분)도 받는다. run 125의
이미지 job 7.0분 가운데 약 2.8분이 이 두 내려받기였다.

고른 방법: 새 composite action `.gitea/actions/trivy-db-cache`가 Trivy 기본
캐시 디렉터리(~/.cache/trivy)의 db와 java-db를 actions/cache로 복원한다.
pr.yml `done`, main.yml `vulnerabilities`, release.yml 이미지 job의 scan 앞에
둔다. Java 색인은 이미지 job만 받는다(`java-db: "true"`).
- Trivy는 DB 자체의 metadata로 최신인지 판단하고, 아니면 새로 받는다. 복원은
  시간을 줄일 뿐이고 아는 권고 목록을 바꾸지 않는다.
- 항목은 덮어쓰지 않으므로 key에 기간을 넣는다. 취약점 DB는 하루, Java 색인은
  Trivy가 스스로 3일마다 갱신하므로 3일 단위다. 952 MB가 매일 캐시에 쌓이지 않는다.
- 저장은 main ref만 한다. JDK·Gradle 캐시와 같은 규칙이다. 브랜치가 main이 쓸
  DB를 정하지 못한다.

버린 대안: `--skip-db-update`로 캐시만 쓰기. DB가 오래될 수 있어 게이트가 약해진다.
key 하나 고정. 첫 사본이 영원히 남는다.

위험·한계: Java 색인 항목이 runner 캐시(5 GB 상한)의 약 1 GB를 쓴다. 효과는
main이 한 번 저장한 뒤부터 보인다.

검증: 네 YAML을 읽었다. runner 측정은 PR 본문에 적는다.
This commit is contained in:
DongHyeonka
2026-11-12 18:56:52 +09:00
parent e7be517e06
commit afbfb1cc73
5 changed files with 61 additions and 1 deletions
+52
View File
@@ -0,0 +1,52 @@
name: Restore the Trivy databases
description: >-
Restores Trivy's vulnerability database, and with java-db its Java index, from the runner's
cache. A run on main also saves the current copy.
# scripts/ci/trivy-scan.sh downloaded the vulnerability database (133 MB, about 30 seconds) in every
# scan job, and the image scan also the Java index (952 MB, about two minutes of the 7-minute image
# job, release run 125). Trivy decides from a database's own metadata whether it is current and
# downloads a new one when it is not, so a restored copy changes how long a scan takes, not which
# advisories it knows.
#
# The keys carry a period because an entry is never overwritten: under a fixed key the first copy
# would stay forever. The vulnerability database gets a key a day; the Java index, which Trivy
# itself refreshes every three days, a key every three days, so its 952 MB lands in the runner's
# cache about twice a week instead of daily. Only main saves, the rule the JDK and Gradle caches
# follow (setup-java-gradle): a branch cannot decide what a later main run scans against.
inputs:
java-db:
description: Also restore Trivy's Java index, which only the image scan reads
default: "false"
runs:
using: composite
steps:
- id: period
shell: bash
run: |
echo "day=$(date -u +%Y-%m-%d)" >> "${GITHUB_OUTPUT}"
echo "three-days=$(( $(date -u +%s) / 259200 ))" >> "${GITHUB_OUTPUT}"
- if: ${{ github.ref == 'refs/heads/main' }}
uses: https://github.com/actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache@v4.3.0
with:
path: ~/.cache/trivy/db
key: trivy-db-${{ runner.os }}-${{ steps.period.outputs.day }}
restore-keys: trivy-db-${{ runner.os }}-
- if: ${{ github.ref != 'refs/heads/main' }}
uses: https://github.com/actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache/restore@v4.3.0
with:
path: ~/.cache/trivy/db
key: trivy-db-${{ runner.os }}-${{ steps.period.outputs.day }}
restore-keys: trivy-db-${{ runner.os }}-
- if: ${{ inputs.java-db == 'true' && github.ref == 'refs/heads/main' }}
uses: https://github.com/actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache@v4.3.0
with:
path: ~/.cache/trivy/java-db
key: trivy-java-db-${{ runner.os }}-${{ steps.period.outputs.three-days }}
restore-keys: trivy-java-db-${{ runner.os }}-
- if: ${{ inputs.java-db == 'true' && github.ref != 'refs/heads/main' }}
uses: https://github.com/actions/cache/restore@0057852bfaa89a56745cba8c7296529d2fc39830 # actions/cache/restore@v4.3.0
with:
path: ~/.cache/trivy/java-db
key: trivy-java-db-${{ runner.os }}-${{ steps.period.outputs.three-days }}
restore-keys: trivy-java-db-${{ runner.os }}-
+1
View File
@@ -109,6 +109,7 @@ jobs:
timeout-minutes: 20
steps:
- uses: https://github.com/actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # actions/checkout@v4.2.2
- uses: ./.gitea/actions/trivy-db-cache
- run: bash scripts/ci/trivy-scan.sh filesystem
# Experimental by design: the result is reported, never used to block anything.
+1
View File
@@ -42,5 +42,6 @@ jobs:
- run: ./gradlew :prCheck
# Plans every stage (--dry-run: nothing executes), so it cannot be a dependency of prCheck.
- run: ./gradlew :verifyStrictLaneStages --dry-run
- uses: ./.gitea/actions/trivy-db-cache
# Known High and Critical vulnerabilities and CISA KEV matches in the dependency tree.
- run: bash scripts/ci/trivy-scan.sh filesystem
+4
View File
@@ -197,6 +197,10 @@ jobs:
.
# The SBOM is printed and kept in the log before the blocking scan, so the inventory of an image
# that is refused still exists. A vulnerable image is never pushed: the push comes after.
# The image scan also reads Trivy's Java index (952 MB), the bulk of this job's download time.
- uses: ./.gitea/actions/trivy-db-cache
with:
java-db: "true"
- name: Inventory and scan the release image
run: |
set -euo pipefail
+3 -1
View File
@@ -15,7 +15,9 @@
# temporary directory): a checksum is a supply-chain claim that can be checked offline, an action
# pinned to a commit is not. .trivyignore.yaml is the one suppression source; each entry carries a
# reason and an expiry by policy (.github/dependency-vulnerability-policy.md).
# Optional mirrors: TRIVY_DOWNLOAD_BASE_URL, JQ_DOWNLOAD_BASE_URL, KEV_FEED_URL.
# Optional mirrors: TRIVY_DOWNLOAD_BASE_URL, JQ_DOWNLOAD_BASE_URL, KEV_FEED_URL. The workflows restore
# Trivy's databases into its default cache directory first (.gitea/actions/trivy-db-cache); Trivy
# still downloads a newer one when the restored copy's own metadata says it is out of date.
set -euo pipefail
readonly TRIVY_VERSION='0.71.2'