GuDingZiChan/scripts/publish-to-server.ps1
2026-03-10 17:05:06 +08:00

104 lines
3.1 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

param(
[string]$HostName = "1.13.22.133",
[string]$User = "root",
[string]$RemoteDir = "/opt/equipment-management",
[string]$RemoteArchive = "/tmp/equipment-management-release.tar.gz",
[switch]$SkipDeploy,
[switch]$SkipBackup
)
$ErrorActionPreference = "Stop"
$projectRoot = Split-Path -Parent $PSScriptRoot
$password = if ($env:DEPLOY_PASSWORD) { $env:DEPLOY_PASSWORD } else { "" }
$archiveName = "equipment-management-release.tar.gz"
$archivePath = Join-Path ([System.IO.Path]::GetTempPath()) $archiveName
$fileListPath = Join-Path ([System.IO.Path]::GetTempPath()) "equipment-management-files.txt"
function Get-CommandPath([string]$name) {
$cmd = Get-Command $name -ErrorAction SilentlyContinue
if (-not $cmd) {
throw "未找到命令: $name"
}
return $cmd.Source
}
function Invoke-External {
param(
[string]$FilePath,
[string[]]$Arguments,
[switch]$IgnoreExitCode
)
& $FilePath @Arguments
if (-not $IgnoreExitCode -and $LASTEXITCODE -ne 0) {
throw "命令执行失败: $FilePath $($Arguments -join ' ')"
}
}
$tarExe = Get-CommandPath "tar"
$sshExe = Get-CommandPath "ssh"
$scpExe = Get-CommandPath "scp"
$sshpassExe = $null
if ($password) {
$sshpassExe = Get-CommandPath "sshpass"
}
Set-Location $projectRoot
$files = git ls-files --cached --others --exclude-standard
if (-not $files) {
throw "未找到可发布文件。"
}
$files | Set-Content -Path $fileListPath -Encoding utf8
if (Test-Path $archivePath) {
Remove-Item $archivePath -Force
}
Write-Host ">>> 打包当前仓库源码..."
Invoke-External -FilePath $tarExe -Arguments @("-czf", $archivePath, "-T", $fileListPath)
$remoteTarget = "$User@$HostName"
$sshBaseArgs = @("-o", "StrictHostKeyChecking=no")
$scpBaseArgs = @("-o", "StrictHostKeyChecking=no")
if ($password) {
Write-Host ">>> 使用 DEPLOY_PASSWORD 进行上传和远程执行..."
Invoke-External -FilePath $sshpassExe -Arguments @("-p", $password, $scpExe) + $scpBaseArgs + @($archivePath, "${remoteTarget}:$RemoteArchive")
} else {
Write-Host ">>> 使用 SSH 密钥或交互密码进行上传和远程执行..."
Invoke-External -FilePath $scpExe -Arguments $scpBaseArgs + @($archivePath, "${remoteTarget}:$RemoteArchive")
}
$skipBackupValue = if ($SkipBackup) { "1" } else { "0" }
$deployCommand = @"
set -e
mkdir -p '$RemoteDir'
cd '$RemoteDir'
rm -rf backend frontend scripts AGENTS.md BACKUP.md README.md deploy.sh docker-compose.yml Dockerfile
tar -xzf '$RemoteArchive' -C '$RemoteDir'
sed -i 's/\r$//' '$RemoteDir/deploy.sh' '$RemoteDir/scripts/backup-equipment.sh'
chmod +x '$RemoteDir/deploy.sh' '$RemoteDir/scripts/backup-equipment.sh'
if [ '$($SkipDeploy.IsPresent)' = 'True' ]; then
echo '>>> 已设置 SkipDeploy,仅同步文件,不执行部署'
else
if [ '$skipBackupValue' = '1' ]; then
SKIP_BACKUP=1 '$RemoteDir/deploy.sh'
else
'$RemoteDir/deploy.sh'
fi
fi
rm -f '$RemoteArchive'
"@
if ($password) {
Invoke-External -FilePath $sshpassExe -Arguments @("-p", $password, $sshExe) + $sshBaseArgs + @($remoteTarget, $deployCommand)
} else {
Invoke-External -FilePath $sshExe -Arguments $sshBaseArgs + @($remoteTarget, $deployCommand)
}
Write-Host ">>> 发布流程完成"