Merge pull request #31 from luispater/docker-build-sh

Inject build metadata into binary during release and docker build
This commit is contained in:
Luis Pater
2025-09-06 15:28:58 +08:00
committed by GitHub
8 changed files with 113 additions and 13 deletions

View File

@@ -10,7 +10,10 @@ builds:
main: ./cmd/server/
binary: cli-proxy-api
ldflags:
- -X 'main.Version={{.Env.GORELEASER_CURRENT_TAG}}'
- -s -w
- -X 'main.Version={{.Version}}'
- -X 'main.Commit={{.ShortCommit}}'
- -X 'main.BuildDate={{.Date}}'
archives:
- id: "cli-proxy-api"
format: tar.gz

View File

@@ -8,9 +8,11 @@ RUN go mod download
COPY . .
ARG APP_VERSION="dev"
ARG VERSION=dev
ARG COMMIT=none
ARG BUILD_DATE=unknown
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'main.Version=${APP_VERSION}'" -o ./CLIProxyAPI ./cmd/server/
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w -X 'main.Version=${VERSION}' -X 'main.Commit=${COMMIT}' -X 'main.BuildDate=${BUILD_DATE}'" -o ./CLIProxyAPI ./cmd/server/
FROM alpine:3.22.0

View File

@@ -499,10 +499,15 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya
1. Create a `config.yaml` from `config.example.yaml` and customize it.
2. Build and start the services using Docker Compose:
```bash
docker compose up -d --build
```
2. Build and start the services using the build scripts:
- For Windows (PowerShell):
```powershell
./docker-build.ps1
```
- For Linux/macOS:
```bash
bash docker-build.sh
```
3. To authenticate with providers, run the login command inside the container:
- **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login`

View File

@@ -512,10 +512,15 @@ docker run --rm -p 8317:8317 -v /path/to/your/config.yaml:/CLIProxyAPI/config.ya
1. 从 `config.example.yaml` 创建一个 `config.yaml` 文件并进行自定义。
2. 使用 Docker Compose 构建并启动服务:
```bash
docker compose up -d --build
```
2. 使用构建脚本构建并启动服务:
- Windows (PowerShell):
```powershell
./docker-build.ps1
```
- Linux/macOS:
```bash
bash docker-build.sh
```
3. 要在容器内运行登录命令进行身份验证:
- **Gemini**: `docker compose exec cli-proxy-api /CLIProxyAPI/CLIProxyAPI -no-browser --login`

View File

@@ -17,7 +17,11 @@ import (
log "github.com/sirupsen/logrus"
)
var Version = "dev"
var (
Version = "dev"
Commit = "none"
BuildDate = "unknown"
)
// LogFormatter defines a custom log format for logrus.
// This formatter adds timestamp, log level, and source location information
@@ -60,7 +64,7 @@ func init() {
// It parses command-line flags, loads configuration, and starts the appropriate
// service based on the provided flags (login, codex-login, or server mode).
func main() {
log.Infof("CLIProxyAPI Version: %v", Version)
log.Infof("CLIProxyAPI Version: %s, Commit: %s, BuiltAt: %s", Version, Commit, BuildDate)
// Command-line flags to control the application's behavior.
var login bool

36
docker-build.ps1 Normal file
View File

@@ -0,0 +1,36 @@
# build.ps1 - Windows PowerShell Build Script
#
# This script automates the process of building and running the Docker container
# with version information dynamically injected at build time.
# 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)
# Get the short commit hash.
$COMMIT = (git rev-parse --short HEAD)
# Get the current UTC date and time in ISO 8601 format.
$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 "----------------------------------------"
# --- 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
# --- 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
Write-Host "Build complete. Services are starting."
Write-Host "Run 'docker compose logs -f' to see the logs."

41
docker-build.sh Normal file
View File

@@ -0,0 +1,41 @@
#!/usr/bin/env bash
#
# build.sh - Linux/macOS Build Script
#
# This script automates the process of building and running the Docker container
# with version information dynamically injected at build time.
# 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)"
# Get the short commit hash.
COMMIT="$(git rev-parse --short HEAD)"
# Get the current UTC date and time in ISO 8601 format.
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 "----------------------------------------"
# --- 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}"
# --- 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
echo "Build complete. Services are starting."
echo "Run 'docker compose logs -f' to see the logs."

View File

@@ -3,6 +3,10 @@ services:
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: