Files
huishou/deploy.ps1
T
2026-07-27 13:37:48 +08:00

62 lines
2.4 KiB
PowerShell

$hostName = "hs.yifenbao.cn"
$userName = "root"
$password = "Torch1112"
$deployPath = "/www/huishou"
$localZip = "D:\Trae\huishou\deploy.zip"
$remoteZip = "$deployPath/deploy.zip"
$secPassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($userName, $secPassword)
Write-Host "Step 1: 清理服务器目录..."
try {
$cleanCmd = "mkdir -p $deployPath && rm -rf $deployPath/admin $deployPath/dist $deployPath/server $deployPath/*.yml $deployPath/*.dockerignore $deployPath/Dockerfile $deployPath/deploy.zip 2>/dev/null"
$session = New-SSHSession -ComputerName $hostName -Credential $credential -AcceptKey:$true -Force
Invoke-SSHCommand -SSHSession $session -Command $cleanCmd
Remove-SSHSession -SSHSession $session
Write-Host "清理完成"
} catch {
Write-Host "清理失败: $_"
}
Write-Host "`nStep 2: 上传部署文件..."
try {
scp -o StrictHostKeyChecking=no "$localZip" "${userName}@${hostName}:${remoteZip}"
Write-Host "上传完成"
} catch {
Write-Host "上传失败: $_"
}
Write-Host "`nStep 3: 解压文件..."
try {
$session = New-SSHSession -ComputerName $hostName -Credential $credential -AcceptKey:$true -Force
Invoke-SSHCommand -SSHSession $session -Command "cd $deployPath && unzip -o deploy.zip && rm deploy.zip"
Remove-SSHSession -SSHSession $session
Write-Host "解压完成"
} catch {
Write-Host "解压失败: $_"
}
Write-Host "`nStep 4: 启动Docker容器..."
try {
$session = New-SSHSession -ComputerName $hostName -Credential $credential -AcceptKey:$true -Force
Invoke-SSHCommand -SSHSession $session -Command "cd $deployPath && docker stop huishou huishou-backend 2>/dev/null; docker rm huishou huishou-backend 2>/dev/null; docker-compose up -d"
Remove-SSHSession -SSHSession $session
Write-Host "Docker启动命令已执行"
} catch {
Write-Host "Docker启动失败: $_"
}
Write-Host "`nStep 5: 等待容器启动并检查状态..."
Start-Sleep -Seconds 15
try {
$session = New-SSHSession -ComputerName $hostName -Credential $credential -AcceptKey:$true -Force
$result = Invoke-SSHCommand -SSHSession $session -Command "docker ps | grep huishou"
Write-Host "容器状态:"
Write-Host $result.Output
Remove-SSHSession -SSHSession $session
} catch {
Write-Host "检查容器状态失败: $_"
}
Write-Host "`n部署完成!"