142 lines
4.7 KiB
PowerShell
142 lines
4.7 KiB
PowerShell
# Simple deploy script
|
|
param(
|
|
[string]$Server = "45.249.208.118",
|
|
[string]$User = "root",
|
|
[string]$Password = "lxvhNYFZ4160",
|
|
[string]$ProjectPath = "/root/youyidian-3d"
|
|
)
|
|
|
|
$ErrorActionPreference = "Stop"
|
|
Write-Host "Starting deployment..." -ForegroundColor Cyan
|
|
|
|
$plinkPath = "C:\Program Files\PuTTY\plink.exe"
|
|
$pscpPath = "C:\Program Files\PuTTY\pscp.exe"
|
|
|
|
if (-not (Test-Path $plinkPath)) {
|
|
Write-Host "Error: plink.exe not found" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Package files
|
|
Write-Host "[1/4] Packaging files..." -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 "Packaging failed" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
$fileSize = (Get-Item $tempFile).Length / 1MB
|
|
Write-Host "Package created: $([math]::Round($fileSize, 2)) MB" -ForegroundColor Green
|
|
|
|
# Upload package
|
|
Write-Host "[2/4] Uploading package..." -ForegroundColor Yellow
|
|
$remoteFileName = "project-deploy.tar.gz"
|
|
& $pscpPath -pw $Password -batch $tempFile "${User}@${Server}:${ProjectPath}/$remoteFileName"
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "Upload failed" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
Write-Host "Upload successful" -ForegroundColor Green
|
|
|
|
# Create deploy script
|
|
Write-Host "[3/4] Creating deploy script..." -ForegroundColor Yellow
|
|
$localDeployScript = Join-Path $env:TEMP "deploy-$timestamp.sh"
|
|
|
|
$deployScript = @'
|
|
#!/bin/bash
|
|
set -e
|
|
cd PROJECT_PATH_PLACEHOLDER
|
|
|
|
echo "Backing up uploads..."
|
|
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 "Extracting new version..."
|
|
tar -xzf REMOTE_FILE_NAME_PLACEHOLDER
|
|
rm -f REMOTE_FILE_NAME_PLACEHOLDER
|
|
|
|
echo "Restoring uploads..."
|
|
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 "Stopping old containers..."
|
|
docker compose down 2>/dev/null || docker-compose down 2>/dev/null || true
|
|
|
|
echo "Building and starting new containers..."
|
|
docker compose up -d --build --force-recreate || docker-compose up -d --build --force-recreate
|
|
|
|
echo "Waiting for containers to start..."
|
|
sleep 20
|
|
|
|
echo "Running database migration..."
|
|
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 "Database migration completed or skipped"
|
|
fi
|
|
|
|
echo "Container status:"
|
|
docker compose ps || docker-compose ps
|
|
|
|
echo "Application logs (last 50 lines):"
|
|
docker compose logs app --tail=50 || docker-compose logs app --tail=50
|
|
'@
|
|
|
|
$deployScript = $deployScript -replace "PROJECT_PATH_PLACEHOLDER", $ProjectPath
|
|
$deployScript = $deployScript -replace "REMOTE_FILE_NAME_PLACEHOLDER", $remoteFileName
|
|
|
|
# Write file with Unix line endings (LF only, no CRLF)
|
|
$deployScript = $deployScript -replace "`r`n", "`n" -replace "`r", "`n"
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
|
|
$bytes = $utf8NoBom.GetBytes($deployScript)
|
|
[System.IO.File]::WriteAllBytes($localDeployScript, $bytes)
|
|
|
|
# Upload and execute deploy script
|
|
Write-Host "[4/4] Deploying to server..." -ForegroundColor Yellow
|
|
$remoteDeployScript = "/tmp/deploy-$timestamp.sh"
|
|
|
|
& $pscpPath -pw $Password -batch $localDeployScript "${User}@${Server}:$remoteDeployScript"
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Host "Failed to upload deploy script" -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 "Deployment failed" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
# Cleanup
|
|
Remove-Item $localDeployScript -Force -ErrorAction SilentlyContinue
|
|
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
|
|
|
|
Write-Host "Deployment completed successfully!" -ForegroundColor Green
|
|
|