|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +name: Trivy Changes |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + merge_group: |
| 9 | + types: [checks_requested] |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + base_sha: |
| 13 | + description: Base commit SHA to compare |
| 14 | + required: true |
| 15 | + type: string |
| 16 | + head_sha: |
| 17 | + description: Candidate commit SHA to compare |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + |
| 21 | +permissions: |
| 22 | + contents: read |
| 23 | + |
| 24 | +concurrency: |
| 25 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 26 | + cancel-in-progress: true |
| 27 | + |
| 28 | +jobs: |
| 29 | + changes: |
| 30 | + name: Detect deployment configuration changes |
| 31 | + runs-on: ubuntu-latest |
| 32 | + permissions: |
| 33 | + contents: read |
| 34 | + pull-requests: read |
| 35 | + outputs: |
| 36 | + should_run: ${{ steps.default.outputs.should_run || steps.changed.outputs.any_modified }} |
| 37 | + steps: |
| 38 | + - id: default |
| 39 | + if: github.event_name != 'pull_request' |
| 40 | + run: echo "should_run=true" >> "$GITHUB_OUTPUT" |
| 41 | + |
| 42 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 43 | + if: github.event_name == 'pull_request' |
| 44 | + with: |
| 45 | + persist-credentials: false |
| 46 | + |
| 47 | + - id: changed |
| 48 | + if: github.event_name == 'pull_request' |
| 49 | + uses: tj-actions/changed-files@aa08304bd477b800d468db44fe10f6c61f7f7b11 # v42.1.0 |
| 50 | + with: |
| 51 | + # `any_modified` covers deletions, which `any_changed` omits, and a |
| 52 | + # failed diff has to fail the job: both otherwise report no relevant |
| 53 | + # change, and removing the scanner or a value fixture would skip the |
| 54 | + # scan behind a green status. |
| 55 | + fail_on_initial_diff_error: true |
| 56 | + files: | |
| 57 | + deploy/docker/** |
| 58 | + deploy/helm/** |
| 59 | + deploy/kube/** |
| 60 | + .trivyignore.yaml |
| 61 | + flake.nix |
| 62 | + flake.lock |
| 63 | + tasks/scripts/trivy-scan.sh |
| 64 | + tasks/scripts/trivy-scan-test.sh |
| 65 | + tasks/scripts/trivy-config-report.jq |
| 66 | + .github/workflows/trivy-scan.yml |
| 67 | + .github/workflows/trivy-changes.yml |
| 68 | +
|
| 69 | + scan: |
| 70 | + name: Scan changed deployment configuration |
| 71 | + needs: changes |
| 72 | + if: needs.changes.outputs.should_run == 'true' |
| 73 | + runs-on: ubuntu-latest |
| 74 | + timeout-minutes: 30 |
| 75 | + env: |
| 76 | + BASE_REF: ${{ github.event.pull_request.base.sha || github.event.merge_group.base_sha || inputs.base_sha }} |
| 77 | + HEAD_REF: ${{ inputs.head_sha || github.sha }} |
| 78 | + defaults: |
| 79 | + run: |
| 80 | + shell: nix develop --command bash -euo pipefail {0} |
| 81 | + steps: |
| 82 | + - name: Check out candidate |
| 83 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 84 | + with: |
| 85 | + ref: ${{ env.HEAD_REF }} |
| 86 | + persist-credentials: false |
| 87 | + |
| 88 | + - name: Check out baseline |
| 89 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 90 | + with: |
| 91 | + ref: ${{ env.BASE_REF }} |
| 92 | + path: .trivy-base |
| 93 | + persist-credentials: false |
| 94 | + |
| 95 | + - name: Set up Nix |
| 96 | + uses: ./.github/actions/setup-nix |
| 97 | + |
| 98 | + - name: Test report comparison |
| 99 | + run: tasks/scripts/trivy-scan-test.sh |
| 100 | + |
| 101 | + - name: Validate candidate ignore policy |
| 102 | + run: tasks/scripts/trivy-scan.sh validate-ignore |
| 103 | + |
| 104 | + # Ignore-policy changes take effect only after merge. Applying the |
| 105 | + # baseline policy to both scans prevents a candidate from exempting a new |
| 106 | + # finding in the same change that introduces it. |
| 107 | + - name: Prepare baseline ignore policy |
| 108 | + run: | |
| 109 | + if [ -f .trivy-base/.trivyignore.yaml ]; then |
| 110 | + cp .trivy-base/.trivyignore.yaml "$RUNNER_TEMP/trivy-baseline-ignore.yaml" |
| 111 | + else |
| 112 | + printf 'misconfigurations: []\n' >"$RUNNER_TEMP/trivy-baseline-ignore.yaml" |
| 113 | + fi |
| 114 | +
|
| 115 | + - name: Scan baseline |
| 116 | + env: |
| 117 | + TRIVY_SOURCE_ROOT: ${{ github.workspace }}/.trivy-base |
| 118 | + TRIVY_IGNORE_FILE: ${{ runner.temp }}/trivy-baseline-ignore.yaml |
| 119 | + TRIVY_REPORT_DIR: ${{ runner.temp }}/trivy-base |
| 120 | + run: | |
| 121 | + mkdir -p "$TRIVY_REPORT_DIR" |
| 122 | + "$GITHUB_WORKSPACE/tasks/scripts/trivy-scan.sh" config |
| 123 | +
|
| 124 | + - name: Scan candidate |
| 125 | + env: |
| 126 | + TRIVY_IGNORE_FILE: ${{ runner.temp }}/trivy-baseline-ignore.yaml |
| 127 | + TRIVY_REPORT_DIR: ${{ runner.temp }}/trivy-head |
| 128 | + run: | |
| 129 | + mkdir -p "$TRIVY_REPORT_DIR" |
| 130 | + tasks/scripts/trivy-scan.sh config |
| 131 | +
|
| 132 | + - name: Reject new high or critical findings |
| 133 | + run: | |
| 134 | + tasks/scripts/trivy-scan.sh gate-config-diff \ |
| 135 | + "$RUNNER_TEMP/trivy-base" "$RUNNER_TEMP/trivy-head" |
| 136 | +
|
| 137 | + - name: Upload reports |
| 138 | + if: always() |
| 139 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 |
| 140 | + with: |
| 141 | + name: trivy-changes-${{ github.run_id }} |
| 142 | + path: | |
| 143 | + ${{ runner.temp }}/trivy-base |
| 144 | + ${{ runner.temp }}/trivy-head |
| 145 | + if-no-files-found: ignore |
| 146 | + retention-days: 14 |
| 147 | + |
| 148 | + result: |
| 149 | + name: OpenShell / Trivy Changes |
| 150 | + needs: [changes, scan] |
| 151 | + if: always() |
| 152 | + runs-on: ubuntu-latest |
| 153 | + steps: |
| 154 | + - name: Check scan result |
| 155 | + env: |
| 156 | + CHANGES_RESULT: ${{ needs.changes.result }} |
| 157 | + SHOULD_RUN: ${{ needs.changes.outputs.should_run }} |
| 158 | + SCAN_RESULT: ${{ needs.scan.result }} |
| 159 | + run: | |
| 160 | + set -euo pipefail |
| 161 | + if [ "$CHANGES_RESULT" != "success" ]; then |
| 162 | + echo "::error::Change detection concluded $CHANGES_RESULT." |
| 163 | + exit 1 |
| 164 | + fi |
| 165 | + if [ "$SHOULD_RUN" = "true" ] && [ "$SCAN_RESULT" != "success" ]; then |
| 166 | + echo "::error::Trivy scan concluded $SCAN_RESULT." |
| 167 | + exit 1 |
| 168 | + fi |
| 169 | + if [ "$SHOULD_RUN" != "true" ]; then |
| 170 | + echo "No Helm or Dockerfile changes to scan." |
| 171 | + fi |
0 commit comments