Files
chat/deploy.ps1
2026-04-23 15:01:58 +08:00

48 lines
2.4 KiB
PowerShell

# 部署脚本 - 自动打包并部署到远程服务器
$serverIp = "64.90.11.73"
$serverUser = "root"
$port = 38080
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host " 开始部署 AI Chat 应用到 $serverIp" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
# 1. 编译前端
Write-Host "`n[1/4] 开始构建前端应用 (chat-web)..." -ForegroundColor Yellow
Set-Location -Path "chat-web"
# 检查 node_modules,如果没有则安装依赖
if (!(Test-Path "node_modules")) {
npm install
}
npm run build
Set-Location -Path ".."
# 2. 拷贝前端资源到后端
Write-Host "`n[2/4] 将前端构建产物拷贝到后端静态资源目录..." -ForegroundColor Yellow
$staticPath = "chat-server\src\main\resources\static"
if (!(Test-Path $staticPath)) {
New-Item -ItemType Directory -Force -Path $staticPath | Out-Null
}
Remove-Item "$staticPath\*" -Recurse -Force -ErrorAction SilentlyContinue
Copy-Item -Path "chat-web\dist\*" -Destination $staticPath -Recurse -Force
# 3. 准备远端构建目录并上传源码
Write-Host "`n[3/4] 准备远端目录并上传源码和 Dockerfile..." -ForegroundColor Yellow
ssh ${serverUser}@${serverIp} "mkdir -p /root/chat-deploy"
Write-Host "正在上传后端源码和构建文件,如果出现提示请输入服务器 $serverIp 的 root 密码:" -ForegroundColor Magenta
# 使用原始包含多阶段构建的 Dockerfile
scp "chat-server\Dockerfile" "${serverUser}@${serverIp}:/root/chat-deploy/"
scp "chat-server\pom.xml" "${serverUser}@${serverIp}:/root/chat-deploy/"
# 上传 src 目录(包含刚刚拷贝进去的前端静态资源)
scp -r "chat-server\src" "${serverUser}@${serverIp}:/root/chat-deploy/"
# 4. 远程构建和部署
Write-Host "`n[4/4] 正在远程 Docker 中编译并构建镜像,请再次输入服务器密码:" -ForegroundColor Magenta
ssh ${serverUser}@${serverIp} "cd /root/chat-deploy ; docker build -t chat-server:latest . ; docker stop chat-server ; docker rm chat-server ; docker run -d --name chat-server -p ${port}:8080 chat-server:latest"
Write-Host "`n=============================================" -ForegroundColor Green
Write-Host " 部署完成!" -ForegroundColor Green
Write-Host " 请访问浏览器:http://${serverIp}:${port}" -ForegroundColor Green
Write-Host "=============================================" -ForegroundColor Green