Files
clean-architecture-backend-…/.gitea/actions/trivy-db-cache/action.yml
T
DongHyeonka afbfb1cc73 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 본문에 적는다.
2026-11-12 18:56:52 +09:00

53 lines
2.8 KiB
YAML

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 }}-