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.9.1"
            build_type: "all"
    runs-on: x64-docker-build-node
    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)
          tag=v${version}-cu129-amd64

          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} \
            --no-cache \
            .

  publish-arm64:
    if: github.repository == 'sgl-project/sglang'
    environment: "prod"
    strategy:
      matrix:
        variant:
          - cuda_version: "12.9.1"
            build_type: "all_aarch64"
    runs-on: arm-docker-build-node
    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)
          tag=v${version}-cu129-arm64

          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} \
            --no-cache \
            .

  create-manifests:
    runs-on: ubuntu-22.04
    needs: [publish-x86, publish-arm64]
    if: github.repository == 'sgl-project/sglang'
    environment: "prod"
    steps:
      - 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: Create multi-arch manifests
        run: |
          version=$(cat python/sglang/version.py | cut -d'"' -f2)

          # Create versioned manifest
          docker buildx imagetools create \
            -t lmsysorg/sglang:v${version} \
            lmsysorg/sglang:v${version}-cu129-amd64 \
            lmsysorg/sglang:v${version}-cu129-arm64

          # Create latest manifest
          docker buildx imagetools create \
            -t lmsysorg/sglang:latest \
            lmsysorg/sglang:v${version}-cu129-amd64 \
            lmsysorg/sglang:v${version}-cu129-arm64
