name: Reusable workflow to run trivy scan

on:
  workflow_call:
    inputs:
      runs-on:
        required: true
        type: string
      scan-type:
        required: true
        type: string
      format:
        required: true
        type: string
      output:
        required: false
        type: string
        default: trivy.sarif
      image-ref:
        required: false
        type: string
        default: ""
      severity:
        required: false
        type: string
        default: "CRITICAL,HIGH"
      exit-code:
        required: true
        type: number
      publish:
        required: true
        type: boolean

jobs:
  trivy-scan:
    name: Trivy - ${{ inputs.runs-on }} - ${{ inputs.scan-type }} ${{ inputs.image-ref }}
    runs-on: ${{ inputs.runs-on }}
    steps:
      - uses: actions/checkout@v3

      - name: Run Trivy
        uses: aquasecurity/trivy-action@0.8.0
        with:
          scan-type: ${{ inputs.scan-type }}
          image-ref: ${{ inputs.image-ref }}
          ignore-unfixed: false
          format: ${{ inputs.format }}
          output: ${{ inputs.output }}
          exit-code: ${{ inputs.exit-code }}
          severity: ${{ inputs.severity }}
          trivy-config: trivy.yml

      - name: Upload Trivy scan results to GitHub Security tab
        uses: github/codeql-action/upload-sarif@v2
        if: ${{ inputs.publish }}
        with:
          sarif_file: ${{ inputs.output }}
