name: CI Docker

on:
  workflow_dispatch:

concurrency:
  group: ci-docker-${{ github.ref }}
  cancel-in-progress: true

env:
  ORT_VERSION: "1.24.2"
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"

permissions:
  contents: read

jobs:
  docker:
    name: Docker (${{ matrix.variant }})
    if: github.repository == 'kreuzberg-dev/kreuzberg' && github.actor != 'dependabot[bot]'
    runs-on: ubuntu-latest
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        variant: [core, full, cli]
    steps:
      - uses: actions/checkout@v6

      - name: Free disk space
        uses: kreuzberg-dev/actions/free-disk-space-linux@v1

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v4

      - name: Build Docker image
        uses: docker/build-push-action@v7
        with:
          context: .
          file: docker/Dockerfile.${{ matrix.variant }}
          push: false
          load: true
          tags: kreuzberg:${{ matrix.variant }}
          build-args: ONNXRUNTIME_VERSION=${{ env.ORT_VERSION }}
          cache-from: type=gha,scope=ci-docker-${{ matrix.variant }}
          cache-to: type=gha,mode=max,scope=ci-docker-${{ matrix.variant }}

      - name: Save Docker image
        shell: bash
        run: |
          mkdir -p /tmp
          docker save kreuzberg:${{ matrix.variant }} | gzip > /tmp/kreuzberg-${{ matrix.variant }}.tar.gz
          ls -lh /tmp/kreuzberg-${{ matrix.variant }}.tar.gz

      - name: Check image size
        uses: kreuzberg-dev/actions/check-docker-image-size@v1
        with:
          image: kreuzberg:${{ matrix.variant }}
          warn-mb: ${{ matrix.variant == 'cli' && '200' || '' }}
          label: "${{ matrix.variant }} image"

      - name: Run feature tests
        if: matrix.variant != 'cli'
        run: scripts/ci/docker/run-feature-tests.sh "${{ matrix.variant }}"

      - name: Run configuration tests
        if: matrix.variant != 'cli'
        run: scripts/ci/docker/run-config-tests.sh "${{ matrix.variant }}"

      - name: Run API contract tests with schemathesis
        if: matrix.variant != 'cli'
        uses: kreuzberg-dev/actions/run-api-contract-tests@v1
        with:
          image: kreuzberg:${{ matrix.variant }}
          port: "8000"

      - name: Run CLI tests
        if: matrix.variant == 'cli'
        run: scripts/ci/docker/run-cli-tests.sh
