diff --git a/.dockerignore b/.dockerignore index 1935a138..30edabaf 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,6 +4,8 @@ # Docker and CI/CD related files docker-compose.yml +docker-compose.override.yml +docker-compose.remote.yml .dockerignore .gitignore .goreleaser.yml diff --git a/README.md b/README.md index 8d15f683..779291c6 100644 --- a/README.md +++ b/README.md @@ -505,7 +505,7 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya 2. Create a `config.yaml` from `config.example.yaml` and customize it. -3. Build and start the services using the build scripts: +3. Build and start the services using the interactive build scripts: - For Windows (PowerShell): ```powershell .\docker-build.ps1 @@ -514,6 +514,9 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya ```bash bash docker-build.sh ``` + The script will prompt you to choose an environment: + - **Option 1: Local Development**: Builds the Docker image from the source and starts the services. + - **Option 2: Remote Deployment**: Pulls the pre-built image specified in `docker-compose.remote.yml` and starts the services. 4. To authenticate with providers, run the login command inside the container: - **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login` diff --git a/README_CN.md b/README_CN.md index 862926c6..2ec62dac 100644 --- a/README_CN.md +++ b/README_CN.md @@ -520,7 +520,7 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya 2. 从 `config.example.yaml` 创建一个 `config.yaml` 文件并进行自定义。 -3. 使用构建脚本构建并启动服务: +3. 使用交互式构建脚本构建并启动服务: - Windows (PowerShell): ```powershell .\docker-build.ps1 @@ -529,6 +529,9 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya ```bash bash docker-build.sh ``` + 脚本将提示您选择一个环境: + - **选项 1:本地开发 (Local Development)**:从源代码构建 Docker 镜像并启动服务。 + - **选项 2:远程部署 (Remote Deployment)**:拉取 `docker-compose.remote.yml` 中指定的预构建镜像并启动服务。 4. 要在容器内运行登录命令进行身份验证: - **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login` diff --git a/docker-build.ps1 b/docker-build.ps1 index caed5b45..6f479bb0 100644 --- a/docker-build.ps1 +++ b/docker-build.ps1 @@ -6,31 +6,45 @@ # Stop script execution on any error $ErrorActionPreference = "Stop" -# --- Step 1: Get Version Information --- -# Get the latest git tag or commit hash as the version string. -$VERSION = (git describe --tags --always --dirty) +# --- Step 1: Choose Environment --- +Write-Host "Please select your environment:" +Write-Host "1) Local Development (Build and Run)" +Write-Host "2) Remote Deployment (Run from Image)" +$choice = Read-Host -Prompt "Enter choice [1-2]" -# Get the short commit hash. -$COMMIT = (git rev-parse --short HEAD) +# --- Step 2: Execute based on choice --- +switch ($choice) { + "1" { + Write-Host "--- Starting Local Development Environment ---" -# Get the current UTC date and time in ISO 8601 format. -$BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") + # Get Version Information + $VERSION = (git describe --tags --always --dirty) + $COMMIT = (git rev-parse --short HEAD) + $BUILD_DATE = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ") -Write-Host "--- Building with the following info ---" -Write-Host "Version: $VERSION" -Write-Host "Commit: $COMMIT" -Write-Host "Build Date: $BUILD_DATE" -Write-Host "----------------------------------------" + Write-Host "Building with the following info:" + Write-Host " Version: $VERSION" + Write-Host " Commit: $COMMIT" + Write-Host " Build Date: $BUILD_DATE" + Write-Host "----------------------------------------" -# --- Step 2: Build the Docker Image --- -# Pass the version information as build arguments to 'docker compose build'. -# These arguments are then used by the Dockerfile to inject them into the Go binary. -docker compose build --build-arg VERSION=$VERSION --build-arg COMMIT=$COMMIT --build-arg BUILD_DATE=$BUILD_DATE + # Build the Docker Image + docker compose build --build-arg VERSION=$VERSION --build-arg COMMIT=$COMMIT --build-arg BUILD_DATE=$BUILD_DATE -# --- Step 3: Start the Services --- -# Start the services in detached mode using the newly built image. -# '--remove-orphans' cleans up any containers for services that are no longer defined. -docker compose up -d --remove-orphans + # Start the Services + docker compose up -d --remove-orphans -Write-Host "Build complete. Services are starting." -Write-Host "Run 'docker compose logs -f' to see the logs." \ No newline at end of file + Write-Host "Build complete. Services are starting." + Write-Host "Run 'docker compose logs -f' to see the logs." + } + "2" { + Write-Host "--- Starting Remote Deployment Environment ---" + docker compose -f docker-compose.yml -f docker-compose.remote.yml up -d + Write-Host "Services are starting from remote image." + Write-Host "Run 'docker compose logs -f' to see the logs." + } + default { + Write-Host "Invalid choice. Please enter 1 or 2." + exit 1 + } +} \ No newline at end of file diff --git a/docker-build.sh b/docker-build.sh index 8a92f3b7..79067f13 100644 --- a/docker-build.sh +++ b/docker-build.sh @@ -8,34 +8,48 @@ # Exit immediately if a command exits with a non-zero status. set -euo pipefail -# --- Step 1: Get Version Information --- -# Get the latest git tag or commit hash as the version string. -VERSION="$(git describe --tags --always --dirty)" +# --- Step 1: Choose Environment --- +echo "Please select your environment:" +echo "1) Local Development (Build and Run)" +echo "2) Remote Deployment (Run from Image)" +read -r -p "Enter choice [1-2]: " choice -# Get the short commit hash. -COMMIT="$(git rev-parse --short HEAD)" +# --- Step 2: Execute based on choice --- +case "$choice" in + 1) + echo "--- Starting Local Development Environment ---" -# Get the current UTC date and time in ISO 8601 format. -BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" + # Get Version Information + VERSION="$(git describe --tags --always --dirty)" + COMMIT="$(git rev-parse --short HEAD)" + BUILD_DATE="$(date -u +%Y-%m-%dT%H:%M:%SZ)" -echo "--- Building with the following info ---" -echo "Version: ${VERSION}" -echo "Commit: ${COMMIT}" -echo "Build Date: ${BUILD_DATE}" -echo "----------------------------------------" + echo "Building with the following info:" + echo " Version: ${VERSION}" + echo " Commit: ${COMMIT}" + echo " Build Date: ${BUILD_DATE}" + echo "----------------------------------------" -# --- Step 2: Build the Docker Image --- -# Pass the version information as build arguments to 'docker compose build'. -# These arguments are then used by the Dockerfile to inject them into the Go binary. -docker compose build \ - --build-arg VERSION="${VERSION}" \ - --build-arg COMMIT="${COMMIT}" \ - --build-arg BUILD_DATE="${BUILD_DATE}" + # Build the Docker Image + docker compose build \ + --build-arg VERSION="${VERSION}" \ + --build-arg COMMIT="${COMMIT}" \ + --build-arg BUILD_DATE="${BUILD_DATE}" -# --- Step 3: Start the Services --- -# Start the services in detached mode using the newly built image. -# '--remove-orphans' cleans up any containers for services that are no longer defined. -docker compose up -d --remove-orphans + # Start the Services + docker compose up -d --remove-orphans -echo "Build complete. Services are starting." -echo "Run 'docker compose logs -f' to see the logs." \ No newline at end of file + echo "Build complete. Services are starting." + echo "Run 'docker compose logs -f' to see the logs." + ;; + 2) + echo "--- Starting Remote Deployment Environment ---" + docker compose -f docker-compose.yml -f docker-compose.remote.yml up -d + echo "Services are starting from remote image." + echo "Run 'docker compose logs -f' to see the logs." + ;; + *) + echo "Invalid choice. Please enter 1 or 2." + exit 1 + ;; +esac \ No newline at end of file diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 00000000..c99e9090 --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,10 @@ +services: + cli-proxy-api: + image: cli-proxy-api:latest + build: + context: . + dockerfile: Dockerfile + args: + VERSION: ${VERSION:-dev} + COMMIT: ${COMMIT:-none} + BUILD_DATE: ${BUILD_DATE:-unknown} \ No newline at end of file diff --git a/docker-compose.remote.yml b/docker-compose.remote.yml new file mode 100644 index 00000000..143d552b --- /dev/null +++ b/docker-compose.remote.yml @@ -0,0 +1,3 @@ +services: + cli-proxy-api: + image: eceasy/cli-proxy-api:latest \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 299748f4..f3a56534 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,5 @@ services: cli-proxy-api: - build: - context: . - dockerfile: Dockerfile - args: - VERSION: ${VERSION:-dev} - COMMIT: ${COMMIT:-none} - BUILD_DATE: ${BUILD_DATE:-unknown} - image: cli-proxy-api:latest container_name: cli-proxy-api ports: - "8317:8317"