|
12 | 12 | # https://github.com/actions/setup-go/issues/457 |
13 | 13 | GOTOOLCHAIN: local |
14 | 14 |
|
| 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 | + |
15 | 22 | 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 | + |
16 | 62 | licenses: |
| 63 | + needs: [cache] |
17 | 64 | runs-on: ubuntu-latest |
18 | 65 | steps: |
19 | 66 | - uses: actions/checkout@v4 |
|
38 | 85 | permissions: |
39 | 86 | security-events: write |
40 | 87 |
|
| 88 | + needs: [cache] |
41 | 89 | runs-on: ubuntu-latest |
42 | 90 | steps: |
43 | 91 | - uses: actions/checkout@v4 |
|
49 | 97 | uses: aquasecurity/[email protected] |
50 | 98 | with: |
51 | 99 | scan-type: filesystem |
52 | | - hide-progress: true |
53 | 100 | scanners: secret,vuln |
54 | | - # Manage the cache only once during this workflow. |
55 | | - # - https://github.com/aquasecurity/trivy-action#cache |
56 | | - cache: true |
57 | 101 |
|
58 | 102 | # Produce a SARIF report of actionable results. This step fails only when |
59 | 103 | # Trivy is unable to scan. |
|
65 | 109 | format: 'sarif' |
66 | 110 | output: 'trivy-results.sarif' |
67 | 111 | scanners: secret,vuln |
68 | | - # Use the cache downloaded in a prior step. |
69 | | - # - https://github.com/aquasecurity/trivy-action#cache |
70 | | - cache: false |
71 | 112 |
|
72 | 113 | # Submit the SARIF report to GitHub code scanning. Pull requests checks |
73 | 114 | # succeed or fail according to branch protection rules. |
|
0 commit comments