fix: deploy.sh reads MySQL config from .env instead of generating it

- Error out if .env is missing, instruct user to copy from .env.example
- Source .env to load MYSQL_* variables for docker compose
- Command-line port argument overrides PORT in .env
This commit is contained in:
2026-06-20 22:53:48 +08:00
parent 95519963c3
commit be7ce2a385
+15 -36
View File
@@ -78,48 +78,27 @@ setup_go_proxy() {
}
# ----------------------------------------------------------
# 4. 生成 .env 配置文件
# 4. 读取 .env 配置文件
# ----------------------------------------------------------
setup_env() {
load_env() {
local port="${1:-9050}"
if [ -f ".env" ]; then
warn ".env 文件已存在,跳过生成"
# 确保端口是最新的
if grep -q "^PORT=" .env; then
sed -i "s/^PORT=.*/PORT=${port}/" .env
else
echo "PORT=${port}" >> .env
fi
return
if [ ! -f ".env" ]; then
error ".env 文件不存在,请先执行: cp .env.example .env 并填写 MySQL 连接信息"
fi
info "生成 .env 配置文件..."
info "读取 .env 配置..."
set -a
# shellcheck disable=SC1091
source .env
set +a
# 生成随机 MySQL 密码
local mysql_password
mysql_password=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 20)
# 命令行参数覆盖 .env 中的 PORT
if [ -n "$port" ]; then
export PORT="$port"
fi
cat > .env <<EOF
# Server
PORT=${port}
GIN_MODE=release
SESSION_SECRET=
# Data directory
DATA_DIR=data
# MySQL
MYSQL_HOST=mysql
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=${mysql_password}
MYSQL_DATABASE=pr_helper
EOF
info ".env 文件已生成"
info "MySQL 密码: ${mysql_password}"
warn "请妥善保管 .env 文件中的数据库密码!"
info "MySQL: ${MYSQL_USER}@${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}"
}
# ----------------------------------------------------------
@@ -184,7 +163,7 @@ main() {
check_docker
setup_docker_mirror
setup_go_proxy
setup_env "$port"
load_env "$port"
build_and_start "$port"
}