From 8b83caf7fc4dabbce9ccb3684ad61c085af724dd Mon Sep 17 00:00:00 2001 From: zhangtao <9480807882@qq.com> Date: Mon, 9 Mar 2026 00:13:12 +0800 Subject: [PATCH] =?UTF-8?q?docs(deploy):=20=E6=9B=B4=E6=96=B0=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E6=96=87=E6=A1=A3=E5=B9=B6=E5=A2=9E=E5=BC=BA=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E8=84=9A=E6=9C=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加两种部署方式的详细说明文档 为部署脚本增加 restart 和 update 命令 优化脚本执行位置检测逻辑 --- README.en.md | 46 +++++++++++++++++++++++++++++++++++++++++----- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++++++----- deploy.sh | 43 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 129 insertions(+), 12 deletions(-) diff --git a/README.en.md b/README.en.md index 85439657..488a24c4 100644 --- a/README.en.md +++ b/README.en.md @@ -186,17 +186,53 @@ pnpm run build ### 🐳 Docker Deployment +#### Method 1: Execute script inside project (Recommended) + ```bash -# Copy the deployment script to the server and grant execution permissions +# 1. Clone the repository to server +git clone https://gitee.com/fastapiadmin/FastapiAdmin.git +cd FastapiAdmin + +# 2. Grant execution permissions and deploy chmod +x deploy.sh -# Execute one-click deployment -./deploy.sh or ./deploy.sh --start +./deploy.sh + # View container logs -./deploy.sh --logs +./deploy.sh logs + # Stop services -./deploy.sh --stop +./deploy.sh stop ``` +#### Method 2: Execute script outside project + +```bash +# 1. Copy the deployment script to server +cp deploy.sh /home/ +cd /home +chmod +x deploy.sh + +# 2. Execute one-click deployment (will auto clone project) +./deploy.sh + +# View container logs +./deploy.sh logs + +# Stop services +./deploy.sh stop + +# Restart services +./deploy.sh restart + +# Update code and restart (without rebuilding images, suitable for backend hot update) +./deploy.sh update +``` + +> **Note**: +> - First deployment will automatically pull code and build images +> - Frontend uses locally built dist directory, please build locally and commit to repository if you need to update frontend +> - Ensure `devops/nginx/ssl/` directory contains SSL certificate files (if using HTTPS) + ## 🛠️ Secondary Development Tutorial ### Backend Development diff --git a/README.md b/README.md index 529662ab..0016d63a 100644 --- a/README.md +++ b/README.md @@ -188,17 +188,59 @@ pnpm run build ### 🐳 Docker 部署 +#### 方式一:脚本放在项目内执行(推荐) + ```bash -# 复制部署脚本到服务器并赋予执行权限 +# 1. 克隆代码到服务器 +git clone https://gitee.com/fastapiadmin/FastapiAdmin.git +cd FastapiAdmin + +# 2. 赋予执行权限并部署 chmod +x deploy.sh -# 执行一键部署 -./deploy.sh or ./deploy.sh --start +./deploy.sh + # 查看容器日志 -./deploy.sh --logs +./deploy.sh logs + # 停止服务 -./deploy.sh --stop +./deploy.sh stop + +# 重启服务 +./deploy.sh restart + +# 更新代码并重启(不重新构建镜像,适合后端代码热更新) +./deploy.sh update ``` +#### 方式二:脚本放在项目外执行 + +```bash +# 1. 将部署脚本复制到服务器 +cp deploy.sh /home/ +cd /home +chmod +x deploy.sh + +# 2. 执行一键部署(会自动克隆项目) +./deploy.sh + +# 查看容器日志 +./deploy.sh logs + +# 停止服务 +./deploy.sh stop + +# 重启服务 +./deploy.sh restart + +# 更新代码并重启(不重新构建镜像,适合后端代码热更新) +./deploy.sh update +``` + +> **注意**: +> - 首次部署时会自动拉取代码并构建镜像 +> - 前端使用本地构建的 dist 目录,如需更新前端请先本地构建并提交到仓库 +> - 确保 `devops/nginx/ssl/` 目录包含 SSL 证书文件(如使用 HTTPS) + ## 🛠️ 二开教程 ### 后端开发 diff --git a/deploy.sh b/deploy.sh index cca2f864..601afbfa 100644 --- a/deploy.sh +++ b/deploy.sh @@ -3,7 +3,17 @@ # 设置全局变量 PROJECT_NAME="FastapiAdmin" WORK_DIR="$(cd "$(dirname "$0")" && pwd)" -PROJECT_PATH="${WORK_DIR}/${PROJECT_NAME}" + +# 检测脚本执行位置 +# 如果当前目录包含 docker-compose.yaml,说明脚本在项目内执行 +if [ -f "${WORK_DIR}/docker-compose.yaml" ] || [ -f "${WORK_DIR}/docker-compose.yml" ]; then + # 脚本在项目内执行,项目路径就是当前目录 + PROJECT_PATH="${WORK_DIR}" +else + # 脚本在项目外执行,项目路径是子目录 + PROJECT_PATH="${WORK_DIR}/${PROJECT_NAME}" +fi + GIT_REPO="https://gitee.com/fastapiadmin/${PROJECT_NAME}.git" # 是否有更新前端 @@ -276,6 +286,25 @@ if [ $# -eq 0 ]; then exit 0 fi +# 重启容器函数 +restart_containers() { + log "==========🔄 重启容器...==========" "INFO" + cd "${PROJECT_PATH}" || { log "❌ 无法进入项目目录:${PROJECT_PATH}" "ERROR"; exit 1; } + docker compose restart || { log "❌ 容器重启失败" "ERROR"; exit 1; } + log "✅ 容器重启成功" "INFO" + docker compose ps +} + +# 更新代码并重启函数 +update_and_restart() { + log "==========🔄 更新代码并重启...==========" "INFO" + update_code + cd "${PROJECT_PATH}" || { log "❌ 无法进入项目目录:${PROJECT_PATH}" "ERROR"; exit 1; } + docker compose restart || { log "❌ 容器重启失败" "ERROR"; exit 1; } + log "✅ 代码更新并重启成功" "INFO" + docker compose ps +} + while [[ $# -gt 0 ]]; do case $1 in start) @@ -286,6 +315,14 @@ while [[ $# -gt 0 ]]; do stop_project exit 0 ;; + restart) + restart_containers + exit 0 + ;; + update) + update_and_restart + exit 0 + ;; logs) show_containers_logs exit 0 @@ -296,7 +333,9 @@ while [[ $# -gt 0 ]]; do echo "" echo "选项:" echo " stop 停止所有容器" - echo " start 启动所有容器" + echo " start 启动所有容器(完整部署流程)" + echo " restart 重启所有容器" + echo " update 更新代码并重启容器(不重新构建镜像)" echo " logs 查看所有容器日志" echo " help|-h 显示此帮助信息" echo ""