name: Docker Build
description: "FastDeploy CI Image Build"

on:
  workflow_call:
    inputs:
      CI_DOCKER_IMAGE_NAME:
        description: "Build Images"
        required: true
        type: string
        default: "ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddleqa:cuda126-py310"
      FASTDEPLOY_ARCHIVE_URL:
        description: "URL of the compressed FastDeploy code archive."
        required: true
        type: string
      DOCKER_IMAGE_NAME:
        description: "Build Images"
        required: false
        type: string
        default: "ccr-2vdh3abv-pub.cnc.bj.baidubce.com/paddlepaddle/paddleqa:fastdeploy-ciuse-cuda126-dailyupdate"
    outputs:
      docker_name_precheck:
        description: "Output path of the generated wheel"
        value: ${{ jobs.docker_build.outputs.docker_name_precheck }}

jobs:
  docker_build:
    runs-on: [self-hosted, Docker-Build]
    outputs:
      docker_name_precheck: ${{ steps.docker_build.outputs.docker_name_precheck }}
    steps:
      - name: Docker Build
        id: docker_build
        shell: bash
        env:
          docker_image_name: ${{ inputs.CI_DOCKER_IMAGE_NAME }}
          docker_image: ${{ inputs.DOCKER_IMAGE_NAME }}
          fd_archive_url: ${{ inputs.FASTDEPLOY_ARCHIVE_URL }}
        run: |
            set -x
            REPO="https://github.com/${{ github.repository }}.git"
            FULL_REPO="${{ github.repository }}"
            REPO_NAME="${FULL_REPO##*/}"
            BASE_BRANCH="${{ github.base_ref }}"

            # Clean the repository directory before starting
            docker run --rm --net=host -v $(pwd):/workspace -w /workspace \
            -e "REPO_NAME=${REPO_NAME}" \
            -e "no_proxy=localhost,127.0.0.1,0.0.0.0,bcebos.com,.bcebos.com,bj.bcebos.com,su.bcebos.com,paddle-ci.gz.bcebos.com,apiin.im.baidu.com,baidu-int.com,.baidu.com,aliyun.com,gitee.com,pypi.tuna.tsinghua.edu.cn,.tuna.tsinghua.edu.cn" \
            ${docker_image} /bin/bash -c '
              if [ -d ${REPO_NAME} ]; then
                echo "Directory ${REPO_NAME} exists, removing it..."
                rm -rf ${REPO_NAME}*
              fi
            '

            wget -q --no-proxy ${fd_archive_url}
            tar -xf FastDeploy.tar.gz
            rm -rf FastDeploy.tar.gz
            cd FastDeploy
            git config --global user.name "FastDeployCI"
            git config --global user.email "fastdeploy_ci@example.com"
            git log -n 3 --oneline

            # Docker Build
            cd tools/dockerfile/
            set -e
            cp ../../requirements.txt ./
            cp ../../scripts/unittest_requirement.txt ./
            docker build -t ${docker_image_name} -f Dockerfile.ci . \
                --network host \
                --no-cache
            docker push ${docker_image_name}
            echo "docker_name_precheck=${docker_image_name}" >> $GITHUB_OUTPUT
