build(deploy): 优化 uv 安装与部署并发保护

This commit is contained in:
yml2213
2026-10-23 09:59:32 +08:00
parent f074b75871
commit 331208742e
3 changed files with 78 additions and 10 deletions
+12 -8
View File
@@ -27,6 +27,7 @@ RUN --mount=type=cache,target=/root/.npm \
FROM python:3.12-slim AS runtime
ARG USE_CHINA_MIRRORS=true
ARG UV_VERSION=0.13.5
# 服务器默认使用阿里云镜像;本地构建可设 USE_CHINA_MIRRORS=false 直连官方源。
# apt 缓存挂载:包索引和 .deb 都留在 BuildKit 缓存里,不再每次重下。
@@ -50,24 +51,28 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
fonts-wqy-microhei
# 安装 uv:服务器走阿里云镜像,本地直连官方 PyPI。
# mirrors.cloud.aliyuncs.com 的证书与主机名不匹配,直接用 mirrors.aliyun.com,
# 避免每次构建先失败重试 10 秒;版本固定,基础层重建时行为保持一致。
RUN --mount=type=cache,target=/root/.cache/pip \
if [ "$USE_CHINA_MIRRORS" = "true" ]; then \
pip install -i https://mirrors.cloud.aliyuncs.com/pypi/simple/ uv || \
pip install -i https://mirrors.aliyun.com/pypi/simple/ uv || \
pip install uv; \
(pip install --retries 2 --timeout 30 \
-i https://mirrors.aliyun.com/pypi/simple/ "uv==${UV_VERSION}" || \
pip install --retries 2 --timeout 30 "uv==${UV_VERSION}"); \
else \
pip install uv; \
pip install --retries 2 --timeout 30 "uv==${UV_VERSION}"; \
fi
WORKDIR /app
# 缓存目录和 .venv 位于不同文件系统时 uv 会自动回退为复制;显式设置可避免构建告警。
ENV UV_LINK_MODE=copy
# 先复制依赖文件,利用 Docker 缓存层
COPY pyproject.toml uv.lock* README.md alembic.ini ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv venv /app/.venv && \
if [ "$USE_CHINA_MIRRORS" = "true" ]; then \
(UV_INDEX_URL=https://mirrors.cloud.aliyuncs.com/pypi/simple/ uv pip install -e . || \
UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ uv pip install -e . || \
(UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ uv pip install -e . || \
uv pip install -e .); \
else \
uv pip install -e .; \
@@ -117,8 +122,7 @@ COPY tests/ ./tests/
COPY conftest.py ./
RUN --mount=type=cache,target=/root/.cache/uv \
if [ "$USE_CHINA_MIRRORS" = "true" ]; then \
(UV_INDEX_URL=https://mirrors.cloud.aliyuncs.com/pypi/simple/ uv sync --frozen --group dev || \
UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ uv sync --frozen --group dev || \
(UV_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/ uv sync --frozen --group dev || \
uv sync --frozen --group dev); \
else \
uv sync --frozen --group dev; \