Skip to content

Commit 7a94dda

Browse files
committed
ci: integrate automated Trivy security scanning in CI workflows
- Add explicit permissions for contents, packages, and security-events to the Docker GitHub Actions workflow - Integrate Trivy vulnerability scanning and results upload into the Docker workflow - Add a dedicated GitHub Actions workflow for Trivy security scanning of both repository files and Docker images, with scheduled, push, and pull request triggers - Ensure Trivy SARIF results are uploaded to the GitHub Security tab after scans Signed-off-by: appleboy <[email protected]>
1 parent 1cc99b6 commit 7a94dda

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

.github/workflows/docker.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ on:
1010
branches:
1111
- "master"
1212

13+
permissions:
14+
contents: read
15+
packages: write
16+
security-events: write
17+
1318
jobs:
1419
build-docker:
1520
runs-on: ubuntu-latest
@@ -60,7 +65,33 @@ jobs:
6065
type=semver,pattern={{major}}.{{minor}}
6166
type=semver,pattern={{major}}
6267
68+
- name: Build image for scanning
69+
uses: docker/build-push-action@v6
70+
with:
71+
context: .
72+
platforms: linux/amd64
73+
file: docker/Dockerfile
74+
push: false
75+
load: true
76+
tags: ${{ github.repository }}:scan
77+
78+
- name: Run Trivy vulnerability scanner
79+
uses: aquasecurity/trivy-action@master
80+
with:
81+
image-ref: "${{ github.repository }}:scan"
82+
format: "sarif"
83+
output: "trivy-results.sarif"
84+
severity: "CRITICAL,HIGH"
85+
exit-code: "1"
86+
87+
- name: Upload Trivy scan results to GitHub Security tab
88+
uses: github/codeql-action/upload-sarif@v3
89+
if: always()
90+
with:
91+
sarif_file: "trivy-results.sarif"
92+
6393
- name: Build and push
94+
if: success()
6495
uses: docker/build-push-action@v6
6596
with:
6697
context: .

.github/workflows/trivy.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
name: Trivy Security Scan
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
schedule:
11+
# Run daily at 00:00 UTC
12+
- cron: "0 0 * * *"
13+
workflow_dispatch:
14+
15+
permissions:
16+
contents: read
17+
security-events: write
18+
19+
jobs:
20+
trivy-repo-scan:
21+
name: Trivy Repository Scan
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout repository
25+
uses: actions/checkout@v4
26+
27+
- name: Run Trivy vulnerability scanner (repo)
28+
uses: aquasecurity/trivy-action@master
29+
with:
30+
scan-type: "fs"
31+
scan-ref: "."
32+
format: "sarif"
33+
output: "trivy-repo-results.sarif"
34+
severity: "CRITICAL,HIGH"
35+
36+
- name: Upload Trivy scan results to GitHub Security tab
37+
uses: github/codeql-action/upload-sarif@v3
38+
if: always()
39+
with:
40+
sarif_file: "trivy-repo-results.sarif"
41+
42+
trivy-image-scan:
43+
name: Trivy Image Scan
44+
runs-on: ubuntu-latest
45+
steps:
46+
- name: Checkout repository
47+
uses: actions/checkout@v4
48+
49+
- name: Setup go
50+
uses: actions/setup-go@v5
51+
with:
52+
go-version-file: go.mod
53+
check-latest: true
54+
55+
- name: Build binary
56+
run: |
57+
make build_linux_amd64
58+
59+
- name: Set up Docker Buildx
60+
uses: docker/setup-buildx-action@v3
61+
62+
- name: Build Docker image for scanning
63+
uses: docker/build-push-action@v6
64+
with:
65+
context: .
66+
file: docker/Dockerfile
67+
platforms: linux/amd64
68+
push: false
69+
load: true
70+
tags: drone-ssh:scan
71+
72+
- name: Run Trivy vulnerability scanner (image)
73+
uses: aquasecurity/trivy-action@master
74+
with:
75+
image-ref: "drone-ssh:scan"
76+
format: "sarif"
77+
output: "trivy-image-results.sarif"
78+
severity: "CRITICAL,HIGH"
79+
80+
- name: Upload Trivy image scan results to GitHub Security tab
81+
uses: github/codeql-action/upload-sarif@v3
82+
if: always()
83+
with:
84+
sarif_file: "trivy-image-results.sarif"
85+
category: "trivy-image"

0 commit comments

Comments
 (0)