Skip to content

Commit 5ec3ea8

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 5ec3ea8

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

.github/workflows/trivy.yaml

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,55 @@ 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+
28+
# The "aquasecurity/trivy-action" looks for data in the GitHub action
29+
# cache under a key with today's date.
30+
# - https://github.com/actions/cache/blob/-/restore#readme
31+
# - https://github.com/aquasecurity/trivy-action/blob/-/action.yaml
32+
- id: values
33+
run: |
34+
(
35+
date +'date=%Y-%m-%d'
36+
echo "glob=${TRIVY_CACHE_DIR}/*/metadata.json"
37+
) |
38+
tee --append $GITHUB_OUTPUT
39+
- id: restore
40+
uses: actions/cache/restore@v4
41+
with:
42+
key: cache-trivy-${{ steps.values.outputs.date }}
43+
path: ${{ env.TRIVY_CACHE_DIR }}
44+
restore-keys: cache-trivy-
45+
46+
# Validate or update the Trivy data cache.
47+
- id: validate
48+
env:
49+
METADATA_HASH: ${{ hashFiles(steps.values.outputs.glob) }}
50+
run: |
51+
<<< "before=${METADATA_HASH}" tee --append $GITHUB_OUTPUT
52+
trivy filesystem --download-db-only --scanners license,secret,vuln --quiet
53+
54+
# Save any successful changes back to the GitHub action cache.
55+
# - https://github.com/actions/cache/blob/-/save#readme
56+
- if: ${{ hashFiles(steps.values.outputs.glob) != steps.validate.outputs.before }}
57+
uses: actions/cache/save@v4
58+
with:
59+
key: ${{ steps.restore.outputs.cache-primary-key }}
60+
path: ${{ env.TRIVY_CACHE_DIR }}
61+
1662
licenses:
63+
needs: [cache]
1764
runs-on: ubuntu-latest
1865
steps:
1966
- uses: actions/checkout@v4
@@ -38,6 +85,7 @@ jobs:
3885
permissions:
3986
security-events: write
4087

88+
needs: [cache]
4189
runs-on: ubuntu-latest
4290
steps:
4391
- uses: actions/checkout@v4
@@ -49,11 +97,7 @@ jobs:
4997
uses: aquasecurity/[email protected]
5098
with:
5199
scan-type: filesystem
52-
hide-progress: true
53100
scanners: secret,vuln
54-
# Manage the cache only once during this workflow.
55-
# - https://github.com/aquasecurity/trivy-action#cache
56-
cache: true
57101

58102
# Produce a SARIF report of actionable results. This step fails only when
59103
# Trivy is unable to scan.
@@ -65,9 +109,6 @@ jobs:
65109
format: 'sarif'
66110
output: 'trivy-results.sarif'
67111
scanners: secret,vuln
68-
# Use the cache downloaded in a prior step.
69-
# - https://github.com/aquasecurity/trivy-action#cache
70-
cache: false
71112

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

0 commit comments

Comments
 (0)