-
Notifications
You must be signed in to change notification settings - Fork 0
152 lines (142 loc) · 4.97 KB
/
Copy pathtrivy-scan.yml
File metadata and controls
152 lines (142 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: 'Trivy Security Scan'
on:
workflow_call:
inputs:
scan-type:
description: 'Type of scan (fs, image, repo, config, sbom)'
required: false
type: string
default: 'fs'
scan-ref:
description: 'Target to scan (path, image ref, or repo URL)'
required: false
type: string
default: '.'
severity:
description: 'Severity levels to report (UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL)'
required: false
type: string
default: 'HIGH,CRITICAL'
format:
description: 'Output format (table, json, sarif, template)'
required: false
type: string
default: 'sarif'
exit-code:
description: 'Exit code when vulnerabilities are found (0 or 1)'
required: false
type: string
default: '1'
upload-sarif:
description: 'Upload SARIF results to GitHub Security'
required: false
type: boolean
default: true
skip-files:
description: 'Comma-separated list of file paths to skip'
required: false
type: string
default: ''
skip-dirs:
description: 'Comma-separated list of directories to skip'
required: false
type: string
default: 'node_modules,dist,build,.git'
timeout:
description: 'Scan timeout duration'
required: false
type: string
default: '10m'
runs-on:
description: 'Runner label to execute the job on'
required: false
type: string
default: 'homelab-runners'
outputs:
findings-count:
description: 'Number of security findings'
value: ${{ jobs.scan.outputs.findings }}
sarif-path:
description: 'Path to SARIF output file'
value: ${{ jobs.scan.outputs.sarif-path }}
permissions:
contents: read
security-events: write
jobs:
scan:
name: Trivy Security Scan
runs-on: ${{ inputs.runs-on }}
timeout-minutes: 15
outputs:
findings: ${{ steps.scan.outputs.findings }}
sarif-path: 'trivy-results.sarif'
steps:
- name: Checkout repository
if: inputs.scan-type == 'fs' || inputs.scan-type == 'repo' || inputs.scan-type == 'config'
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- name: Run Trivy scanner
id: scan
uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 # v0.33.1
with:
scan-type: ${{ inputs.scan-type }}
scan-ref: ${{ inputs.scan-ref }}
format: ${{ inputs.format }}
output: 'trivy-results.${{ inputs.format == ''sarif'' && ''sarif'' || ''json'' }}'
severity: ${{ inputs.severity }}
exit-code: ${{ inputs.exit-code }}
skip-files: ${{ inputs.skip-files }}
skip-dirs: ${{ inputs.skip-dirs }}
timeout: ${{ inputs.timeout }}
trivyignores: '.trivyignore'
version: 'v0.69.3'
- name: Count findings
id: count
if: always()
shell: bash
run: |
if [ -f "trivy-results.sarif" ]; then
FINDINGS=$(jq '.runs[0].results | length' trivy-results.sarif)
echo "findings=$FINDINGS" >> "$GITHUB_OUTPUT"
echo "Found $FINDINGS security findings"
else
echo "findings=0" >> "$GITHUB_OUTPUT"
fi
- name: Upload Trivy scan results to GitHub Security
if: inputs.upload-sarif && inputs.format == 'sarif' && always()
uses: github/codeql-action/upload-sarif@48ab28a6f5dbc2a99bf1e0131198dd8f1df78169 # v3.28.0
with:
sarif_file: 'trivy-results.sarif'
category: 'trivy-${{ inputs.scan-type }}'
- name: Upload scan results as artifact
if: always()
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
with:
name: trivy-results-${{ inputs.scan-type }}
path: trivy-results.*
retention-days: 30
- name: Generate scan summary
if: always()
shell: bash
run: |
{
echo "## 🔍 Trivy Security Scan Results"
echo ""
echo "**Scan Type:** \`${{ inputs.scan-type }}\`"
echo "**Target:** \`${{ inputs.scan-ref }}\`"
echo "**Severity:** \`${{ inputs.severity }}\`"
echo ""
} >> "$GITHUB_STEP_SUMMARY"
if [ -f "trivy-results.sarif" ]; then
FINDINGS=$(jq '.runs[0].results | length' trivy-results.sarif)
if [ "$FINDINGS" -eq 0 ]; then
echo "✅ **No vulnerabilities found**" >> "$GITHUB_STEP_SUMMARY"
else
{
echo "⚠️ **Found $FINDINGS security findings**"
echo ""
echo "Review the Security tab for detailed findings."
} >> "$GITHUB_STEP_SUMMARY"
fi
else
echo "ℹ️ Scan completed (non-SARIF format)" >> "$GITHUB_STEP_SUMMARY"
fi