name: Release Docker Xeon Images
on:
  push:
    tags:
      - 'v[0-9]+.*'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to build (without v prefix, e.g., 0.5.7)'
        required: true

jobs:
  publish:
    if: github.repository == 'sgl-project/sglang'
    runs-on: ubuntu-24.04
    environment: 'prod'
    strategy:
      matrix:
        build_type: ['all']
    steps:

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

      - name: Login to Docker Hub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Get version from tag
        id: version
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            VERSION="${{ github.event.inputs.version }}"
          else
            # Extract version from tag (e.g., v0.5.7 -> 0.5.7)
            VERSION="${GITHUB_REF_NAME#v}"
          fi

          # Validate version format
          if [ -z "$VERSION" ]; then
            echo "::error::Version is empty"
            exit 1
          fi
          if ! echo "$VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+'; then
            echo "::error::Invalid version format: $VERSION (expected: X.Y.Z)"
            exit 1
          fi

          echo "version=${VERSION}" >> $GITHUB_OUTPUT

      - name: Build and Push
        run: |
          version=${{ steps.version.outputs.version }}
          tag=v${version}-xeon

          docker build . -f docker/xeon.Dockerfile \
            --build-arg VER_SGLANG=v${version} \
            -t lmsysorg/sglang:${tag} \
            --no-cache
          docker push lmsysorg/sglang:${tag}
