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

201 lines
7.6 KiB
PowerShell

# Try to deploy and check what happens
param(
[string]$Server = "45.249.208.118",
[string]$User = "root",
[string]$Password = "lxvhNYFZ4160",
[string]$ProjectPath = "/root/youyidian-3d"
)
$ErrorActionPreference = "Stop"
Write-Host "Attempting deployment with current server state..." -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
}
Write-Host "`n[1/6] Checking server Docker images..." -ForegroundColor Yellow
$allImages = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "docker images --format 'table {{.Repository}}\t{{.Tag}}\t{{.ID}}' 2>&1"
Write-Host $allImages
Write-Host "`n[2/6] Checking Docker build cache..." -ForegroundColor Yellow
$cacheInfo = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "docker system df 2>&1"
Write-Host $cacheInfo
Write-Host "`n[3/6] Checking if project directory exists..." -ForegroundColor Yellow
$projectExists = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "test -d $ProjectPath && echo 'exists' || echo 'not found'"
Write-Host "Project directory: $projectExists" -ForegroundColor Cyan
if ($projectExists -notmatch "exists") {
Write-Host "Project directory not found. Creating..." -ForegroundColor Yellow
& $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "mkdir -p $ProjectPath"
}
Write-Host "`n[4/6] Checking Dockerfile on server..." -ForegroundColor Yellow
$dockerfileCheck = & $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "test -f $ProjectPath/Dockerfile && echo 'exists' || echo 'not found'"
Write-Host "Dockerfile: $dockerfileCheck" -ForegroundColor Cyan
# First, let's try to upload the source code (this is what deploy-simple.ps1 does)
Write-Host "`n[5/6] Packaging and uploading source code..." -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
$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 that tries to build
Write-Host "`n[6/6] Creating and executing deploy script..." -ForegroundColor Yellow
$localDeployScript = Join-Path $env:TEMP "deploy-$timestamp.sh"
$deployScript = @'
#!/bin/bash
set -e
cd PROJECT_PATH_PLACEHOLDER
echo "=== Deployment Script ==="
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 "=== Attempting Docker Build ==="
echo "This may fail if base image is not available..."
echo "Checking if we can pull base image..."
# Try to pull the base image first
if docker pull node:18-alpine 2>&1 | tee /tmp/docker-pull.log; then
echo "Base image pulled successfully"
else
echo "WARNING: Failed to pull base image"
echo "Pull log:"
cat /tmp/docker-pull.log
echo ""
echo "Checking if base image exists locally..."
if docker images node:18-alpine --format "{{.Repository}}:{{.Tag}}" | grep -q "node:18-alpine"; then
echo "Base image exists locally, continuing..."
else
echo "ERROR: Base image not found and cannot be pulled"
echo "You need to manually import the image or fix network access"
exit 1
fi
fi
echo "Building Docker image..."
if docker compose build --no-cache 2>&1 | tee /tmp/docker-build.log; then
echo "Build successful"
else
echo "Build failed. Log:"
tail -50 /tmp/docker-build.log
echo ""
echo "Trying with cache..."
if docker compose build 2>&1 | tee /tmp/docker-build-cache.log; then
echo "Build with cache successful"
else
echo "Build with cache also failed. Log:"
tail -50 /tmp/docker-build-cache.log
exit 1
fi
fi
echo "Starting containers..."
docker compose up -d --force-recreate || docker-compose up -d --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
$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)
$remoteDeployScript = "/tmp/deploy-$timestamp.sh"
& $pscpPath -pw $Password -batch $localDeployScript "${User}@${Server}:$remoteDeployScript"
& $plinkPath -ssh -pw $Password -batch "${User}@${Server}" "chmod +x $remoteDeployScript; bash $remoteDeployScript; rm -f $remoteDeployScript"
if ($LASTEXITCODE -eq 0) {
Write-Host "`n[SUCCESS] Deployment completed!" -ForegroundColor Green
} else {
Write-Host "`n[WARN] Deployment may have encountered issues" -ForegroundColor Yellow
Write-Host "Check the output above for details" -ForegroundColor Yellow
}
# Cleanup
Remove-Item $localDeployScript -Force -ErrorAction SilentlyContinue
Remove-Item $tempFile -Force -ErrorAction SilentlyContinue
Write-Host "`n=== Next Steps ===" -ForegroundColor Cyan
Write-Host "If deployment failed due to missing base image:" -ForegroundColor Yellow
Write-Host "1. Check server logs: ssh $User@$Server 'cat /tmp/docker-pull.log'" -ForegroundColor White
Write-Host "2. Check build logs: ssh $User@$Server 'cat /tmp/docker-build.log'" -ForegroundColor White
Write-Host "3. Manually import image using: .\scripts\deploy\export-and-upload-image.ps1" -ForegroundColor White
Write-Host " (Requires Docker Desktop running locally)" -ForegroundColor Gray