Skip to content

Commit d99f12a

Browse files
authored
fix(ci): align Trivy change detection and scan baselines (#3277)
Signed-off-by: Adrien Langou <alangou@nvidia.com>
1 parent 9b4b63e commit d99f12a

4 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎.agents/skills/watch-github-actions/SKILL.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ gh run list --json databaseId,status,headBranch,url --jq '.[] | {id: .databaseId
125125

126126
## View Job Logs
127127

128+
For `Trivy Changes`, inspect the `Resolve PR baseline` step for the base and head
129+
SHAs. PR runs compare the tested merge commit with its
130+
first parent; change detection and scans must use the same pair. On reruns, do
131+
not substitute the current `main` tip or the event's older PR base SHA. Merge
132+
groups and manual runs use their explicit baseline. Findings are reported by
133+
`Reject new high or critical findings`; distinguish those from scanner failures.
134+
128135
View logs for a specific run:
129136

130137
```bash

‎.github/workflows/trivy-changes.yml‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
pull-requests: read
3535
outputs:
3636
should_run: ${{ steps.default.outputs.should_run || steps.changed.outputs.any_modified }}
37+
base_sha: ${{ steps.comparison.outputs.base_sha }}
3738
steps:
3839
- id: default
3940
if: github.event_name != 'pull_request'
@@ -42,12 +43,27 @@ jobs:
4243
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
4344
if: github.event_name == 'pull_request'
4445
with:
46+
ref: ${{ github.sha }}
47+
# Include both parents of the exact PR merge commit, even on reruns.
48+
fetch-depth: 2
4549
persist-credentials: false
4650

51+
- name: Resolve PR baseline
52+
id: comparison
53+
if: github.event_name == 'pull_request'
54+
run: |
55+
git rev-parse --verify HEAD^2 >/dev/null
56+
base_sha=$(git rev-parse --verify HEAD^1)
57+
echo "base_sha=$base_sha" >> "$GITHUB_OUTPUT"
58+
echo "Trivy comparison: $base_sha -> $(git rev-parse HEAD)"
59+
4760
- id: changed
4861
if: github.event_name == 'pull_request'
4962
uses: tj-actions/changed-files@aa08304bd477b800d468db44fe10f6c61f7f7b11 # v42.1.0
5063
with:
64+
base_sha: ${{ steps.comparison.outputs.base_sha }}
65+
sha: ${{ github.sha }}
66+
output_renamed_files_as_deleted_and_added: true
5167
# `any_modified` covers deletions, which `any_changed` omits, and a
5268
# failed diff has to fail the job: both otherwise report no relevant
5369
# change, and removing the scanner or a value fixture would skip the
@@ -73,7 +89,7 @@ jobs:
7389
runs-on: ubuntu-latest
7490
timeout-minutes: 30
7591
env:
76-
BASE_REF: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha || inputs.base_sha }}
92+
BASE_REF: ${{ needs.changes.outputs.base_sha || github.event.merge_group.base_sha || inputs.base_sha }}
7793
HEAD_REF: ${{ inputs.head_sha || github.sha }}
7894
defaults:
7995
run:

‎CI.md‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,16 @@ Use a fresh `TRIVY_REPORT_DIR` for each scan session. `prepare-sarif` creates
216216

217217
### Pull-request change gate
218218

219-
`Trivy Changes` scans the base and candidate when a pull request or merge group
220-
changes deployment configuration or scanner inputs. It fails only for new
221-
`HIGH` or `CRITICAL` misconfigurations and retains both report sets.
219+
`Trivy Changes` scans the base and candidate when a pull request changes
220+
deployment configuration or scanner inputs; merge groups and manual runs always
221+
scan. It fails only for new `HIGH` or `CRITICAL` misconfigurations and retains
222+
both report sets.
223+
224+
For pull requests, change detection compares the exact tested merge commit with
225+
its first parent. Both detection and scanning use that same pair. This excludes
226+
unrelated changes on `main` even when the event carries an older base SHA or a
227+
job is rerun. Merge groups use the event's base SHA; manual runs use the supplied
228+
base and head. A missing revision or invalid PR merge checkout fails the gate.
222229

223230
The candidate ignore file is validated, but the baseline policy applies to both
224231
scans so a change cannot exempt its own finding. Existing profiles are compared

‎architecture/build.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ references. Publication batches respect GitHub's limit of 20 SARIF runs.
519519
The PR/merge-group gate scans base and candidate with the same scanner and rejects
520520
new `HIGH` or `CRITICAL` configuration findings. Its stable
521521
`OpenShell / Trivy Changes` status succeeds when nothing relevant changed.
522+
For PRs, the baseline is the first parent of the exact merge commit being tested,
523+
not the event's potentially older base SHA or the current branch tip. Change
524+
detection and both scans use that same immutable pair, including on reruns.
525+
Merge groups and manual runs retain their explicit baseline and always scan.
522526
Image CVEs need the standalone scan. The reporting and gate invariants are:
523527

524528
- A structurally invalid Trivy report is an error, not an empty finding set.

0 commit comments

Comments
 (0)