name: Docker Security Scan

on:
  push:
    branches: [ main ]
    paths:
      - 'Dockerfile'
      - '.dockerignore'
      - 'package.json'
      - 'bun.lock*'
  pull_request:
    branches: [ main ]
    paths:
      - 'Dockerfile'
      - '.dockerignore'
      - 'package.json'
      - 'bun.lock*'
  schedule:
    - cron: '0 0 * * 0'  # Run weekly on Sunday at midnight

jobs:
  scan:
    name: Scan Docker Image
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
        with:
          driver-opts: network=host

      - name: Build Docker image
        uses: docker/build-push-action@v5
        with:
          context: .
          push: false
          load: true
          tags: gitea-mirror:scan
          # Disable GitHub Actions cache for this workflow
          no-cache: true

      - name: Run Trivy vulnerability scanner
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: gitea-mirror:scan
          format: 'table'
          exit-code: '1'
          ignore-unfixed: true
          vuln-type: 'os,library'
          severity: 'CRITICAL,HIGH'
