Skip to content

Commit b2cee1d

Browse files
committed
UPDATED pipeline workflow to include trivy scanning in PRs of this project
1 parent 79c1eea commit b2cee1d

5 files changed

Lines changed: 110 additions & 30 deletions

File tree

.github/workflows/pr-scan.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: PR Security Scan
2+
3+
on:
4+
pull_request_target:
5+
types: [opened, synchronize, reopened]
6+
7+
jobs:
8+
scan:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
contents: read
12+
pull-requests: write
13+
steps:
14+
- name: Check out code
15+
uses: actions/checkout@v4
16+
with:
17+
ref: ${{ github.event.pull_request.head.sha }}
18+
fetch-depth: 0
19+
submodules: 'recursive'
20+
21+
- name: Build the Docker image
22+
uses: docker/build-push-action@v5
23+
with:
24+
context: .
25+
push: false
26+
load: true
27+
tags: pr_image:${{ github.sha }}
28+
29+
- name: Run Trivy vulnerability scanner on image
30+
uses: aquasecurity/trivy-action@0.20.0
31+
with:
32+
image-ref: 'pr_image:${{ github.sha }}'
33+
format: 'json'
34+
output: 'trivy-image-results.json'
35+
severity: 'CRITICAL,HIGH'
36+
37+
- name: Run Trivy vulnerability scanner on source code
38+
uses: aquasecurity/trivy-action@0.20.0
39+
with:
40+
scan-type: 'fs'
41+
scan-ref: '.'
42+
format: 'json'
43+
output: 'trivy-fs-results.json'
44+
severity: 'CRITICAL,HIGH'
45+
46+
- name: Process Trivy scan results
47+
id: process-results
48+
uses: actions/github-script@v7
49+
with:
50+
script: |
51+
const fs = require('fs');
52+
let commentBody = '## Security Scan Results for PR\n\n';
53+
54+
function processResults(results, title) {
55+
let sectionBody = `### ${title}\n\n`;
56+
if (results.Results && results.Results.some(result => result.Vulnerabilities && result.Vulnerabilities.length > 0)) {
57+
sectionBody += '| Package | Version | Vulnerability | Severity |\n';
58+
sectionBody += '|---------|---------|----------------|----------|\n';
59+
60+
const uniqueVulns = new Set();
61+
results.Results.forEach(result => {
62+
if (result.Vulnerabilities) {
63+
result.Vulnerabilities.forEach(vuln => {
64+
const vulnKey = `${vuln.PkgName}-${vuln.InstalledVersion}-${vuln.VulnerabilityID}`;
65+
if (!uniqueVulns.has(vulnKey)) {
66+
uniqueVulns.add(vulnKey);
67+
sectionBody += `| ${vuln.PkgName} | ${vuln.InstalledVersion} | [${vuln.VulnerabilityID}](https://nvd.nist.gov/vuln/detail/${vuln.VulnerabilityID}) | ${vuln.Severity} |\n`;
68+
}
69+
});
70+
}
71+
});
72+
} else {
73+
sectionBody += '🎉 No vulnerabilities found!\n';
74+
}
75+
return sectionBody;
76+
}
77+
78+
try {
79+
const imageResults = JSON.parse(fs.readFileSync('trivy-image-results.json', 'utf8'));
80+
const fsResults = JSON.parse(fs.readFileSync('trivy-fs-results.json', 'utf8'));
81+
82+
commentBody += processResults(imageResults, "Docker Image Scan Results");
83+
commentBody += '\n';
84+
commentBody += processResults(fsResults, "Source Code Scan Results");
85+
86+
} catch (error) {
87+
commentBody += `There was an error while running the security scan: ${error.message}\n`;
88+
commentBody += 'Please contact the core team for assistance.';
89+
}
90+
91+
core.setOutput('comment-body', commentBody);
92+
- name: Find Comment
93+
uses: peter-evans/find-comment@v3
94+
id: fc
95+
with:
96+
issue-number: ${{ github.event.pull_request.number }}
97+
comment-author: 'github-actions[bot]'
98+
body-includes: Security Scan Results for PR
99+
100+
- name: Create or update comment
101+
uses: peter-evans/create-or-update-comment@v3
102+
with:
103+
issue-number: ${{ github.event.pull_request.number }}
104+
comment-id: ${{ steps.fc.outputs.comment-id }}
105+
body: ${{ steps.process-results.outputs.comment-body }}
106+
edit-mode: replace

.github/workflows/test.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

.github/workflows/trivy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# separate terms of service, privacy policy, and support
44
# documentation.
55

6-
name: trivy
6+
name: Scheduled Trivy Vulnerability Scanning
77

88
on:
99
push:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.idea
22
*.logs
33
NOTES*.md
4-
trivy-image-results.json
4+
trivy-*-results.json

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ docker buildx build --tag appwrite/base:latest .
4545
Multi-arch building.
4646

4747
```shell
48-
docker buildx build --platform linux/amd64,linux/arm64/v8,linux/ppc64le --tag appwrite/base:latest .
48+
docker buildx build --platform linux/amd64,linux/arm64/v8,linux/ppc64le --push --tag appwrite/base:latest .
4949
```
5050

5151
## Scan
5252

5353
```shell
54-
trivy image appwrite/base:latest
54+
trivy image --format json --pkg-types os,library --severity CRITICAL,HIGH --output trivy-image-results.json appwrite/base:latest
5555
```
5656

5757
## Test

0 commit comments

Comments
 (0)