deploy.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/bin/bash
  2. # ============================================================
  3. # 部署脚本 — 订单发货管理系统 v2.0
  4. # 使用方法:bash deploy.sh [install|build|start|restart|stop|status]
  5. # ============================================================
  6. set -e
  7. APP_NAME="order-shipping"
  8. APP_DIR="$(cd "$(dirname "$0")" && pwd)"
  9. NODE_VERSION="18"
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. NC='\033[0m'
  14. log() { echo -e "${GREEN}[✓]${NC} $1"; }
  15. warn() { echo -e "${YELLOW}[!]${NC} $1"; }
  16. err() { echo -e "${RED}[]${NC} $1"; }
  17. # 检查 Node.js
  18. check_node() {
  19. if ! command -v node &>/dev/null; then
  20. err "Node.js 未安装,请先安装 Node.js ${NODE_VERSION}+"
  21. exit 1
  22. fi
  23. log "Node.js $(node -v) 已安装"
  24. }
  25. # 安装依赖
  26. install() {
  27. log "安装后端依赖..."
  28. cd "$APP_DIR" && npm install
  29. log "安装前端依赖并构建..."
  30. cd "$APP_DIR/web" && npm install && npm run build
  31. log "安装 PM2..."
  32. npm install -g pm2
  33. log "依赖安装完成"
  34. }
  35. # 构建前端
  36. build() {
  37. log "构建前端..."
  38. cd "$APP_DIR/web" && npm run build
  39. log "前端构建完成 → web/dist/"
  40. }
  41. # 启动服务
  42. start() {
  43. check_node
  44. log "使用 PM2 启动服务..."
  45. cd "$APP_DIR" && pm2 start ecosystem.config.js
  46. log "服务已启动"
  47. pm2 status
  48. }
  49. # 重启服务
  50. restart() {
  51. log "重启服务..."
  52. cd "$APP_DIR" && pm2 restart "$APP_NAME"
  53. log "服务已重启"
  54. pm2 status
  55. }
  56. # 停止服务
  57. stop() {
  58. log "停止服务..."
  59. cd "$APP_DIR" && pm2 stop "$APP_NAME"
  60. log "服务已停止"
  61. }
  62. # 查看状态
  63. status() {
  64. pm2 status
  65. echo ""
  66. pm2 logs "$APP_NAME" --lines 20 --nostream
  67. }
  68. # 查看日志
  69. logs() {
  70. pm2 logs "$APP_NAME" --lines 50
  71. }
  72. # 主逻辑
  73. case "${1:-help}" in
  74. install) install ;;
  75. build) build ;;
  76. start) start ;;
  77. restart) restart ;;
  78. stop) stop ;;
  79. status) status ;;
  80. logs) logs ;;
  81. *)
  82. echo "用法: bash deploy.sh [install|build|start|restart|stop|status|logs]"
  83. echo ""
  84. echo " install - 安装所有依赖 + 构建前端 + 安装PM2"
  85. echo " build - 仅构建前端"
  86. echo " start - 启动服务(PM2)"
  87. echo " restart - 重启服务"
  88. echo " stop - 停止服务"
  89. echo " status - 查看服务状态 + 最近日志"
  90. echo " logs - 实时查看日志"
  91. ;;
  92. esac