name: Release Docker Images
on:
  push:
    branches:
      - main
    paths:
      - "python/sglang/version.py"
  workflow_dispatch:

jobs:
  publish-x86:
    if: github.repository == 'sgl-project/sglang'
    environment: "prod"
    strategy:
      matrix:
        variant:
          - cuda_version: "12.6.1"
            build_type: "all"
          - cuda_version: "12.8.1"
            build_type: "blackwell"
          - cuda_version: "12.9.1"
            build_type: "blackwell"
    runs-on: nvidia
    steps:
      - name: Delete huge unnecessary tools folder
        run: rm -rf /opt/hostedtoolcache

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

      - name: Free disk space
        uses: jlumbroso/free-disk-space@main
        with:
          tool-cache: false
          docker-images: false
          android: true
          dotnet: true
          haskell: true
          large-packages: true
          swap-storage: false

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

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

      - name: Build and Push AMD64
        run: |
          version=$(cat python/sglang/version.py | cut -d'"' -f2)

          if [ "${{ matrix.variant.cuda_version }}" = "12.6.1" ]; then
            cuda_tag="cu126"
          elif [ "${{ matrix.variant.cuda_version }}" = "12.8.1" ]; then
            cuda_tag="cu128"
          elif [ "${{ matrix.variant.cuda_version }}" = "12.9.1" ]; then
            cuda_tag="cu129"
          else
            echo "Unsupported CUDA version"
            exit 1
          fi

          tag=v${version}-${cuda_tag}

          if [ "${{ matrix.variant.build_type }}" = "all" ]; then
            tag_suffix=""
          elif [ "${{ matrix.variant.build_type }}" = "blackwell" ]; then
            tag_suffix="-b200"
          else
            echo "Unsupported build type"
            exit 1
          fi

          if [ "${{ matrix.variant.cuda_version }}" = "12.9.1" ]; then
            docker buildx build --platform linux/amd64 --push -f docker/Dockerfile --build-arg CUDA_VERSION=${{ matrix.variant.cuda_version }} --build-arg BUILD_TYPE=${{ matrix.variant.build_type }} -t lmsysorg/sglang:${tag}${tag_suffix} -t lmsysorg/sglang:latest --no-cache .
          else
            docker buildx build --platform linux/amd64 --push -f docker/Dockerfile --build-arg CUDA_VERSION=${{ matrix.variant.cuda_version }} --build-arg BUILD_TYPE=${{ matrix.variant.build_type }} -t lmsysorg/sglang:${tag}${tag_suffix} --no-cache .
          fi

  publish-arm64:
    if: github.repository == 'sgl-project/sglang'
    environment: "prod"
    strategy:
      matrix:
        variant:
          - cuda_version: "12.9.1"
            build_type: "blackwell_aarch64"
    runs-on: sgl-kernel-release-node-arm
    steps:
      - name: Delete huge unnecessary tools folder
        run: rm -rf /opt/hostedtoolcache

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

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

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

      - name: Build and Push ARM64
        run: |
          version=$(cat python/sglang/version.py | cut -d'"' -f2)

          if [ "${{ matrix.variant.cuda_version }}" = "12.9.1" ]; then
            cuda_tag="cu129"
          else
            echo "Unsupported CUDA version"
            exit 1
          fi

          tag=v${version}-${cuda_tag}
          tag_suffix="-gb200"

          docker buildx build --platform linux/arm64 --push -f docker/Dockerfile --build-arg CUDA_VERSION=${{ matrix.variant.cuda_version }} --build-arg BUILD_TYPE=${{ matrix.variant.build_type }} -t lmsysorg/sglang:${tag}${tag_suffix} --no-cache .
