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

152 lines
4.9 KiB
PowerShell

# Simple deploy script without Chinese characters
param(
[string]$Server = "45.249.208.118",
[string]$User = "root",
[string]$ProjectPath = "/root/youyidian-3d"
)
$ErrorActionPreference = "Stop"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deploy Script" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# Check if in project root
if (-not (Test-Path "package.json")) {
Write-Host "Error: Please run this script from project root directory" -ForegroundColor Red
exit 1
}
# Step 1: Package project files
Write-Host "[1/4] Packaging project files..." -ForegroundColor Yellow
$excludes = @(
"node_modules",
".next",
".git",
".env.local",
".env*.local",
"dist",
"build",
".DS_Store",
"Thumbs.db",
".vscode",
".idea",
".cursor",
"*.swp",
"*.swo",
"docs",
"*.md",
"README.md",
"scripts",
"*.sh",
"*.ps1",
"*.bat",
"uploads",
"backups",
"*.tar.gz",
"*.zip"
)
$excludeArgs = $excludes | ForEach-Object { "--exclude=$_" }
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
$tempFile = Join-Path $env:TEMP "project-deploy-$timestamp.tar.gz"
Write-Host " Packaging..." -ForegroundColor Gray
& tar -czf $tempFile $excludeArgs *
if (-not (Test-Path $tempFile)) {
Write-Host " Failed to package" -ForegroundColor Red
exit 1
}
$fileSize = (Get-Item $tempFile).Length / 1MB
Write-Host " Package complete: $([math]::Round($fileSize, 2)) MB" -ForegroundColor Green
Write-Host ""
# Step 2: Upload to server
Write-Host "[2/4] Uploading to server..." -ForegroundColor Yellow
Write-Host " Server: $Server" -ForegroundColor Gray
Write-Host " User: $User" -ForegroundColor Gray
Write-Host " Project Path: $ProjectPath" -ForegroundColor Gray
Write-Host " Note: Please enter SSH password if prompted" -ForegroundColor Yellow
Write-Host ""
$remoteFileName = "project-deploy.tar.gz"
Write-Host " Uploading package..." -ForegroundColor Gray
scp $tempFile "${User}@${Server}:${ProjectPath}/$remoteFileName"
if ($LASTEXITCODE -ne 0) {
Write-Host " Upload failed" -ForegroundColor Red
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
exit 1
}
Write-Host " Upload successful" -ForegroundColor Green
Write-Host ""
# Step 3: Create deployment script
Write-Host "[3/4] Creating deployment script..." -ForegroundColor Yellow
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$templatePath = Join-Path $scriptDir "deploy-remote.sh.template"
if (-not (Test-Path $templatePath)) {
Write-Host " Error: Template not found: $templatePath" -ForegroundColor Red
exit 1
}
$bashScriptTemplate = Get-Content -Path $templatePath -Raw -Encoding UTF8
$bashScript = $bashScriptTemplate -replace "PROJECT_PATH_PLACEHOLDER", $ProjectPath
$bashScript = $bashScript -replace "REMOTE_FILE_NAME_PLACEHOLDER", $remoteFileName
$localDeployScript = Join-Path $env:TEMP "deploy-$timestamp.sh"
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
[System.IO.File]::WriteAllText($localDeployScript, $bashScript, $utf8NoBom)
$remoteDeployScript = "/tmp/deploy-$timestamp.sh"
Write-Host " Uploading deployment script..." -ForegroundColor Gray
scp $localDeployScript "${User}@${Server}:$remoteDeployScript"
if ($LASTEXITCODE -ne 0) {
Write-Host " Failed to upload deployment script" -ForegroundColor Red
Remove-Item $localDeployScript -Force -ErrorAction SilentlyContinue
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
exit 1
}
Write-Host " Deployment script uploaded" -ForegroundColor Green
Write-Host ""
# Step 4: Execute deployment on server
Write-Host "[4/4] Executing deployment on server..." -ForegroundColor Yellow
Write-Host " Note: Please enter SSH password if prompted" -ForegroundColor Yellow
Write-Host ""
ssh "${User}@${Server}" "chmod +x $remoteDeployScript; bash $remoteDeployScript; rm -f $remoteDeployScript"
if ($LASTEXITCODE -ne 0) {
Write-Host " Deployment failed on server" -ForegroundColor Red
Remove-Item $localDeployScript -Force -ErrorAction SilentlyContinue
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
exit 1
}
# Cleanup
Remove-Item $localDeployScript -Force -ErrorAction SilentlyContinue
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
Write-Host " Deployment successful" -ForegroundColor Green
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deployment Complete!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Yellow
Write-Host " 1. Check website status" -ForegroundColor White
Write-Host " 2. Test key features" -ForegroundColor White
Write-Host " 3. Check logs: ssh ${User}@${Server} `"cd ${ProjectPath}; docker compose logs app --tail=100`"" -ForegroundColor White
Write-Host ""