|
| 1 | +name: 'Trivy Vulnerability Scanner' |
| 2 | +description: | |
| 3 | + Run [Trivy](https://trivy.dev) vulnerability scanner on container images or filesystems. |
| 4 | +
|
| 5 | + Features |
| 6 | + - Supports both container image and filesystem scanning |
| 7 | + - Configurable Trivy version |
| 8 | + - Customizable scan parameters (scanners, timeout, exit codes) |
| 9 | + - Generates markdown reports |
| 10 | + - Automatically adds reports to GitHub job summary |
| 11 | +
|
| 12 | + Requirements |
| 13 | + - Podman must be installed and running (for image scans) |
| 14 | + - The Trivy report template must exist at the specified path |
| 15 | + - For image scans, the image must be available in the local Podman storage |
| 16 | +
|
| 17 | +inputs: |
| 18 | + # https://trivy.dev/docs/latest/guide/target/container_image/#vulnerabilities |
| 19 | + scan-type: |
| 20 | + description: 'Type of scan to perform: "image" or "fs" (filesystem)' |
| 21 | + required: true |
| 22 | + scan-target: |
| 23 | + description: 'Target to scan (image name or filesystem path)' |
| 24 | + required: true |
| 25 | + trivy-version: |
| 26 | + description: 'Version of Trivy to use' |
| 27 | + required: false |
| 28 | + default: '0.68.2' |
| 29 | + podman-socket: |
| 30 | + description: 'Path to Podman socket (required for image scans)' |
| 31 | + required: false |
| 32 | + default: '/var/run/podman/podman.sock' |
| 33 | + workspace-path: |
| 34 | + description: 'Workspace path for filesystem scans' |
| 35 | + required: false |
| 36 | + default: ${{ github.workspace }} |
| 37 | + report-template: |
| 38 | + description: 'Path to Trivy report template' |
| 39 | + required: false |
| 40 | + default: 'ci/trivy-markdown.tpl' |
| 41 | + scanners: |
| 42 | + description: 'Comma-separated list of scanners to use' |
| 43 | + required: false |
| 44 | + default: 'vuln' |
| 45 | + ignore-unfixed: |
| 46 | + description: 'Ignore unfixed vulnerabilities' |
| 47 | + required: false |
| 48 | + default: 'true' |
| 49 | + timeout: |
| 50 | + description: 'Scan timeout' |
| 51 | + required: false |
| 52 | + default: '30m' |
| 53 | + exit-code: |
| 54 | + description: 'Exit code when vulnerabilities are found' |
| 55 | + required: false |
| 56 | + default: '0' |
| 57 | + |
| 58 | +outputs: |
| 59 | + report-file: |
| 60 | + description: 'Path to the generated report file' |
| 61 | + value: ${{ steps.scan.outputs.report-file }} |
| 62 | + |
| 63 | +runs: |
| 64 | + using: 'composite' |
| 65 | + steps: |
| 66 | + - name: Setup report directory |
| 67 | + id: setup |
| 68 | + shell: bash |
| 69 | + run: | |
| 70 | + REPORT_FOLDER=${{ inputs.workspace-path }}/trivy-report |
| 71 | + REPORT_FILE=trivy-report.md |
| 72 | + REPORT_TEMPLATE=$(basename ${{ inputs.report-template }}) |
| 73 | +
|
| 74 | + mkdir -p $REPORT_FOLDER |
| 75 | + cp ${{ inputs.report-template }} $REPORT_FOLDER/ |
| 76 | +
|
| 77 | + echo "report-folder=$REPORT_FOLDER" >> $GITHUB_OUTPUT |
| 78 | + echo "report-file=$REPORT_FILE" >> $GITHUB_OUTPUT |
| 79 | + echo "report-template=$REPORT_TEMPLATE" >> $GITHUB_OUTPUT |
| 80 | +
|
| 81 | + - name: Run Trivy vulnerability scanner |
| 82 | + id: scan |
| 83 | + shell: bash |
| 84 | + run: | |
| 85 | + REPORT_FOLDER=${{ steps.setup.outputs.report-folder }} |
| 86 | + REPORT_FILE=${{ steps.setup.outputs.report-file }} |
| 87 | + REPORT_TEMPLATE=${{ steps.setup.outputs.report-template }} |
| 88 | +
|
| 89 | + SCAN_TARGET=${{ inputs.scan-target }} |
| 90 | + SCAN_TYPE=${{ inputs.scan-type }} |
| 91 | +
|
| 92 | + echo "Scanning $SCAN_TARGET ($SCAN_TYPE)" |
| 93 | +
|
| 94 | + # Configure scan arguments based on type |
| 95 | + if [[ "$SCAN_TYPE" == "image" ]]; then |
| 96 | + SCAN_ARGS="--image-src podman --podman-host /var/run/podman/podman.sock" |
| 97 | + PODMAN_ARGS="-v ${{ inputs.podman-socket }}:/var/run/podman/podman.sock" |
| 98 | + elif [[ "$SCAN_TYPE" == "fs" ]]; then |
| 99 | + WORKSPACE_FOLDER="/workspace" |
| 100 | + SCAN_TARGET="$WORKSPACE_FOLDER/$SCAN_TARGET" |
| 101 | + PODMAN_ARGS="-v ${{ inputs.workspace-path }}:$WORKSPACE_FOLDER" |
| 102 | + else |
| 103 | + echo "Error: Invalid scan type '$SCAN_TYPE'. Must be 'image' or 'fs'" |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | +
|
| 107 | + # Run Trivy scan in container |
| 108 | + podman run --rm \ |
| 109 | + $PODMAN_ARGS \ |
| 110 | + -v ${REPORT_FOLDER}:/report \ |
| 111 | + docker.io/aquasec/trivy:${{ inputs.trivy-version }} \ |
| 112 | + $SCAN_TYPE \ |
| 113 | + $SCAN_ARGS \ |
| 114 | + --scanners ${{ inputs.scanners }} \ |
| 115 | + ${{ inputs.ignore-unfixed == 'true' && '--ignore-unfixed' || '' }} \ |
| 116 | + --exit-code ${{ inputs.exit-code }} \ |
| 117 | + --timeout ${{ inputs.timeout }} \ |
| 118 | + --format template --template "@/report/$REPORT_TEMPLATE" \ |
| 119 | + -o /report/$REPORT_FILE \ |
| 120 | + $SCAN_TARGET |
| 121 | +
|
| 122 | + echo "report-file=$REPORT_FOLDER/$REPORT_FILE" >> $GITHUB_OUTPUT |
| 123 | +
|
| 124 | + - name: Add report to job summary |
| 125 | + shell: bash |
| 126 | + run: | |
| 127 | + REPORT_FILE=${{ steps.scan.outputs.report-file }} |
| 128 | + if [[ -f "$REPORT_FILE" ]]; then |
| 129 | + cat $REPORT_FILE >> $GITHUB_STEP_SUMMARY |
| 130 | + else |
| 131 | + echo "Warning: Report file not found at $REPORT_FILE" |
| 132 | + fi |
0 commit comments