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

209 lines
7.1 KiB
PowerShell

# Deploy script with password support
param(
[string]$Server = "45.249.208.118",
[string]$User = "root",
[string]$Password = "lxvhNYFZ4160",
[string]$ProjectPath = "/root/youyidian-3d"
)
$ErrorActionPreference = "Stop"
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Deploy Script with Password" -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/5] 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: Check for plink/pscp (PuTTY tools)
Write-Host "[2/5] Checking SSH tools..." -ForegroundColor Yellow
$plinkPath = "C:\Program Files\PuTTY\plink.exe"
$pscpPath = "C:\Program Files\PuTTY\pscp.exe"
$usePlink = (Test-Path $plinkPath) -and (Test-Path $pscpPath)
if ($usePlink) {
Write-Host " Using plink/pscp (PuTTY tools)" -ForegroundColor Green
} else {
Write-Host " Using OpenSSH (will prompt for password)" -ForegroundColor Yellow
Write-Host " Note: If SSH key is not configured, you may need to enter password manually" -ForegroundColor Gray
}
Write-Host ""
# Step 3: Upload to server
Write-Host "[3/5] 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 ""
$remoteFileName = "project-deploy.tar.gz"
Write-Host " Uploading package..." -ForegroundColor Gray
if ($usePlink) {
# Use pscp with password
$pscpCmd = "`"$pscpPath`" -pw `"$Password`" -batch `"$tempFile`" ${User}@${Server}:${ProjectPath}/$remoteFileName"
$result = cmd /c $pscpCmd 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host " Upload failed" -ForegroundColor Red
Write-Host $result -ForegroundColor Red
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
exit 1
}
} else {
# Use OpenSSH scp (may need SSH key or manual password entry)
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 4: Create and upload deployment script
Write-Host "[4/5] 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
# Convert Windows line endings to Unix line endings
$bashScript = $bashScript -replace "`r`n", "`n" -replace "`r", "`n"
$localDeployScript = Join-Path $env:TEMP "deploy-$timestamp.sh"
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
# Write with Unix line endings
$bytes = $utf8NoBom.GetBytes($bashScript)
[System.IO.File]::WriteAllBytes($localDeployScript, $bytes)
$remoteDeployScript = "/tmp/deploy-$timestamp.sh"
Write-Host " Uploading deployment script..." -ForegroundColor Gray
if ($usePlink) {
$pscpCmd = "`"$pscpPath`" -pw `"$Password`" -batch `"$localDeployScript`" ${User}@${Server}:$remoteDeployScript"
$result = cmd /c $pscpCmd 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host " Failed to upload deployment script" -ForegroundColor Red
Write-Host $result -ForegroundColor Red
Remove-Item $localDeployScript -Force -ErrorAction SilentlyContinue
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
exit 1
}
} else {
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 5: Execute deployment on server
Write-Host "[5/5] Executing deployment on server..." -ForegroundColor Yellow
Write-Host " This may take several minutes..." -ForegroundColor Gray
Write-Host ""
$deployCmd = "chmod +x $remoteDeployScript; bash $remoteDeployScript; rm -f $remoteDeployScript"
if ($usePlink) {
$plinkCmd = "`"$plinkPath`" -ssh -pw `"$Password`" -batch ${User}@${Server} `"$deployCmd`""
Write-Host " Executing deployment..." -ForegroundColor Gray
cmd /c $plinkCmd
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
}
} else {
ssh "${User}@${Server}" $deployCmd
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 ""
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 ""