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 ""