vulnerability-scan.yaml 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. name: Trivy Vulnerability Scanner
  2. permissions:
  3. issues: write
  4. contents: read
  5. security-events: write
  6. on:
  7. pull_request:
  8. branches:
  9. - develop
  10. push:
  11. branches:
  12. - develop
  13. jobs:
  14. scan:
  15. name: Scan for Vulnerabilities
  16. runs-on: ubuntu-latest
  17. steps:
  18. - name: Checkout code
  19. uses: actions/checkout@v4
  20. - name: Install Trivy
  21. run: |
  22. curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
  23. - name: Run Trivy scan
  24. id: trivy-scan
  25. continue-on-error: true
  26. run: |
  27. # Generate SARIF report first
  28. trivy fs \
  29. --format template \
  30. --template '@/contrib/sarif.tpl' \
  31. --output trivy-results.sarif \
  32. --severity CRITICAL,HIGH \
  33. --vuln-type os,library \
  34. --no-progress .
  35. # Generate JSON report and fail step if vulnerabilities are found
  36. trivy fs \
  37. --format json \
  38. --output trivy-results.json \
  39. --severity CRITICAL,HIGH \
  40. --vuln-type os,library \
  41. --no-progress \
  42. --exit-code 1 .
  43. - name: Upload Trivy JSON report as artifact
  44. if: steps.trivy-scan.outcome == 'failure'
  45. uses: actions/upload-artifact@v4
  46. with:
  47. name: trivy-json-report
  48. path: trivy-results.json
  49. retention-days: 1
  50. - name: Upload SARIF to GitHub Security tab
  51. if: always()
  52. uses: github/codeql-action/upload-sarif@v3
  53. with:
  54. sarif_file: 'trivy-results.sarif'
  55. category: trivy-fs
  56. - name: Print vulnerability details and fail job
  57. if: steps.trivy-scan.outcome == 'failure'
  58. run: |
  59. echo "🛑 Trivy scan found CRITICAL or HIGH severity vulnerabilities. Details:"
  60. echo "--------------------------------------------------------------------"
  61. # Parse the JSON report and print a summary of each vulnerability
  62. jq -r '.Results[] | .Target as $target | if .Vulnerabilities then .Vulnerabilities[] | "File: \($target)\nPackage: \(.PkgName) (\(.InstalledVersion))\nID: \(.VulnerabilityID)\nSeverity: \(.Severity)\nLink: \(.PrimaryURL)\n--------------------------------------------------------------------" else empty end' trivy-results.json
  63. # Exit with a failure code to fail the workflow
  64. exit 1