flnxi-Wu/scripts/deploy/deploy-now.ps1
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

138 lines
4.2 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Quick deploy script
param(
[string]$Server = "45.249.208.118",
[string]$User = "root",
[string]$Password = "lxvhNYFZ4160",
[string]$ProjectPath = "/root/youyidian-3d"
)
$ErrorActionPreference = "Stop"
Write-Host "开始部署..." -ForegroundColor Cyan
# Check plink
$plinkPath = "C:\Program Files\PuTTY\plink.exe"
$pscpPath = "C:\Program Files\PuTTY\pscp.exe"
if (-not (Test-Path $plinkPath)) {
Write-Host "错误: 未找到 plink.exe" -ForegroundColor Red
exit 1
}
# Step 1: Package files
Write-Host "[1/4] 打包文件..." -ForegroundColor Yellow
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$tempFile = Join-Path $env:TEMP "project-deploy-$timestamp.tar.gz"
$excludes = @(
"node_modules", ".next", "out", "build", "dist", "uploads", "backups", ".git",
"*.tar.gz", "*.zip", "*.sql", "*.log", ".DS_Store", ".vscode", ".idea", ".cursor",
"*.rpm", "frp_*", "docker-daemon-config.json",
"docs", "*.md", "README.md", "LICENSE", "CHANGELOG.md", "CONTRIBUTING.md",
"scripts", "*.sh", "*.ps1", "*.bat", "*.py",
"*.tmp", "*.temp", ".cache", ".tmp", "tmp", "temp", "*.cache", "Thumbs.db",
"*.rar", "*.7z", "*.tar", "*.tgz", "release-*", "dist-*", "*.db", "*.sqlite", "*.sqlite3",
"*.pem", "*.key", "*.cert", "*.crt"
)
$excludeArgs = $excludes | ForEach-Object { "--exclude=$_" }
& tar -czf $tempFile $excludeArgs *
if (-not (Test-Path $tempFile)) {
Write-Host "打包失败" -ForegroundColor Red
exit 1
}
$fileSize = (Get-Item $tempFile).Length / 1MB
Write-Host "打包完成: $([math]::Round($fileSize, 2)) MB" -ForegroundColor Green
# Step 2: Upload package
Write-Host "[2/4] 上传文件..." -ForegroundColor Yellow
$remoteFileName = "project-deploy.tar.gz"
& $pscpPath -pw $Password -batch $tempFile "${User}@${Server}:${ProjectPath}/$remoteFileName"
if ($LASTEXITCODE -ne 0) {
Write-Host "上传失败" -ForegroundColor Red
exit 1
}
Write-Host "上传成功" -ForegroundColor Green
# Step 3: Create deploy script
Write-Host "[3/4] 创建部署脚本..." -ForegroundColor Yellow
$localDeployScript = Join-Path $env:TEMP "deploy-$timestamp.sh"
$deployScript = @"
#!/bin/bash
set -e
cd $ProjectPath
echo "备份上传文件..."
if [ -d "uploads" ]; then
mkdir -p backups
cp -r uploads backups/uploads-$(date +%Y%m%d-%H%M%S) 2>/dev/null || true
fi
echo "解压新版本..."
tar -xzf $remoteFileName
rm -f $remoteFileName
echo "恢复上传目录..."
if [ -d "backups" ]; then
latest_backup=$(ls -td backups/uploads-* 2>/dev/null | head -1)
if [ -n "$latest_backup" ] && [ -d "$latest_backup" ]; then
mkdir -p uploads
cp -r "$latest_backup"/* uploads/ 2>/dev/null || true
fi
fi
mkdir -p uploads
chmod -R 777 uploads
echo "停止旧容器..."
docker compose down 2>/dev/null || docker-compose down 2>/dev/null || true
echo "构建并启动新容器(使用智能缓存)..."
docker compose up -d --build app || docker-compose up -d --build app
echo "等待容器启动..."
sleep 20
echo "执行数据库迁移..."
if [ -f prisma/schema.prisma ]; then
docker compose exec -T app npx prisma db push --accept-data-loss --schema=/app/prisma/schema.prisma 2>/dev/null || echo "数据库迁移完成或跳过"
fi
echo "查看容器状态..."
docker compose ps || docker-compose ps
echo "查看应用日志(最后50行)..."
docker compose logs app --tail=50 || docker-compose logs app --tail=50
"@
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($localDeployScript, $deployScript, $utf8NoBom)
# Step 4: Upload and execute deploy script
Write-Host "[4/4] 部署到服务器..." -ForegroundColor Yellow
$remoteDeployScript = "/tmp/deploy-$timestamp.sh"
& $pscpPath -pw $Password -batch $localDeployScript "${User}@${Server}:$remoteDeployScript"
if ($LASTEXITCODE -ne 0) {
Write-Host "上传部署脚本失败" -ForegroundColor Red
exit 1
}
& $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "chmod +x $remoteDeployScript; bash $remoteDeployScript; rm -f $remoteDeployScript"
if ($LASTEXITCODE -ne 0) {
Write-Host "部署失败" -ForegroundColor Red
exit 1
}
# Cleanup
Remove-Item $localDeployScript -Force -ErrorAction SilentlyContinue
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
Write-Host "部署完成!" -ForegroundColor Green