vulnerability-scan.yaml 2.5 KB

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