-
Notifications
You must be signed in to change notification settings - Fork 4
64 lines (54 loc) · 2.27 KB
/
trivy-scan.yml
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
name: Trivy Vulnerability Scan
on:
pull_request:
branches:
- develop
- master
workflow_dispatch: # Allows manual runs from the GitHub UI
jobs:
trivy-scan:
runs-on: ubuntu-latest
steps:
# Checkout the pull request code
- name: Checkout code
uses: actions/checkout@v4
# Set up Java 17
- name: Set up Java 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin' # Use Eclipse Temurin distribution for Java
# Install Trivy if not cached
- name: Install Trivy
run: |
TRIVY_LATEST_VERSION=$(curl --silent "https://api.github.com/repos/aquasecurity/trivy/releases/latest" | jq -r .tag_name)
wget https://github.com/aquasecurity/trivy/releases/download/${TRIVY_LATEST_VERSION}/trivy_${TRIVY_LATEST_VERSION#v}_Linux-64bit.deb
sudo dpkg -i trivy_${TRIVY_LATEST_VERSION#v}_Linux-64bit.deb
# Run Trivy to scan the repository or container image
- name: Run Trivy scan
id: trivy-scan
run: |
./gradlew dependencies --write-locks
trivy fs gradle.lockfile --format json --output trivy_report.json --severity HIGH,CRITICAL
/bin/rm -rf gradle/dependency-locks
/bin/rm gradle.lockfile
env:
TRIVY_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-db
TRIVY_JAVA_DB_REPOSITORY: public.ecr.aws/aquasecurity/trivy-java-db
# Check for high or critical vulnerabilities
- name: Check vulnerabilities
id: check-vulns
run: |
vuln_count=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "HIGH" or .Severity == "CRITICAL")] | length' trivy_report.json)
echo "Vulnerability count: $vuln_count"
if [ "$vuln_count" -gt 0 ]; then
echo "High or Critical vulnerabilities found!"
exit 1
else
echo "No high or critical vulnerabilities found."
fi
# Output the vulnerabilities in a human-readable format
- name: Output vulnerabilities
if: failure() # Run only if the previous step fails
run: |
jq '.Results[]?.Vulnerabilities[] | select(.Severity == "HIGH" or .Severity == "CRITICAL") | "\(.VulnerabilityID): \(.PkgName) - Severity: \(.Severity) - Fixed Version: \(.FixedVersion)"' trivy_report.json