vulnerability-scan.yaml 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. contents: read
  18. security-events: write
  19. steps:
  20. - name: Checkout code
  21. uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
  22. with:
  23. persist-credentials: false
  24. - name: Install Trivy
  25. # Pinned release verified by checksum. To bump, take the Linux-64bit
  26. # hash from trivy_<version>_checksums.txt on the release page.
  27. env:
  28. TRIVY_VERSION: 0.74.0
  29. TRIVY_SHA256: 2ae6fe3ee734b7fdf11335663e18c75ea12dccc76062f09f164a3b0f8be4371a
  30. run: |
  31. curl -sSfL -o trivy.tar.gz \
  32. "https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz"
  33. echo "${TRIVY_SHA256} trivy.tar.gz" | sha256sum -c -
  34. tar -xzf trivy.tar.gz -C /usr/local/bin trivy
  35. rm trivy.tar.gz
  36. trivy --version
  37. - name: Run Trivy scan
  38. id: trivy-scan
  39. continue-on-error: true
  40. run: |
  41. # Generate SARIF report first
  42. trivy fs \
  43. --format sarif \
  44. --output trivy-results.sarif \
  45. --severity CRITICAL,HIGH \
  46. --pkg-types os,library \
  47. --no-progress .
  48. # Generate JSON report and fail step if vulnerabilities are found
  49. trivy fs \
  50. --format json \
  51. --output trivy-results.json \
  52. --severity CRITICAL,HIGH \
  53. --pkg-types os,library \
  54. --no-progress \
  55. --exit-code 1 .
  56. - name: Upload Trivy JSON report as artifact
  57. if: steps.trivy-scan.outcome == 'failure'
  58. uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
  59. with:
  60. name: trivy-json-report
  61. path: trivy-results.json
  62. retention-days: 1
  63. - name: Upload SARIF to GitHub Security tab
  64. if: always()
  65. uses: github/codeql-action/upload-sarif@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2
  66. with:
  67. sarif_file: 'trivy-results.sarif'
  68. category: trivy-fs
  69. - name: Print vulnerability details and fail job
  70. if: steps.trivy-scan.outcome == 'failure'
  71. run: |
  72. echo "🛑 Trivy scan found CRITICAL or HIGH severity vulnerabilities. Details:"
  73. echo "--------------------------------------------------------------------"
  74. # Parse the JSON report and print a summary of each vulnerability
  75. 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
  76. # Exit with a failure code to fail the workflow
  77. exit 1