Skip to content

Commit 2112c3c

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 2112c3c

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

.github/workflows/trivy.yaml

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

91+
needs: [cache]
4192
runs-on: ubuntu-latest
4293
steps:
4394
- uses: actions/checkout@v4
@@ -49,11 +100,7 @@ jobs:
49100
uses: aquasecurity/[email protected]
50101
with:
51102
scan-type: filesystem
52-
hide-progress: true
53103
scanners: secret,vuln
54-
# Manage the cache only once during this workflow.
55-
# - https://github.com/aquasecurity/trivy-action#cache
56-
cache: true
57104

58105
# Produce a SARIF report of actionable results. This step fails only when
59106
# Trivy is unable to scan.
@@ -65,9 +112,6 @@ jobs:
65112
format: 'sarif'
66113
output: 'trivy-results.sarif'
67114
scanners: secret,vuln
68-
# Use the cache downloaded in a prior step.
69-
# - https://github.com/aquasecurity/trivy-action#cache
70-
cache: false
71115

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

0 commit comments

Comments
 (0)