Skip to content

ci(trivy): cache Trivy vulnerability DB across runs - #1

Merged
tooming merged 1 commit into
mainfrom
ci/cache-fix
Sep 14, 2026
Merged

tooming merged 1 commit into
mainfrom
ci/cache-fix

Conversation

@tooming

@tooming tooming commented Sep 14, 2026

Copy link
Copy Markdown
Owner

What

Adds an actions/cache step in .github/workflows/trivy.yml, before the aquasecurity/trivy-action step, to persist Trivy's vulnerability/secret DB cache directory (~/.cache/trivy) across workflow runs.

Why

Every run of this workflow currently downloads the full Trivy DB from scratch, which is slow and adds unnecessary network load to every push/PR run. Caching it lets most runs reuse a warm DB and only pull an incremental update.

Key design point: the cache key

A naive run-id-based key (e.g. ${{ github.run_id }}) would never hit, since the run id is unique per run and no future run can match it. Instead this uses a coarser, date-based key:

  • A prior step writes the current UTC date to $GITHUB_ENV (CACHE_DATE).
  • The cache key is ${{ runner.os }}-trivy-db-${{ env.CACHE_DATE }}.
  • restore-keys: ${{ runner.os }}-trivy-db- lets any prior day's cache be restored as a base when today's exact key hasn't been saved yet.

This is safe even when a stale (prior-day) cache is restored, because trivy-action validates the DB's freshness/integrity itself before trusting a restored cache — a stale hit just costs a smaller incremental update instead of a full download, never a wrong/outdated scan result.

Validation

  • python3 -c "import yaml; yaml.safe_load(open('.github/workflows/trivy.yml'))" — well-formed YAML.
  • actionlint .github/workflows/trivy.yml — clean, no findings.

This is a CI-config-only change; no application code, tests, or build pipeline touched.

Found via a CI-speed audit.

🤖 Generated with Claude Code

Add an actions/cache step before aquasecurity/trivy-action to persist
Trivy's DB cache directory (~/.cache/trivy) between workflow runs.

The cache key is date-based (UTC day, written to $GITHUB_ENV by a
prior step) rather than run-id-based, since a run-id key never hits
on a later run. restore-keys falls back to any prior day's cache as
a base, and trivy-action itself validates DB freshness/integrity
before trusting a restored cache, so a stale hit just costs a
smaller incremental update instead of a full DB download - never a
wrong scan.

Found via a CI-speed audit.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@tooming
tooming merged commit d5c6c44 into main Sep 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant