deploy-enhanced.ps1 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. # Enhanced Deployment Script for junma-api
  2. # PowerShell版本部署脚本
  3. param(
  4. [string]$Environment = "production"
  5. )
  6. # 设置错误处理
  7. $ErrorActionPreference = "Stop"
  8. # 颜色定义
  9. $Colors = @{
  10. Red = "Red"
  11. Green = "Green"
  12. Yellow = "Yellow"
  13. Cyan = "Cyan"
  14. White = "White"
  15. }
  16. # 日志函数
  17. function Write-LogInfo {
  18. param([string]$Message)
  19. Write-Host $Message -ForegroundColor $Colors.Green
  20. }
  21. function Write-LogWarn {
  22. param([string]$Message)
  23. Write-Host $Message -ForegroundColor $Colors.Yellow
  24. }
  25. function Write-LogError {
  26. param([string]$Message)
  27. Write-Host $Message -ForegroundColor $Colors.Red
  28. }
  29. function Write-LogStep {
  30. param([string]$Message)
  31. Write-Host $Message -ForegroundColor $Colors.Cyan
  32. }
  33. # 错误处理函数
  34. function Handle-Error {
  35. param([string]$ErrorMessage)
  36. Write-LogError "Deployment failed: $ErrorMessage"
  37. exit 1
  38. }
  39. # 主部署流程
  40. try {
  41. Write-LogInfo "Starting junma-api deployment..."
  42. # 拉取最新代码
  43. Write-LogWarn "Pulling latest code..."
  44. try {
  45. # 检查Git是否可用
  46. $gitPath = Get-Command git -ErrorAction SilentlyContinue
  47. if (-not $gitPath) {
  48. Write-LogWarn "Git not found in PATH, trying common locations..."
  49. $commonGitPaths = @(
  50. "C:\Program Files\Git\bin\git.exe",
  51. "C:\Program Files (x86)\Git\bin\git.exe",
  52. "C:\Users\$env:USERNAME\AppData\Local\Programs\Git\bin\git.exe"
  53. )
  54. $gitFound = $false
  55. foreach ($path in $commonGitPaths) {
  56. if (Test-Path $path) {
  57. Write-LogInfo "Found Git at: $path"
  58. & $path pull origin main
  59. $gitFound = $true
  60. break
  61. }
  62. }
  63. if (-not $gitFound) {
  64. Write-LogWarn "Git not found, skipping git pull step"
  65. Write-LogWarn "Please ensure Git is installed and in PATH, or run 'git pull origin main' manually"
  66. }
  67. }
  68. else {
  69. git pull origin main
  70. }
  71. Write-LogInfo "Code pull completed"
  72. }
  73. catch {
  74. Write-LogWarn "Git pull failed: $($_.Exception.Message)"
  75. Write-LogWarn "Continuing with deployment..."
  76. }
  77. # 清理旧的构建文件
  78. Write-LogWarn "Cleaning old build files..."
  79. if (Test-Path "dist") {
  80. Remove-Item -Recurse -Force "dist"
  81. Write-LogInfo "dist directory cleaned"
  82. }
  83. # 清理node_modules(可选)
  84. if (Test-Path "node_modules") {
  85. Write-LogWarn "Cleaning node_modules..."
  86. Remove-Item -Recurse -Force "node_modules"
  87. Write-LogInfo "node_modules cleaned"
  88. }
  89. # 移除锁文件(可选)
  90. if (Test-Path "package-lock.json") {
  91. Remove-Item -Force "package-lock.json"
  92. }
  93. if (Test-Path "yarn.lock") {
  94. Remove-Item -Force "yarn.lock"
  95. }
  96. # 安装依赖
  97. Write-LogWarn "Installing dependencies..."
  98. try {
  99. yarn install
  100. Write-LogInfo "Dependencies installed successfully"
  101. }
  102. catch {
  103. Handle-Error "Dependency installation failed: $($_.Exception.Message)"
  104. }
  105. # 构建生产版本
  106. Write-LogWarn "Building production version..."
  107. try {
  108. yarn build
  109. Write-LogInfo "Build successful"
  110. }
  111. catch {
  112. Handle-Error "Build failed: $($_.Exception.Message)"
  113. }
  114. # 检查构建结果
  115. Write-LogWarn "Checking build results..."
  116. if (-not (Test-Path "dist")) {
  117. Handle-Error "Build failed, dist directory does not exist"
  118. }
  119. # 复制环境配置文件
  120. Write-LogWarn "Copying environment configuration files..."
  121. if (Test-Path ".env.production") {
  122. Copy-Item ".env.production" "dist\.env"
  123. Write-LogInfo "Environment configuration file copied"
  124. }
  125. else {
  126. Write-LogWarn "Warning: .env.production file does not exist"
  127. }
  128. # 复制package.json
  129. Copy-Item "package.json" "dist\package.json"
  130. # 部署到服务器
  131. Write-LogWarn "Deploying to server..."
  132. # SSH配置
  133. $sshKeyPath = "D:\dev tools\key\id_huang.pem"
  134. $serverUser = "root"
  135. $serverHost = "8.210.167.152"
  136. $serverPath = "/var/www/junma-api/"
  137. # 检查SSH密钥文件是否存在
  138. if (-not (Test-Path $sshKeyPath)) {
  139. Handle-Error "SSH key file does not exist: $sshKeyPath"
  140. }
  141. # 尝试不同的上传方法
  142. $uploadSuccess = $false
  143. # 尝试rsync
  144. $rsyncPath = Get-Command rsync -ErrorAction SilentlyContinue
  145. if ($rsyncPath) {
  146. Write-LogStep "Using rsync to upload..."
  147. try {
  148. $rsyncArgs = @(
  149. "--exclude=node_modules/"
  150. "-ravzh"
  151. "--delete"
  152. "-e"
  153. "ssh -i `"$sshKeyPath`" -o StrictHostKeyChecking=no"
  154. "./dist/"
  155. "$serverUser@$serverHost`:$serverPath"
  156. )
  157. & $rsyncPath.Source $rsyncArgs
  158. $uploadSuccess = $true
  159. Write-LogInfo "rsync upload successful"
  160. }
  161. catch {
  162. Write-LogWarn "rsync upload failed, trying scp..."
  163. }
  164. }
  165. else {
  166. Write-LogWarn "rsync not available, trying scp..."
  167. }
  168. # 尝试scp如果rsync失败
  169. if (-not $uploadSuccess) {
  170. $scpPath = Get-Command scp -ErrorAction SilentlyContinue
  171. if ($scpPath) {
  172. Write-LogStep "Using scp to upload..."
  173. try {
  174. $scpArgs = @(
  175. "-r"
  176. "-i"
  177. "`"$sshKeyPath`""
  178. "-o"
  179. "StrictHostKeyChecking=no"
  180. "./dist/*"
  181. "$serverUser@$serverHost`:$serverPath"
  182. )
  183. & $scpPath.Source $scpArgs
  184. $uploadSuccess = $true
  185. Write-LogInfo "scp upload successful"
  186. }
  187. catch {
  188. Handle-Error "scp upload failed: $($_.Exception.Message)"
  189. }
  190. }
  191. else {
  192. Handle-Error "No available upload tools (rsync/scp)"
  193. }
  194. }
  195. if (-not $uploadSuccess) {
  196. Handle-Error "File upload failed - no available upload method found"
  197. }
  198. # 在服务器上执行部署后操作
  199. Write-LogWarn "Executing post-deployment operations on server..."
  200. try {
  201. $sshArgs = @(
  202. "-i"
  203. "`"$sshKeyPath`""
  204. "-o"
  205. "StrictHostKeyChecking=no"
  206. "$serverUser@$serverHost"
  207. "cd /var/www/junma-api && yarn install && pm2 restart junma-api"
  208. )
  209. & ssh $sshArgs
  210. Write-LogInfo "Server deployment operations successful"
  211. }
  212. catch {
  213. Handle-Error "Server deployment operations failed: $($_.Exception.Message)"
  214. }
  215. Write-LogInfo "Deployment completed!"
  216. Write-LogInfo "API service restarted"
  217. Write-LogInfo "Service address: http://8.210.167.152:3000"
  218. }
  219. catch {
  220. Write-LogError "Error occurred during deployment: $($_.Exception.Message)"
  221. exit 1
  222. }