| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- # Enhanced Deployment Script for junma-api
- # PowerShell版本部署脚本
- param(
- [string]$Environment = "production"
- )
- # 设置错误处理
- $ErrorActionPreference = "Stop"
- # 颜色定义
- $Colors = @{
- Red = "Red"
- Green = "Green"
- Yellow = "Yellow"
- Cyan = "Cyan"
- White = "White"
- }
- # 日志函数
- function Write-LogInfo {
- param([string]$Message)
- Write-Host $Message -ForegroundColor $Colors.Green
- }
- function Write-LogWarn {
- param([string]$Message)
- Write-Host $Message -ForegroundColor $Colors.Yellow
- }
- function Write-LogError {
- param([string]$Message)
- Write-Host $Message -ForegroundColor $Colors.Red
- }
- function Write-LogStep {
- param([string]$Message)
- Write-Host $Message -ForegroundColor $Colors.Cyan
- }
- # 错误处理函数
- function Handle-Error {
- param([string]$ErrorMessage)
- Write-LogError "Deployment failed: $ErrorMessage"
- exit 1
- }
- # 主部署流程
- try {
- Write-LogInfo "Starting junma-api deployment..."
- # 拉取最新代码
- Write-LogWarn "Pulling latest code..."
- try {
- # 检查Git是否可用
- $gitPath = Get-Command git -ErrorAction SilentlyContinue
- if (-not $gitPath) {
- Write-LogWarn "Git not found in PATH, trying common locations..."
- $commonGitPaths = @(
- "C:\Program Files\Git\bin\git.exe",
- "C:\Program Files (x86)\Git\bin\git.exe",
- "C:\Users\$env:USERNAME\AppData\Local\Programs\Git\bin\git.exe"
- )
-
- $gitFound = $false
- foreach ($path in $commonGitPaths) {
- if (Test-Path $path) {
- Write-LogInfo "Found Git at: $path"
- & $path pull origin main
- $gitFound = $true
- break
- }
- }
-
- if (-not $gitFound) {
- Write-LogWarn "Git not found, skipping git pull step"
- Write-LogWarn "Please ensure Git is installed and in PATH, or run 'git pull origin main' manually"
- }
- }
- else {
- git pull origin main
- }
- Write-LogInfo "Code pull completed"
- }
- catch {
- Write-LogWarn "Git pull failed: $($_.Exception.Message)"
- Write-LogWarn "Continuing with deployment..."
- }
- # 清理旧的构建文件
- Write-LogWarn "Cleaning old build files..."
- if (Test-Path "dist") {
- Remove-Item -Recurse -Force "dist"
- Write-LogInfo "dist directory cleaned"
- }
- # 清理node_modules(可选)
- if (Test-Path "node_modules") {
- Write-LogWarn "Cleaning node_modules..."
- Remove-Item -Recurse -Force "node_modules"
- Write-LogInfo "node_modules cleaned"
- }
- # 移除锁文件(可选)
- if (Test-Path "package-lock.json") {
- Remove-Item -Force "package-lock.json"
- }
- if (Test-Path "yarn.lock") {
- Remove-Item -Force "yarn.lock"
- }
- # 安装依赖
- Write-LogWarn "Installing dependencies..."
- try {
- yarn install
- Write-LogInfo "Dependencies installed successfully"
- }
- catch {
- Handle-Error "Dependency installation failed: $($_.Exception.Message)"
- }
- # 构建生产版本
- Write-LogWarn "Building production version..."
- try {
- yarn build
- Write-LogInfo "Build successful"
- }
- catch {
- Handle-Error "Build failed: $($_.Exception.Message)"
- }
- # 检查构建结果
- Write-LogWarn "Checking build results..."
- if (-not (Test-Path "dist")) {
- Handle-Error "Build failed, dist directory does not exist"
- }
- # 复制环境配置文件
- Write-LogWarn "Copying environment configuration files..."
- if (Test-Path ".env.production") {
- Copy-Item ".env.production" "dist\.env"
- Write-LogInfo "Environment configuration file copied"
- }
- else {
- Write-LogWarn "Warning: .env.production file does not exist"
- }
- # 复制package.json
- Copy-Item "package.json" "dist\package.json"
- # 部署到服务器
- Write-LogWarn "Deploying to server..."
-
- # SSH配置
- $sshKeyPath = "D:\dev tools\key\id_huang.pem"
- $serverUser = "root"
- $serverHost = "8.210.167.152"
- $serverPath = "/var/www/junma-api/"
-
- # 检查SSH密钥文件是否存在
- if (-not (Test-Path $sshKeyPath)) {
- Handle-Error "SSH key file does not exist: $sshKeyPath"
- }
-
- # 尝试不同的上传方法
- $uploadSuccess = $false
-
- # 尝试rsync
- $rsyncPath = Get-Command rsync -ErrorAction SilentlyContinue
- if ($rsyncPath) {
- Write-LogStep "Using rsync to upload..."
- try {
- $rsyncArgs = @(
- "--exclude=node_modules/"
- "-ravzh"
- "--delete"
- "-e"
- "ssh -i `"$sshKeyPath`" -o StrictHostKeyChecking=no"
- "./dist/"
- "$serverUser@$serverHost`:$serverPath"
- )
- & $rsyncPath.Source $rsyncArgs
- $uploadSuccess = $true
- Write-LogInfo "rsync upload successful"
- }
- catch {
- Write-LogWarn "rsync upload failed, trying scp..."
- }
- }
- else {
- Write-LogWarn "rsync not available, trying scp..."
- }
-
- # 尝试scp如果rsync失败
- if (-not $uploadSuccess) {
- $scpPath = Get-Command scp -ErrorAction SilentlyContinue
- if ($scpPath) {
- Write-LogStep "Using scp to upload..."
- try {
- $scpArgs = @(
- "-r"
- "-i"
- "`"$sshKeyPath`""
- "-o"
- "StrictHostKeyChecking=no"
- "./dist/*"
- "$serverUser@$serverHost`:$serverPath"
- )
- & $scpPath.Source $scpArgs
- $uploadSuccess = $true
- Write-LogInfo "scp upload successful"
- }
- catch {
- Handle-Error "scp upload failed: $($_.Exception.Message)"
- }
- }
- else {
- Handle-Error "No available upload tools (rsync/scp)"
- }
- }
-
- if (-not $uploadSuccess) {
- Handle-Error "File upload failed - no available upload method found"
- }
- # 在服务器上执行部署后操作
- Write-LogWarn "Executing post-deployment operations on server..."
- try {
- $sshArgs = @(
- "-i"
- "`"$sshKeyPath`""
- "-o"
- "StrictHostKeyChecking=no"
- "$serverUser@$serverHost"
- "cd /var/www/junma-api && yarn install && pm2 restart junma-api"
- )
- & ssh $sshArgs
- Write-LogInfo "Server deployment operations successful"
- }
- catch {
- Handle-Error "Server deployment operations failed: $($_.Exception.Message)"
- }
- Write-LogInfo "Deployment completed!"
- Write-LogInfo "API service restarted"
- Write-LogInfo "Service address: http://8.210.167.152:3000"
- }
- catch {
- Write-LogError "Error occurred during deployment: $($_.Exception.Message)"
- exit 1
- }
|