fix: update deploy.sh to generate .env with MySQL configuration
- Generate .env with random MySQL password on first deploy - Update PORT in .env instead of modifying docker-compose.yml directly - Wait for MySQL container healthcheck before checking app status - Skip .env generation if file already exists
This commit is contained in:
@@ -78,36 +78,89 @@ setup_go_proxy() {
|
||||
}
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# 4. 配置服务端口
|
||||
# 4. 生成 .env 配置文件
|
||||
# ----------------------------------------------------------
|
||||
setup_port() {
|
||||
setup_env() {
|
||||
local port="${1:-9050}"
|
||||
local compose_file="docker-compose.yml"
|
||||
|
||||
info "配置服务端口为 ${port}..."
|
||||
sed -i "s/- \"8080:8080\"/- \"${port}:8080\"/" "$compose_file"
|
||||
info "端口配置完成: ${port}"
|
||||
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
|
||||
fi
|
||||
|
||||
info "生成 .env 配置文件..."
|
||||
|
||||
# 生成随机 MySQL 密码
|
||||
local mysql_password
|
||||
mysql_password=$(openssl rand -base64 16 | tr -dc 'a-zA-Z0-9' | head -c 20)
|
||||
|
||||
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 文件中的数据库密码!"
|
||||
}
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# 5. 构建并启动服务
|
||||
# ----------------------------------------------------------
|
||||
build_and_start() {
|
||||
local port="${1:-9050}"
|
||||
|
||||
info "构建 Docker 镜像(首次可能需要几分钟)..."
|
||||
docker compose build
|
||||
|
||||
info "启动服务..."
|
||||
docker compose up -d
|
||||
|
||||
info "等待服务启动..."
|
||||
info "等待 MySQL 就绪..."
|
||||
local retries=30
|
||||
while [ $retries -gt 0 ]; do
|
||||
if docker compose exec -T mysql mysqladmin ping -h localhost --silent 2>/dev/null; then
|
||||
info "MySQL 就绪"
|
||||
break
|
||||
fi
|
||||
retries=$((retries - 1))
|
||||
sleep 2
|
||||
done
|
||||
|
||||
if [ $retries -eq 0 ]; then
|
||||
warn "MySQL 启动超时,继续等待应用自行重连..."
|
||||
sleep 10
|
||||
fi
|
||||
|
||||
info "等待应用启动..."
|
||||
sleep 3
|
||||
|
||||
if docker compose ps | grep -q "running\|Up"; then
|
||||
info "=========================================="
|
||||
info " PR-Helper 部署成功!"
|
||||
info "=========================================="
|
||||
info "访问地址: http://$(hostname -I | awk '{print $1}'):${PORT:-9050}"
|
||||
info "本地访问: http://localhost:${PORT:-9050}"
|
||||
info "访问地址: http://$(hostname -I | awk '{print $1}'):${port}"
|
||||
info "本地访问: http://localhost:${port}"
|
||||
info ""
|
||||
info "数据库配置: .env"
|
||||
info ""
|
||||
info "常用命令:"
|
||||
info " 查看日志: docker compose logs -f"
|
||||
@@ -124,7 +177,6 @@ build_and_start() {
|
||||
# ----------------------------------------------------------
|
||||
main() {
|
||||
local port="${1:-9050}"
|
||||
export PORT="$port"
|
||||
|
||||
info "开始部署 PR-Helper..."
|
||||
info "目标端口: ${port}"
|
||||
@@ -132,8 +184,8 @@ main() {
|
||||
check_docker
|
||||
setup_docker_mirror
|
||||
setup_go_proxy
|
||||
setup_port "$port"
|
||||
build_and_start
|
||||
setup_env "$port"
|
||||
build_and_start "$port"
|
||||
}
|
||||
|
||||
main "$@"
|
||||
|
||||
Reference in New Issue
Block a user