fix(deploy): 改进环境文件处理逻辑并添加缺失检查

添加对环境文件存在性的检查,当文件不存在时显示警告而非直接退出
当未指定环境文件时,允许仅通过配置文件加载配置
This commit is contained in:
zhouyonggao 2025-11-28 16:47:20 +08:00
parent 0f603041d9
commit 455859dab6
1 changed files with 33 additions and 18 deletions

View File

@ -7,19 +7,22 @@ TAG="$ENV_NAME"
PORT="${PORT:-8077}" PORT="${PORT:-8077}"
cd "$ROOT_DIR" cd "$ROOT_DIR"
ENV_FILE_OVERRIDE="${2:-}" ENV_FILE_OVERRIDE="${2:-}"
ENV_FILE=""
if [ -n "$ENV_FILE_OVERRIDE" ]; then if [ -n "$ENV_FILE_OVERRIDE" ]; then
if [ -f "$ENV_FILE_OVERRIDE" ]; then
ENV_FILE="$ENV_FILE_OVERRIDE" ENV_FILE="$ENV_FILE_OVERRIDE"
else else
echo "警告:指定的 env 文件不存在:$ENV_FILE_OVERRIDE,忽略 --env-file" >&2
fi
else
if [ -f ".env.$ENV_NAME" ]; then
ENV_FILE=".env.$ENV_NAME" ENV_FILE=".env.$ENV_NAME"
if [ ! -f "$ENV_FILE" ] && [ -f "server/.env.$ENV_NAME" ]; then elif [ -f "server/.env.$ENV_NAME" ]; then
ENV_FILE="server/.env.$ENV_NAME" ENV_FILE="server/.env.$ENV_NAME"
fi fi
fi fi
if [ ! -f "$ENV_FILE" ]; then if [ -z "$ENV_FILE" ]; then
echo "$ENV_FILE not found" >&2 echo "未使用 env 文件,配置仅通过 server/config.$ENV_NAME.yaml 加载" >&2
echo "请创建 .env.$ENV_NAME 或指定第二个参数为 env 文件路径" >&2
echo "示例server/.env.example" >&2
exit 1
fi fi
docker build -t "$IMAGE:$TAG" -f Dockerfile . docker build -t "$IMAGE:$TAG" -f Dockerfile .
mkdir -p log storage/export mkdir -p log storage/export
@ -36,6 +39,7 @@ if [ ! -f "$CONFIG_PATH" ]; then
echo "配置文件缺失:$CONFIG_PATH" >&2 echo "配置文件缺失:$CONFIG_PATH" >&2
exit 1 exit 1
fi fi
if [ -n "$ENV_FILE" ]; then
docker run -d \ docker run -d \
--name "$CID_NAME" \ --name "$CID_NAME" \
--restart unless-stopped \ --restart unless-stopped \
@ -46,4 +50,15 @@ docker run -d \
-v "$ROOT_DIR/log:/app/log" \ -v "$ROOT_DIR/log:/app/log" \
-v "$CONFIG_PATH:/app/server/config.$ENV_NAME.yaml:ro" \ -v "$CONFIG_PATH:/app/server/config.$ENV_NAME.yaml:ro" \
"$IMAGE:$TAG" "$IMAGE:$TAG"
else
docker run -d \
--name "$CID_NAME" \
--restart unless-stopped \
--env APP_ENV="$ENV_NAME" \
-p "$PORT:8077" \
-v "$ROOT_DIR/storage/export:/app/storage/export" \
-v "$ROOT_DIR/log:/app/log" \
-v "$CONFIG_PATH:/app/server/config.$ENV_NAME.yaml:ro" \
"$IMAGE:$TAG"
fi
echo "container: $CID_NAME image: $IMAGE:$TAG port: $PORT" echo "container: $CID_NAME image: $IMAGE:$TAG port: $PORT"