Skip to content

Commit e3ecf62

Browse files
authored
Update trivy.yml
1 parent 129bec0 commit e3ecf62

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

.github/workflows/trivy.yml

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,69 @@
1-
name: Trivy Scan
1+
name: Trivy Image Scan
2+
23
on:
34
push:
45
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
58

69
jobs:
7-
scan:
10+
trivy-scan:
811
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
914

1015
steps:
11-
- name: Checkout code
16+
- name: Checkout repository
1217
uses: actions/checkout@v4
1318

19+
- name: Set up QEMU
20+
uses: docker/setup-qemu-action@v2
21+
22+
- name: Set up Docker Buildx
23+
uses: docker/setup-buildx-action@v2
24+
1425
- name: Build Docker image
15-
run: docker build -t devsecops-scan .
26+
run: |
27+
set -euo pipefail
28+
REPO_OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
29+
REPO_NAME=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]')
30+
docker build -t ghcr.io/$REPO_OWNER/$REPO_NAME:scan-latest .
31+
32+
- name: Install Trivy via APT repo
33+
run: |
34+
set -euo pipefail
35+
sudo apt-get update
36+
sudo apt-get install -y gnupg wget apt-transport-https lsb-release
37+
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
38+
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/trivy.list
39+
sudo apt-get update
40+
sudo apt-get install -y trivy
41+
trivy --version
42+
43+
- name: Run Trivy Scan (fail on HIGH/CRITICAL) and save output
44+
run: |
45+
set -euo pipefail
46+
REPO_OWNER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]')
47+
REPO_NAME=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]')
48+
49+
# Run trivy and capture its exit code while saving output to file
50+
# trivy exit codes: 0 = no vulnerabilities found (or below severity filter)
51+
# 1 = vulnerabilities found (for severity filter) -> we want to fail pipeline
52+
trivy image --exit-code 1 --severity HIGH,CRITICAL --format table ghcr.io/$REPO_OWNER/$REPO_NAME:scan-latest | tee trivy-output.txt
53+
rc=${PIPESTATUS[0]:-0}
54+
if [ "$rc" -eq 1 ]; then
55+
echo "Trivy found HIGH/CRITICAL vulnerabilities (exit code $rc). See trivy-output.txt"
56+
exit 1
57+
elif [ "$rc" -ne 0 ]; then
58+
echo "Trivy returned unexpected exit code $rc"
59+
exit $rc
60+
else
61+
echo "No HIGH/CRITICAL vulnerabilities found."
62+
fi
1663
17-
- name: Run Trivy (action)
18-
uses: aquasecurity/trivy-action@v0.9.0
64+
- name: Upload Trivy Output
65+
if: always()
66+
uses: actions/upload-artifact@v4
1967
with:
20-
image-ref: devsecops-scan
21-
format: table
22-
severity: HIGH,CRITICAL
23-
exit-code: 1
68+
name: trivy-output
69+
path: trivy-output.txt

0 commit comments

Comments
 (0)