deploy-frontend.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #!/bin/bash
  2. # ============================================
  3. # 灵越智报 - 前端部署到 sygpudev 服务器
  4. # 本地构建 → 上传 dist → 配置 Nginx
  5. # ============================================
  6. # 颜色定义
  7. RED='\033[0;31m'
  8. GREEN='\033[0;32m'
  9. YELLOW='\033[1;33m'
  10. BLUE='\033[0;34m'
  11. NC='\033[0m'
  12. # ========== 配置区域 ==========
  13. SERVER_USER="root"
  14. SERVER_HOST="sygpudev"
  15. SERVER_PORT="${SERVER_PORT:-28529}"
  16. # 路径配置
  17. LOCAL_PROJECT_DIR="/home/hws/workspace/GitLab/ay/lingyue-zhibao"
  18. LOCAL_FRONTEND_DIR="${LOCAL_PROJECT_DIR}/frontend/vue-demo"
  19. LOCAL_DIST_DIR="${LOCAL_FRONTEND_DIR}/dist"
  20. REMOTE_APP_DIR="/data/application/lingyue-zhibao"
  21. REMOTE_FRONTEND_DIR="/data/web/lingyue-zhibao"
  22. REMOTE_NGINX_CONF="/etc/nginx/conf.d/lingyue-frontend.conf"
  23. # 后端 API 地址(前端请求代理目标)
  24. BACKEND_API="http://127.0.0.1:8001"
  25. # 前端监听端口
  26. FRONTEND_PORT=28082
  27. # ==============================
  28. log_info() { echo -e "${GREEN}[INFO]${NC} $1"; }
  29. log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
  30. log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
  31. log_title() { echo -e "\n${BLUE}========== $1 ==========${NC}\n"; }
  32. ssh_cmd() {
  33. ssh -p ${SERVER_PORT} ${SERVER_USER}@${SERVER_HOST} "$@"
  34. }
  35. # 检查服务器连接
  36. check_connection() {
  37. log_title "检查服务器连接"
  38. if ssh_cmd "echo 'OK'" > /dev/null 2>&1; then
  39. log_info "服务器连接成功: ${SERVER_USER}@${SERVER_HOST}"
  40. else
  41. log_error "无法连接服务器: ${SERVER_USER}@${SERVER_HOST}"
  42. log_warn "请检查:"
  43. log_warn " 1. 服务器地址是否正确"
  44. log_warn " 2. SSH 密钥是否配置"
  45. exit 1
  46. fi
  47. }
  48. # 安装依赖
  49. install_deps() {
  50. log_title "安装前端依赖"
  51. cd ${LOCAL_FRONTEND_DIR}
  52. if [ ! -d "node_modules" ]; then
  53. log_info "安装 npm 依赖..."
  54. yarn install || npm install
  55. else
  56. log_info "依赖已存在,跳过安装"
  57. fi
  58. }
  59. # 本地构建
  60. build_local() {
  61. log_title "本地构建前端"
  62. cd ${LOCAL_FRONTEND_DIR}
  63. # 清理旧构建
  64. rm -rf dist
  65. log_info "执行 yarn build..."
  66. yarn build || npm run build
  67. if [ ! -d "${LOCAL_DIST_DIR}" ]; then
  68. log_error "构建失败: dist 目录不存在"
  69. exit 1
  70. fi
  71. local dist_size=$(du -sh "${LOCAL_DIST_DIR}" | awk '{print $1}')
  72. log_info "构建完成,dist 大小: ${dist_size}"
  73. }
  74. # 上传 dist
  75. upload_dist() {
  76. log_title "上传前端文件到服务器"
  77. # 创建远程目录
  78. ssh_cmd "mkdir -p ${REMOTE_FRONTEND_DIR}"
  79. # 备份旧版本
  80. ssh_cmd "cd ${REMOTE_APP_DIR} && \
  81. [ -d frontend ] && mv frontend frontend.bak.$(date +%Y%m%d%H%M%S) || true"
  82. # 上传新版本
  83. log_info "正在上传 dist 目录..."
  84. scp -P ${SERVER_PORT} -r "${LOCAL_DIST_DIR}" "${SERVER_USER}@${SERVER_HOST}:${REMOTE_FRONTEND_DIR}"
  85. # 移动文件到正确位置
  86. ssh_cmd "cd ${REMOTE_FRONTEND_DIR} && mv dist/* . && rmdir dist"
  87. local remote_size=$(ssh_cmd "du -sh ${REMOTE_FRONTEND_DIR}" | awk '{print $1}')
  88. log_info "上传完成,远程目录大小: ${remote_size}"
  89. }
  90. # 配置 Nginx
  91. setup_nginx() {
  92. log_title "配置 Nginx"
  93. # 检查 Nginx 是否安装
  94. if ! ssh_cmd "which nginx" > /dev/null 2>&1; then
  95. log_warn "Nginx 未安装,正在安装..."
  96. ssh_cmd "apt update && apt install -y nginx"
  97. fi
  98. # 生成 Nginx 配置
  99. local nginx_conf="server {
  100. listen ${FRONTEND_PORT};
  101. server_name _;
  102. root ${REMOTE_FRONTEND_DIR};
  103. index index.html;
  104. # Gzip 压缩
  105. gzip on;
  106. gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
  107. # 静态资源缓存
  108. location ~* \\.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)\$ {
  109. expires 1y;
  110. add_header Cache-Control \"public, immutable\";
  111. }
  112. # API 代理到后端
  113. location /api {
  114. proxy_pass ${BACKEND_API};
  115. proxy_set_header Host \\\$host;
  116. proxy_set_header X-Real-IP \\\$remote_addr;
  117. proxy_set_header X-Forwarded-For \\\$proxy_add_x_forwarded_for;
  118. proxy_set_header X-Forwarded-Proto \\\$scheme;
  119. # WebSocket 支持
  120. proxy_http_version 1.1;
  121. proxy_set_header Upgrade \\\$http_upgrade;
  122. proxy_set_header Connection \"upgrade\";
  123. # 超时设置
  124. proxy_connect_timeout 60s;
  125. proxy_send_timeout 60s;
  126. proxy_read_timeout 60s;
  127. }
  128. # SPA 路由支持
  129. location / {
  130. try_files \\\$uri \\\$uri/ /index.html;
  131. }
  132. # 文件上传大小限制
  133. client_max_body_size 100M;
  134. }"
  135. # 写入配置文件
  136. echo "${nginx_conf}" | ssh_cmd "cat > ${REMOTE_NGINX_CONF}"
  137. # 测试配置
  138. if ssh_cmd "nginx -t" 2>&1 | grep -q "successful"; then
  139. log_info "Nginx 配置测试通过"
  140. else
  141. log_error "Nginx 配置有误"
  142. ssh_cmd "nginx -t"
  143. exit 1
  144. fi
  145. # 重载 Nginx
  146. ssh_cmd "systemctl reload nginx"
  147. log_info "Nginx 已重载"
  148. }
  149. # 健康检查
  150. health_check() {
  151. log_title "健康检查"
  152. local max_attempts=6
  153. local attempt=1
  154. while [ $attempt -le $max_attempts ]; do
  155. log_info "尝试 ${attempt}/${max_attempts}..."
  156. local response=$(ssh_cmd "curl -s -o /dev/null -w '%{http_code}' http://localhost:${FRONTEND_PORT}/ 2>/dev/null" || echo "000")
  157. if [ "$response" = "200" ]; then
  158. log_info "前端服务正常 ✓ (HTTP ${response})"
  159. return 0
  160. fi
  161. sleep 2
  162. attempt=$((attempt + 1))
  163. done
  164. log_warn "健康检查未通过,请手动检查"
  165. }
  166. # 完整部署
  167. full_deploy() {
  168. check_connection
  169. install_deps
  170. build_local
  171. upload_dist
  172. setup_nginx
  173. health_check
  174. log_title "前端部署完成 🎉"
  175. # 获取服务器 IP
  176. local server_ip=$(ssh_cmd "hostname -I | awk '{print \$1}'" 2>/dev/null || echo "${SERVER_HOST}")
  177. echo -e "${GREEN}访问地址: http://${server_ip}:${FRONTEND_PORT}${NC}"
  178. echo ""
  179. }
  180. # 快速部署(跳过依赖安装)
  181. quick_deploy() {
  182. if [ ! -d "${LOCAL_DIST_DIR}" ]; then
  183. log_error "本地 dist 不存在,请先构建或使用 deploy 命令"
  184. exit 1
  185. fi
  186. check_connection
  187. upload_dist
  188. setup_nginx
  189. health_check
  190. log_title "快速部署完成 🎉"
  191. }
  192. # 仅构建
  193. build_only() {
  194. install_deps
  195. build_local
  196. log_info "构建完成,dist 目录: ${LOCAL_DIST_DIR}"
  197. }
  198. # 查看状态
  199. show_status() {
  200. log_title "前端服务状态"
  201. check_connection
  202. if ssh_cmd "curl -s -o /dev/null -w '%{http_code}' http://localhost:${FRONTEND_PORT}/" 2>/dev/null | grep -q "200"; then
  203. log_info "前端服务运行中 ✓"
  204. else
  205. log_warn "前端服务未响应"
  206. fi
  207. log_info "Nginx 状态:"
  208. ssh_cmd "systemctl status nginx --no-pager -l" 2>/dev/null | head -10
  209. }
  210. # 帮助
  211. show_help() {
  212. cat <<EOF
  213. 灵越智报 - 前端部署到 sygpudev 服务器
  214. 用法: ./deploy-frontend.sh [命令]
  215. 命令:
  216. deploy 完整部署(安装依赖 + 构建 + 上传 + 配置 Nginx)
  217. quick 快速部署(上传已构建的 dist + 配置 Nginx)
  218. build 仅本地构建
  219. upload 仅上传 dist(不配置 Nginx)
  220. nginx 仅配置 Nginx
  221. status 查看服务状态
  222. help 显示此帮助
  223. 示例:
  224. ./deploy-frontend.sh deploy # 完整部署
  225. ./deploy-frontend.sh quick # 快速部署(已构建过)
  226. ./deploy-frontend.sh build # 仅构建
  227. 配置:
  228. 服务器: ${SERVER_USER}@${SERVER_HOST}:${SERVER_PORT}
  229. 前端端口: ${FRONTEND_PORT}
  230. 后端 API: ${BACKEND_API}
  231. EOF
  232. }
  233. # 主函数
  234. main() {
  235. case "${1:-deploy}" in
  236. deploy)
  237. full_deploy
  238. ;;
  239. quick)
  240. quick_deploy
  241. ;;
  242. build)
  243. build_only
  244. ;;
  245. upload)
  246. check_connection
  247. upload_dist
  248. log_info "上传完成(未配置 Nginx)"
  249. ;;
  250. nginx)
  251. check_connection
  252. setup_nginx
  253. health_check
  254. ;;
  255. status)
  256. show_status
  257. ;;
  258. help|--help|-h)
  259. show_help
  260. ;;
  261. *)
  262. log_error "未知命令: $1"
  263. show_help
  264. exit 1
  265. ;;
  266. esac
  267. }
  268. main "$@"