增强开发部署构建配置

This commit is contained in:
Cheng Zhou
2026-06-17 17:23:13 +08:00
parent 9b2b8e90f6
commit e507ab98cb
5 changed files with 52 additions and 4 deletions
+5
View File
@@ -1,6 +1,7 @@
services:
db:
image: postgres:15-alpine
pull_policy: if_not_present
container_name: ctms_db
restart: always
environment:
@@ -24,6 +25,7 @@ services:
build:
context: ./backend
dockerfile: Dockerfile
pull: false
container_name: ctms_backend
restart: always
ports:
@@ -44,6 +46,7 @@ services:
build:
context: ./backend
dockerfile: Dockerfile
pull: false
command: python scripts/init_production.py
environment:
DATABASE_URL: postgresql+asyncpg://ctms_user:secret_password@db/ctms_db
@@ -62,7 +65,9 @@ services:
build:
context: .
dockerfile: nginx/Dockerfile
pull: false
args:
NPM_CONFIG_REGISTRY: ${NPM_CONFIG_REGISTRY:-https://registry.npmmirror.com}
VITE_RUNTIME_ENV: ${ENV:-production}
VITE_ALLOW_INSECURE_DEV_LOGIN: ${VITE_ALLOW_INSECURE_DEV_LOGIN:-false}
container_name: ctms_nginx
+11 -1
View File
@@ -2,8 +2,18 @@ FROM node:20-alpine AS build
WORKDIR /app
ARG NPM_CONFIG_REGISTRY=https://registry.npmjs.org/
ENV NPM_CONFIG_REGISTRY=$NPM_CONFIG_REGISTRY
COPY package*.json ./
RUN npm ci
RUN --mount=type=cache,target=/root/.npm \
npm ci \
--prefer-offline \
--no-audit \
--fetch-retries=5 \
--fetch-retry-mintimeout=20000 \
--fetch-retry-maxtimeout=120000 \
--replace-registry-host=npmjs
COPY . .
RUN npm run build
+3
View File
@@ -29,6 +29,9 @@ export default defineConfig({
server: {
host: true,
port: 5173,
hmr: {
clientPort: 8888,
},
proxy: {
"/api": {
target: "http://backend:8000",
+10 -1
View File
@@ -2,13 +2,22 @@ FROM node:20-alpine AS frontend-build
WORKDIR /app
ARG NPM_CONFIG_REGISTRY=https://registry.npmjs.org/
ARG VITE_RUNTIME_ENV=production
ARG VITE_ALLOW_INSECURE_DEV_LOGIN=false
ENV NPM_CONFIG_REGISTRY=$NPM_CONFIG_REGISTRY
ENV VITE_RUNTIME_ENV=$VITE_RUNTIME_ENV
ENV VITE_ALLOW_INSECURE_DEV_LOGIN=$VITE_ALLOW_INSECURE_DEV_LOGIN
COPY frontend/package*.json ./
RUN npm ci
RUN --mount=type=cache,target=/root/.npm \
npm ci \
--prefer-offline \
--no-audit \
--fetch-retries=5 \
--fetch-retry-mintimeout=20000 \
--fetch-retry-maxtimeout=120000 \
--replace-registry-host=npmjs
COPY frontend/ ./
RUN npm run build
+23 -2
View File
@@ -472,10 +472,18 @@ run_backend_init() {
run_build_and_start() {
if [[ "$SKIP_BUILD" -eq 1 ]]; then
step "启动服务(跳过镜像构建)"
run_quiet "启动容器" -- compose_cmd up -d
if [[ "$TARGET_ENV" == "dev" ]]; then
run_quiet "启动开发容器并刷新后端/Nginx/Vite" -- compose_cmd up -d --force-recreate backend nginx frontend-dev
else
run_quiet "启动容器" -- compose_cmd up -d
fi
else
step "构建镜像并启动服务"
run_quiet "构建镜像并启动容器(首次较慢)" -- compose_cmd up -d --build
if [[ "$TARGET_ENV" == "dev" ]]; then
run_quiet "构建并刷新开发容器(首次较慢)" -- compose_cmd up -d --build --force-recreate backend nginx frontend-dev
else
run_quiet "构建镜像并启动容器(首次较慢)" -- compose_cmd up -d --build
fi
fi
}
@@ -571,6 +579,7 @@ run_health_checks() {
local expect_rsa=0
[[ "$runtime_env" == "production" ]] && expect_rsa=1
check_container_status
check_dev_nginx_mode
check_backend_environment "$runtime_env" "$login_key_id" "$expect_rsa"
step "探测 HTTP 接口可用性"
check_http_endpoint "/health"
@@ -578,6 +587,18 @@ run_health_checks() {
ok "HTTP 接口全部就绪"
}
check_dev_nginx_mode() {
[[ "$TARGET_ENV" == "dev" ]] || return 0
step "校验开发 Nginx 代理配置"
if compose_cmd exec -T nginx sh -c 'grep -q "frontend_dev" /etc/nginx/nginx.conf && grep -q "proxy_pass http://frontend_dev" /etc/nginx/nginx.conf' >/dev/null 2>&1; then
ok "Nginx 已代理到 Vite dev server"
return
fi
fail "当前 Nginx 未加载开发配置,请重试:bash scripts/install-ctms.sh dev --skip-migrate --verbose"
}
# ── 完成提示 ─────────────────────────────────
show_success() {