实现Salon生产流水线自动准备

This commit is contained in:
2026-08-27 12:39:20 +08:00
parent 0df0a86d9a
commit 5211c2d6cd
4 changed files with 292 additions and 6 deletions
@@ -8,6 +8,12 @@ param(
[switch]$SkipTests,
[switch]$AllowDirtyWorkingTree,
[switch]$SkipDatabaseInit,
[switch]$PackageOnly,
[switch]$RequireSystemd,
[string]$PackageDirectory = "",
[string]$ServerInfoJsonPath = "",
[string]$SshKeyPath = "",
[string]$KnownHostsPath = "",
[string]$DatabaseTargetName = "mysql02",
[int]$DatabasePort = 3306,
[string]$DatabaseName = "jayucloud_salon",
@@ -324,6 +330,31 @@ function Get-ServersFromWorkbook {
}
}
function Get-ServersFromJson {
param([string]$Path)
if (-not (Test-Path -LiteralPath $Path)) {
throw "服务器 JSON 清单不存在:$Path"
}
$items = @(Get-Content -LiteralPath $Path -Raw -Encoding UTF8 | ConvertFrom-Json)
foreach ($item in $items) {
if (-not $item.Name -or -not $item.Host) {
throw "服务器 JSON 清单缺少 Name 或 Host:$Path"
}
[pscustomobject]@{
Name = [string]$item.Name
Host = [string]$item.Host
Port = if ($item.Port) { [int]$item.Port } else { 22 }
User = if ($item.User) { [string]$item.User } else { "root" }
Password = ""
Purpose = if ($item.Purpose) { [string]$item.Purpose } else { "" }
Note = if ($item.Note) { [string]$item.Note } else { "" }
Environment = if ($item.Environment) { [string]$item.Environment } else { "prod" }
}
}
}
function Resolve-ServerInfoPath {
param([string]$ExplicitPath)
@@ -374,7 +405,15 @@ function Invoke-RemoteShell {
[switch]$Sensitive
)
if ($UsePuttyPassword) {
if ($SshKeyPath) {
$sshArgs = @("-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-i", $SshKeyPath)
if ($KnownHostsPath) {
$sshArgs += @("-o", "UserKnownHostsFile=$KnownHostsPath")
}
$sshArgs += @("-p", [string]$Server.Port, (Get-SshTarget $Server), $Command)
Invoke-Native "ssh" $sshArgs -Sensitive:$Sensitive -RemoteMutation
}
elseif ($UsePuttyPassword) {
$plink = Get-Command plink -ErrorAction SilentlyContinue
if ((-not $DryRun) -and (-not $plink)) {
throw "找不到 PuTTY plink,请安装 PuTTY 工具或改用 SSH key。"
@@ -399,7 +438,15 @@ function Copy-FileToRemote {
throw "本地上传文件不存在:$LocalPath"
}
if ($UsePuttyPassword) {
if ($SshKeyPath) {
$scpArgs = @("-o", "BatchMode=yes", "-o", "StrictHostKeyChecking=yes", "-i", $SshKeyPath)
if ($KnownHostsPath) {
$scpArgs += @("-o", "UserKnownHostsFile=$KnownHostsPath")
}
$scpArgs += @("-P", [string]$Server.Port, $LocalPath, "$(Get-SshTarget $Server):$RemotePath")
Invoke-Native "scp" $scpArgs -Sensitive:$Sensitive -RemoteMutation
}
elseif ($UsePuttyPassword) {
$pscp = Get-Command pscp -ErrorAction SilentlyContinue
if ((-not $DryRun) -and (-not $pscp)) {
throw "找不到 PuTTY pscp,请安装 PuTTY 工具或改用 SSH key。"
@@ -569,6 +616,10 @@ RELEASE="`$APP_DIR/releases/`$RELEASE_NAME"
BACKUP_NAME="app-$releaseTimestamp.jar"
mkdir -p "`$APP_DIR/releases" "`$APP_DIR/backups" "`$APP_DIR/logs"
test -f "`$RELEASE"
if [ "`$RequireSystemd" = "True" ] && ! systemctl list-unit-files | grep -q "^`$SERVICE.service"; then
echo "Required systemd unit missing: `$SERVICE"
exit 44
fi
if command -v systemctl >/dev/null 2>&1 && systemctl list-unit-files | grep -q "^`$SERVICE.service"; then
systemctl stop "`$SERVICE" || true
fi
@@ -776,9 +827,66 @@ function Invoke-PublicSmokeTests {
}
}
if ($PackageOnly) {
Write-Step "检查工作区"
$gitState = Test-GitState
Write-Step "构建发布包"
$package = Build-ReleasePackage $gitState
$packageManifest = [pscustomobject]@{
releaseTimestamp = $releaseTimestamp
git = $package.Git
artifacts = $package.Hashes
parameters = [pscustomobject]@{
databaseName = $DatabaseName
webRemoteDir = $WebRemoteDir
publicBaseUrl = $PublicBaseUrl
nacosNamespace = "jayucloud-salon"
packageOnly = $true
}
}
$packageManifest | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath $manifestPath -Encoding UTF8
Write-Host "发布包已生成:$($package.Directory)"
exit 0
}
function Get-ExistingReleasePackage {
param([string]$Path)
if (-not (Test-Path -LiteralPath $Path)) {
throw "已下载发布包目录不存在:$Path"
}
$manifestFile = Get-ChildItem -LiteralPath $Path -Filter "manifest.json" -File -Recurse | Select-Object -First 1
if (-not $manifestFile) {
throw "发布包目录中缺少 manifest.json:$Path"
}
$releaseDir = $manifestFile.Directory.FullName
$coreArtifact = Join-Path $releaseDir "backend\JC-Salon-Core.jar"
$gatewayArtifact = Join-Path $releaseDir "backend\JC-Salon-Gateway.jar"
$frontendArtifact = Join-Path $releaseDir "frontend\jc-salon-frontend-dist.tar.gz"
$sqlArtifact = Join-Path $releaseDir "sql\jayucloud-salon.sql"
foreach ($requiredPath in @($coreArtifact, $gatewayArtifact, $frontendArtifact, $sqlArtifact)) {
if (-not (Test-Path -LiteralPath $requiredPath)) {
throw "发布包缺少文件:$requiredPath"
}
}
$hashes = foreach ($artifact in @($coreArtifact, $gatewayArtifact, $frontendArtifact, $sqlArtifact)) {
$hash = Get-FileHash -LiteralPath $artifact -Algorithm SHA256
[pscustomobject]@{ Path = $artifact.Replace($releaseDir, "").TrimStart("\", "/"); Sha256 = $hash.Hash }
}
[pscustomobject]@{
Directory = $releaseDir
CoreJar = $coreArtifact
GatewayJar = $gatewayArtifact
FrontendArchive = $frontendArtifact
InitSql = $sqlArtifact
Hashes = @($hashes)
Git = $null
}
}
Write-Step "读取服务器清单"
$resolvedServerInfoPath = Resolve-ServerInfoPath $ServerInfoPath
$servers = @(Get-ServersFromWorkbook $resolvedServerInfoPath)
$resolvedServerInfoPath = if ($ServerInfoJsonPath) { (Resolve-Path -LiteralPath $ServerInfoJsonPath).Path } else { Resolve-ServerInfoPath $ServerInfoPath }
$servers = if ($ServerInfoJsonPath) { @(Get-ServersFromJson $resolvedServerInfoPath) } else { @(Get-ServersFromWorkbook $resolvedServerInfoPath) }
$coreServers = @($servers | Where-Object { $_.Name -match $CorePattern })
$gatewayServers = @($servers | Where-Object { $_.Name -match $GatewayPattern })
$webServers = @(if ($WebTargetName) {
@@ -816,8 +924,8 @@ if ($databaseServer) {
Write-Step "检查工作区"
$gitState = Test-GitState
Write-Step "构建发布包"
$package = Build-ReleasePackage $gitState
Write-Step "准备发布包"
$package = if ($PackageDirectory) { Get-ExistingReleasePackage $PackageDirectory } else { Build-ReleasePackage $gitState }
Write-ReleaseManifest $package $coreServers $gatewayServers $webServers[0] $databaseServer $resolvedServerInfoPath
if (-not $Deploy) {