From afbfb1cc735df527c5e587327eceae6c713af0a3 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 12 Nov 2026 18:56:52 +0900 Subject: [PATCH] =?UTF-8?q?ci:=20Trivy=20=EB=8D=B0=EC=9D=B4=ED=84=B0?= =?UTF-8?q?=EB=B2=A0=EC=9D=B4=EC=8A=A4=EB=A5=BC=20runner=20=EC=BA=90?= =?UTF-8?q?=EC=8B=9C=EC=97=90=EC=84=9C=20=EB=B3=B5=EC=9B=90=ED=95=9C?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: 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 본문에 적는다. --- .gitea/actions/trivy-db-cache/action.yml | 52 ++++++++++++++++++++++++ .gitea/workflows/main.yml | 1 + .gitea/workflows/pr.yml | 1 + .gitea/workflows/release.yml | 4 ++ scripts/ci/trivy-scan.sh | 4 +- 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 .gitea/actions/trivy-db-cache/action.yml diff --git a/.gitea/actions/trivy-db-cache/action.yml b/.gitea/actions/trivy-db-cache/action.yml new file mode 100644 index 00000000..835b1b0e --- /dev/null +++ b/.gitea/actions/trivy-db-cache/action.yml @@ -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 }}- diff --git a/.gitea/workflows/main.yml b/.gitea/workflows/main.yml index 5311e09a..fbd1638b 100644 --- a/.gitea/workflows/main.yml +++ b/.gitea/workflows/main.yml @@ -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. diff --git a/.gitea/workflows/pr.yml b/.gitea/workflows/pr.yml index fa082b97..fdf4ee97 100644 --- a/.gitea/workflows/pr.yml +++ b/.gitea/workflows/pr.yml @@ -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 diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 2b178cfc..bd306484 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -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 diff --git a/scripts/ci/trivy-scan.sh b/scripts/ci/trivy-scan.sh index b98b0bf9..92ae8459 100755 --- a/scripts/ci/trivy-scan.sh +++ b/scripts/ci/trivy-scan.sh @@ -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'