name: Publish Docker Images

on:
  push:
    tags:
      - "v*"
  workflow_dispatch:
    inputs:
      tag:
        description: "Image tag to publish (without image suffix)"
        required: true
        type: string

permissions:
  contents: read
  packages: write

jobs:
  docker:
    runs-on: ubuntu-latest
    env:
      IMAGE_TAG: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.tag || github.ref_name }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - image_suffix: cpu-diarization-sortformer
            dockerfile: Dockerfile.cpu
            extras: cpu,diarization-sortformer
          - image_suffix: cu129-diarization-sortformer
            dockerfile: Dockerfile
            extras: cu129,diarization-sortformer
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set lowercase owner
        id: owner
        run: echo "value=${GITHUB_REPOSITORY_OWNER,,}" >> "${GITHUB_OUTPUT}"

      - name: Login to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build and push image
        uses: docker/build-push-action@v6
        with:
          context: .
          file: ./${{ matrix.dockerfile }}
          push: true
          build-args: |
            EXTRAS=${{ matrix.extras }}
          tags: |
            ghcr.io/${{ steps.owner.outputs.value }}/whisperlivekit:${{ env.IMAGE_TAG }}-${{ matrix.image_suffix }}
            ghcr.io/${{ steps.owner.outputs.value }}/whisperlivekit:latest-${{ matrix.image_suffix }}
