实现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) {
@@ -0,0 +1,36 @@
# Salon 生产 Gitea 流水线配置说明
## 工作流
- `.gitea/workflows/salon-prod-prepare.yml`:仅在 `sit` 来源的 PR 合并到 `prod` 后自动运行,构建后端、前端和 SQL 归档并上传 Artifact,不修改生产环境。
- `.gitea/workflows/salon-prod-deploy.yml`:仅支持手工运行,填写准备工作流运行 ID 和合并提交 SHA 后,下载对应 Artifact 并执行生产发布。
## prod 分支保护
在 Gitea 仓库的 Branches/Protected branches 中为 `prod` 配置:
- 禁止直接 push;
- 仅允许 Pull Request 合并;
- 合并来源分支限制为 `sit`;
- 至少一名审批人,并要求状态检查 `Salon 生产发布准备 / prepare` 成功。
## Runner
Runner 标签为 `jayucloud`,需要预装或可用:Node.js 20、JDK 17、Maven、npm、tar、PowerShell 7、OpenSSH client。人工发布阶段还必须能访问生产 Nacos、6 台 Salon 后端服务器和阿里云 NGINX 主机。
## Secrets
仓库 Actions Secrets 需要配置:
- `SALON_PROD_SSH_PRIVATE_KEY`:仅用于生产发布的 SSH 私钥;
- `SALON_PROD_KNOWN_HOSTS`:6 台后端和阿里云 NGINX 的固定主机指纹;
- `SALON_PROD_WEB_HOST`、`SALON_PROD_WEB_PORT`、`SALON_PROD_WEB_USER`、`SALON_PROD_WEB_DIR`:阿里云 NGINX 实际目标信息;
- `GITHUB_TOKEN`:用于下载准备工作流 Artifact(由 Gitea 自动提供)。
SSH 私钥必须对应后端节点和 Web 节点上的部署公钥,禁止把 root 密码写入工作流或 Secrets。生产 Nacos 认证和应用启动所需运行时变量应通过受控的服务环境文件或 Gitea Secrets 注入,不能写入仓库。
## 发布边界
自动准备阶段不连接生产 SSH、不发布 Nacos、不重启服务、不执行 SQL。人工发布阶段跳过数据库初始化;SQL 仅作为 Artifact 附件,由 DBA 按版本化迁移规则人工执行并记录。
首次人工发布前,需在 6 台后端完成 `jc-salon-core`、`jc-salon-gateway` 服务单元及运行时环境文件初始化;人工发布工作流使用 `-RequireSystemd`,缺少服务单元会直接失败,不会退回后台进程模式。还需确认阿里云 NGINX 已使用 `/opt/jc-salon-frontend/current` 或等价目录作为 Salon 站点根目录。