diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b56e593 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,40 @@ +# 构建阶段 +FROM python:3.11-slim as builder + +# 设置工作目录 +WORKDIR /app + +# 设置环境变量 +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +# 安装构建依赖 +RUN apt-get update && \ + apt-get install -y --no-install-recommends gcc && \ + rm -rf /var/lib/apt/lists/* + +# 安装Python依赖 +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ + +# 最终阶段 +FROM python:3.11-slim + +# 设置工作目录 +WORKDIR /app + +# 设置环境变量 +ENV PYTHONDONTWRITEBYTECODE=1 \ + PYTHONUNBUFFERED=1 + +# 从构建阶段复制Python环境 +COPY --from=builder /usr/local/lib/python3.11/site-packages/ /usr/local/lib/python3.11/site-packages/ + +# 复制应用代码 +COPY . . + +# 暴露端口 +EXPOSE 8000 + +# 启动命令 +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/app/core/version.py b/app/core/version.py new file mode 100644 index 0000000..f9d6fa9 --- /dev/null +++ b/app/core/version.py @@ -0,0 +1,28 @@ +"""版本管理模块""" + +import tomli +from pathlib import Path + +# 读取pyproject.toml中的版本号 +def _read_version_from_toml(): + try: + project_root = Path(__file__).parent.parent.parent + toml_path = project_root / "pyproject.toml" + with open(toml_path, "rb") as f: + data = tomli.load(f) + return data["project"]["version"] + except Exception: + return "0.0.0" # 默认版本号 + +# 项目版本号,采用语义化版本格式 (https://semver.org/) +VERSION = _read_version_from_toml() + +# 解析版本号组件 +try: + MAJOR, MINOR, PATCH = map(int, VERSION.split(".")) +except ValueError: + MAJOR = MINOR = PATCH = 0 + +def get_version(): + """获取完整的版本号""" + return VERSION \ No newline at end of file diff --git a/app/main.py b/app/main.py index 1210def..750c59c 100644 --- a/app/main.py +++ b/app/main.py @@ -31,7 +31,7 @@ templates = Jinja2Templates(directory=str(Path(__file__).parent / "templates")) app = FastAPI( title=settings.APP_NAME, description="配置中心API", - version="1.0.1", + version="1.0.0", docs_url="/api/docs", redoc_url="/api/redoc", openapi_url="/api/openapi.json" diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..af7b246 --- /dev/null +++ b/build.sh @@ -0,0 +1,106 @@ +#!/bin/bash + +# 从version.py获取版本号 +VERSION=$(python3 -c "from app.core.version import get_version; print(get_version())") +if [ $? -ne 0 ]; then + echo "错误: 无法获取版本号" + exit 1 +fi +IMAGE_NAME="config-center" +REGISTRY="" +LATEST=false + +# 显示帮助信息 +show_help() { + echo "Usage: $0 [options]" + echo "Options:" + echo " -h, --help 显示帮助信息" + echo " -n, --name 设置镜像名称 (默认: config-center)" + echo " -r, --registry 设置镜像仓库地址" + echo " -v, --version 设置版本号 (默认: 1.0.0)" + echo " -l, --latest 同时构建latest标签" +} + +# 解析命令行参数 +while [[ $# -gt 0 ]]; do + key="$1" + case $key in + -h|--help) + show_help + exit 0 + ;; + -n|--name) + IMAGE_NAME="$2" + shift + shift + ;; + -r|--registry) + REGISTRY="$2" + shift + shift + ;; + -v|--version) + VERSION="$2" + shift + shift + ;; + -l|--latest) + LATEST=true + shift + ;; + *) + echo "未知参数: $1" + show_help + exit 1 + ;; + esac +done + +# 构建完整的镜像名称 +if [ -n "$REGISTRY" ]; then + FULL_IMAGE_NAME="$REGISTRY/$IMAGE_NAME" +else + FULL_IMAGE_NAME="$IMAGE_NAME" +fi + +# 构建Docker镜像 +echo "开始构建Docker镜像: $FULL_IMAGE_NAME:$VERSION" +if ! docker build -t "$FULL_IMAGE_NAME:$VERSION" .; then + echo "错误: Docker镜像构建失败" + exit 1 +fi + +# 如果指定了latest标签,则也构建latest版本 +if [ "$LATEST" = true ]; then + echo "标记latest版本" + if ! docker tag "$FULL_IMAGE_NAME:$VERSION" "$FULL_IMAGE_NAME:latest"; then + echo "错误: 标记latest版本失败" + exit 1 + fi + + # 如果设置了镜像仓库,推送latest版本 + if [ -n "$REGISTRY" ]; then + echo "推送latest版本到镜像仓库" + if ! docker push "$FULL_IMAGE_NAME:latest"; then + echo "错误: 推送latest版本失败" + exit 1 + fi + fi +fi + +# 如果设置了镜像仓库,推送版本化的镜像 +if [ -n "$REGISTRY" ]; then + echo "推送版本化镜像到镜像仓库" + if ! docker push "$FULL_IMAGE_NAME:$VERSION"; then + echo "错误: 推送版本化镜像失败" + exit 1 + fi +fi + +echo "构建完成!" +echo "镜像信息:" +echo " 名称: $FULL_IMAGE_NAME" +echo " 版本: $VERSION" +if [ "$LATEST" = true ]; then + echo " latest标签: 是" +fi \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5579e08 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,12 @@ +[project] +name = "config_center" +version = "1.0.0" +description = "配置中心服务" +authors = [ + {name = "Guyue", email = "guyuecw@qq.com"} +] +requires-python = ">=3.8" + +[build-system] +requires = ["setuptools>=42", "wheel"] +build-backend = "setuptools.build_meta" \ No newline at end of file