|
| 1 | +name: Build, scan, and test container image |
| 2 | + |
| 3 | +on: [pull_request] |
| 4 | + |
| 5 | +env: |
| 6 | + REGISTRY: docker.io |
| 7 | + IMAGE_NAME: appwrite/base |
| 8 | + TAG: ${{ github.event.release.tag_name }} |
| 9 | + |
| 10 | +jobs: |
| 11 | + build: |
| 12 | + runs-on: ubuntu-24.04 |
| 13 | + permissions: |
| 14 | + contents: read |
| 15 | + pull-requests: write |
| 16 | + steps: |
| 17 | + |
| 18 | + - name: Check out code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + ref: ${{ github.event.pull_request.head.sha }} |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Build an image from Dockerfile |
| 25 | + run: | |
| 26 | + docker build -t appwrite/docker-base:${{ github.sha }} . |
| 27 | +
|
| 28 | + scan: |
| 29 | + runs-on: ubuntu-24.04 |
| 30 | + steps: |
| 31 | + - name: Run Trivy vulnerability scanner on image |
| 32 | + uses: aquasecurity/trivy-action@0.35.0 |
| 33 | + with: |
| 34 | + image-ref: 'appwrite/docker-base:${{ github.sha }}' |
| 35 | + format: 'template' |
| 36 | + template: '@/contrib/sarif.tpl' |
| 37 | + output: 'trivy-results.sarif' |
| 38 | + severity: 'CRITICAL,HIGH' |
| 39 | + |
| 40 | + - name: Run Trivy vulnerability scanner on source code |
| 41 | + uses: aquasecurity/trivy-action@0.35.0 |
| 42 | + with: |
| 43 | + scan-type: 'fs' |
| 44 | + scan-ref: '.' |
| 45 | + format: 'json' |
| 46 | + output: 'trivy-fs-results.json' |
| 47 | + severity: 'CRITICAL,HIGH' |
| 48 | + |
| 49 | + - name: Process Trivy scan results |
| 50 | + id: process-results |
| 51 | + uses: actions/github-script@v7 |
| 52 | + with: |
| 53 | + script: | |
| 54 | + const fs = require('fs'); |
| 55 | + let commentBody = '## Security Scan Results for PR\n\n'; |
| 56 | + function processResults(results, title) { |
| 57 | + let sectionBody = `### ${title}\n\n`; |
| 58 | + if (results.Results && results.Results.some(result => result.Vulnerabilities && result.Vulnerabilities.length > 0)) { |
| 59 | + sectionBody += '| Package | Version | Vulnerability | Severity |\n'; |
| 60 | + sectionBody += '|---------|---------|----------------|----------|\n'; |
| 61 | + const uniqueVulns = new Set(); |
| 62 | + results.Results.forEach(result => { |
| 63 | + if (result.Vulnerabilities) { |
| 64 | + result.Vulnerabilities.forEach(vuln => { |
| 65 | + const vulnKey = `${vuln.PkgName}-${vuln.InstalledVersion}-${vuln.VulnerabilityID}`; |
| 66 | + if (!uniqueVulns.has(vulnKey)) { |
| 67 | + uniqueVulns.add(vulnKey); |
| 68 | + sectionBody += `| ${vuln.PkgName} | ${vuln.InstalledVersion} | [${vuln.VulnerabilityID}](https://nvd.nist.gov/vuln/detail/${vuln.VulnerabilityID}) | ${vuln.Severity} |\n`; |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + }); |
| 73 | + } else { |
| 74 | + sectionBody += '🎉 No vulnerabilities found!\n'; |
| 75 | + } |
| 76 | + return sectionBody; |
| 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 | + commentBody += processResults(imageResults, "Docker Image Scan Results"); |
| 82 | + commentBody += '\n'; |
| 83 | + commentBody += processResults(fsResults, "Source Code Scan Results"); |
| 84 | + } catch (error) { |
| 85 | + commentBody += `There was an error while running the security scan: ${error.message}\n`; |
| 86 | + commentBody += 'Please contact the core team for assistance.'; |
| 87 | + } |
| 88 | + core.setOutput('comment-body', commentBody); |
| 89 | +
|
| 90 | + - name: Find Comment |
| 91 | + uses: peter-evans/find-comment@v3 |
| 92 | + id: fc |
| 93 | + with: |
| 94 | + issue-number: ${{ github.event.pull_request.number }} |
| 95 | + comment-author: 'github-actions[bot]' |
| 96 | + body-includes: Security Scan Results for PR |
| 97 | + |
| 98 | + - name: Create or update comment |
| 99 | + uses: peter-evans/create-or-update-comment@v3 |
| 100 | + with: |
| 101 | + issue-number: ${{ github.event.pull_request.number }} |
| 102 | + comment-id: ${{ steps.fc.outputs.comment-id }} |
| 103 | + body: ${{ steps.process-results.outputs.comment-body }} |
| 104 | + edit-mode: replace |
| 105 | + |
| 106 | + test: |
| 107 | + runs-on: ubuntu-24.04 |
| 108 | + steps: |
| 109 | + - name: Setup container structure test |
| 110 | + run: | |
| 111 | + curl -LO https://storage.googleapis.com/container-structure-test/latest/container-structure-test-linux-amd64 |
| 112 | + chmod +x container-structure-test-linux-amd64 |
| 113 | + sudo mv container-structure-test-linux-amd64 /usr/local/bin/container-structure-test |
| 114 | +
|
| 115 | + - name: Run container structure test |
| 116 | + run: | |
| 117 | + docker build -t appwrite-base-test . |
| 118 | + container-structure-test test --image appwrite-base-test --config tests.yaml |
0 commit comments