|
| 1 | +name: Security Scan |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, master] |
| 6 | + pull_request: |
| 7 | + branches: [main, master] |
| 8 | + schedule: |
| 9 | + # Run weekly on Mondays at 9 AM UTC |
| 10 | + - cron: "0 9 * * 1" |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +jobs: |
| 14 | + security-scan: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Set up Docker Buildx |
| 22 | + uses: docker/setup-buildx-action@v3 |
| 23 | + |
| 24 | + - name: Build Docker image |
| 25 | + uses: docker/build-push-action@v5 |
| 26 | + with: |
| 27 | + context: . |
| 28 | + push: false |
| 29 | + load: true |
| 30 | + tags: wsi-slides-processor:${{ github.sha }} |
| 31 | + cache-from: type=gha |
| 32 | + cache-to: type=gha,mode=max |
| 33 | + |
| 34 | + - name: Run Trivy vulnerability scanner |
| 35 | + uses: aquasecurity/trivy-action@master |
| 36 | + with: |
| 37 | + image-ref: wsi-slides-processor:${{ github.sha }} |
| 38 | + format: "sarif" |
| 39 | + output: "trivy-results.sarif" |
| 40 | + severity: "CRITICAL,HIGH" |
| 41 | + |
| 42 | + - name: Upload Trivy results to GitHub Security |
| 43 | + uses: github/codeql-action/upload-sarif@v3 |
| 44 | + if: always() |
| 45 | + with: |
| 46 | + sarif_file: "trivy-results.sarif" |
| 47 | + |
| 48 | + - name: Run Trivy vulnerability scanner (table output) |
| 49 | + uses: aquasecurity/trivy-action@master |
| 50 | + with: |
| 51 | + image-ref: wsi-slides-processor:${{ github.sha }} |
| 52 | + format: "table" |
| 53 | + severity: "CRITICAL,HIGH" |
| 54 | + |
| 55 | + - name: Check for critical vulnerabilities |
| 56 | + uses: aquasecurity/trivy-action@master |
| 57 | + with: |
| 58 | + image-ref: wsi-slides-processor:${{ github.sha }} |
| 59 | + format: "json" |
| 60 | + output: "trivy-results.json" |
| 61 | + severity: "CRITICAL" |
| 62 | + exit-code: "0" # Don't fail the build, just report |
| 63 | + |
| 64 | + - name: Generate security report |
| 65 | + if: always() |
| 66 | + run: | |
| 67 | + echo "# Security Scan Report" >> $GITHUB_STEP_SUMMARY |
| 68 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 69 | + echo "**Scan Date:** $(date -u)" >> $GITHUB_STEP_SUMMARY |
| 70 | + echo "**Image:** wsi-slides-processor:${{ github.sha }}" >> $GITHUB_STEP_SUMMARY |
| 71 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 72 | +
|
| 73 | + if [ -f trivy-results.json ]; then |
| 74 | + CRITICAL_COUNT=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity=="CRITICAL")] | length' trivy-results.json) |
| 75 | + HIGH_COUNT=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity=="HIGH")] | length' trivy-results.json) |
| 76 | + |
| 77 | + echo "## Vulnerability Summary" >> $GITHUB_STEP_SUMMARY |
| 78 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 79 | + echo "- 🔴 Critical: $CRITICAL_COUNT" >> $GITHUB_STEP_SUMMARY |
| 80 | + echo "- 🟠 High: $HIGH_COUNT" >> $GITHUB_STEP_SUMMARY |
| 81 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 82 | + |
| 83 | + if [ "$CRITICAL_COUNT" -gt 0 ]; then |
| 84 | + echo "⚠️ **Action Required:** Critical vulnerabilities detected!" >> $GITHUB_STEP_SUMMARY |
| 85 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 86 | + echo "Review the detailed scan results and update dependencies." >> $GITHUB_STEP_SUMMARY |
| 87 | + else |
| 88 | + echo "✅ No critical vulnerabilities detected" >> $GITHUB_STEP_SUMMARY |
| 89 | + fi |
| 90 | + fi |
| 91 | +
|
| 92 | + - name: Upload scan results as artifact |
| 93 | + uses: actions/upload-artifact@v4 |
| 94 | + if: always() |
| 95 | + with: |
| 96 | + name: security-scan-results |
| 97 | + path: | |
| 98 | + trivy-results.sarif |
| 99 | + trivy-results.json |
| 100 | + retention-days: 30 |
0 commit comments