Skip to content

Commit 0172ef4

Browse files
committed
Validate the Trivy data cache before scanning
The upstream action caches its data once per date, while Trivy considers the data invalid 24 hours after it was generated. As a result, the action cache is invalid for a significant portion of each day. Issue: PGO-1893
1 parent 96132b8 commit 0172ef4

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

.github/workflows/trivy.yaml

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,57 @@ env:
1212
# https://github.com/actions/setup-go/issues/457
1313
GOTOOLCHAIN: local
1414

15+
# Manage the Trivy data directory until upstream can do it reliably
16+
# https://github.com/aquasecurity/trivy-action/issues/389
17+
#
18+
# NOTE: This must match the default "cache-dir" upstream:
19+
# https://github.com/aquasecurity/trivy-action/blob/-/action.yaml
20+
TRIVY_CACHE_DIR: ${{ github.workspace }}/.cache/trivy
21+
1522
jobs:
23+
cache:
24+
runs-on: ubuntu-latest
25+
steps:
26+
- uses: aquasecurity/[email protected]
27+
with:
28+
cache: true
29+
30+
# The "aquasecurity/trivy-action" looks for data in the GitHub action
31+
# cache under a key with today's date.
32+
# - https://github.com/actions/cache/blob/-/restore#readme
33+
# - https://github.com/aquasecurity/trivy-action/blob/-/action.yaml
34+
- id: values
35+
run: |
36+
(
37+
date +'date=%Y-%m-%d'
38+
echo "glob=${TRIVY_CACHE_DIR}/*/metadata.json"
39+
) |
40+
tee --append $GITHUB_OUTPUT
41+
- id: restore
42+
uses: actions/cache/restore@v4
43+
with:
44+
key: cache-trivy-${{ steps.values.outputs.date }}
45+
path: ${{ env.TRIVY_CACHE_DIR }}
46+
restore-keys: cache-trivy-
47+
48+
# Validate or update the Trivy data cache.
49+
- id: validate
50+
env:
51+
METADATA_HASH: ${{ hashFiles(steps.values.outputs.glob) }}
52+
run: |
53+
<<< "before=${METADATA_HASH}" tee --append $GITHUB_OUTPUT
54+
trivy filesystem --download-db-only --scanners license,secret,vuln --quiet
55+
56+
# Save any successful changes back to the GitHub action cache.
57+
# - https://github.com/actions/cache/blob/-/save#readme
58+
- if: ${{ hashFiles(steps.values.outputs.glob) != steps.validate.outputs.before }}
59+
uses: actions/cache/save@v4
60+
with:
61+
key: ${{ steps.restore.outputs.cache-primary-key }}
62+
path: ${{ env.TRIVY_CACHE_DIR }}
63+
1664
licenses:
65+
needs: [cache]
1766
runs-on: ubuntu-latest
1867
steps:
1968
- uses: actions/checkout@v4
@@ -38,6 +87,7 @@ jobs:
3887
permissions:
3988
security-events: write
4089

90+
needs: [cache]
4191
runs-on: ubuntu-latest
4292
steps:
4393
- uses: actions/checkout@v4
@@ -49,11 +99,7 @@ jobs:
4999
uses: aquasecurity/[email protected]
50100
with:
51101
scan-type: filesystem
52-
hide-progress: true
53102
scanners: secret,vuln
54-
# Manage the cache only once during this workflow.
55-
# - https://github.com/aquasecurity/trivy-action#cache
56-
cache: true
57103

58104
# Produce a SARIF report of actionable results. This step fails only when
59105
# Trivy is unable to scan.
@@ -65,9 +111,6 @@ jobs:
65111
format: 'sarif'
66112
output: 'trivy-results.sarif'
67113
scanners: secret,vuln
68-
# Use the cache downloaded in a prior step.
69-
# - https://github.com/aquasecurity/trivy-action#cache
70-
cache: false
71114

72115
# Submit the SARIF report to GitHub code scanning. Pull requests checks
73116
# succeed or fail according to branch protection rules.

0 commit comments

Comments
 (0)