vulnerability-scan.yaml 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. merge_group:
  14. types: [checks_requested]
  15. jobs:
  16. scan:
  17. name: Scan for Vulnerabilities
  18. runs-on: ubuntu-latest
  19. steps:
  20. - name: Checkout code
  21. uses: actions/checkout@v6.0.2
  22. - name: Install Trivy
  23. run: |
  24. curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
  25. - name: Run Trivy scan
  26. id: trivy-scan
  27. continue-on-error: true
  28. run: |
  29. # Generate SARIF report first
  30. trivy fs \
  31. --format template \
  32. --template '@/contrib/sarif.tpl' \
  33. --output trivy-results.sarif \
  34. --severity CRITICAL,HIGH \
  35. --vuln-type os,library \
  36. --no-progress .
  37. # Generate JSON report and fail step if vulnerabilities are found
  38. trivy fs \
  39. --format json \
  40. --output trivy-results.json \
  41. --severity CRITICAL,HIGH \
  42. --vuln-type os,library \
  43. --no-progress \
  44. --exit-code 1 .
  45. - name: Upload Trivy JSON report as artifact
  46. if: steps.trivy-scan.outcome == 'failure'
  47. uses: actions/upload-artifact@v7
  48. with:
  49. name: trivy-json-report
  50. path: trivy-results.json
  51. retention-days: 1
  52. - name: Upload SARIF to GitHub Security tab
  53. if: always()
  54. uses: github/codeql-action/upload-sarif@v4
  55. with:
  56. sarif_file: 'trivy-results.sarif'
  57. category: trivy-fs
  58. - name: Print vulnerability details and fail job
  59. if: steps.trivy-scan.outcome == 'failure'
  60. run: |
  61. echo "🛑 Trivy scan found CRITICAL or HIGH severity vulnerabilities. Details:"
  62. echo "--------------------------------------------------------------------"
  63. # Parse the JSON report and print a summary of each vulnerability
  64. 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
  65. # Exit with a failure code to fail the workflow
  66. exit 1