实现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
+95
View File
@@ -0,0 +1,95 @@
name: Salon 生产发布
on:
workflow_dispatch:
inputs:
prepare_run_id:
description: 自动准备工作流的运行 ID
required: true
type: string
release_sha:
description: prod 合并提交 SHA
required: true
type: string
jobs:
deploy:
runs-on: jayucloud
env:
WEB_TARGET_HOST: ${{ secrets.SALON_PROD_WEB_HOST }}
WEB_TARGET_PORT: ${{ secrets.SALON_PROD_WEB_PORT }}
WEB_TARGET_USER: ${{ secrets.SALON_PROD_WEB_USER }}
WEB_REMOTE_DIR: ${{ secrets.SALON_PROD_WEB_DIR }}
steps:
- name: 检出发布脚本
uses: actions/checkout@v4
with:
ref: ${{ inputs.release_sha }}
- name: 准备 Node.js 20
uses: actions/setup-node@v4
with:
node-version: '20'
- name: 发布并校验 Salon 生产 Nacos
shell: bash
env:
NACOS_ENVIRONMENT: prod-both
NACOS_PROD_URLS: ${{ secrets.NACOS_PROD_URLS }}
NACOS_PROD_USERNAME: ${{ secrets.NACOS_PROD_USERNAME }}
NACOS_PROD_PASSWORD: ${{ secrets.NACOS_PROD_PASSWORD }}
NACOS_DR_URLS: ${{ secrets.NACOS_DR_URLS }}
NACOS_DR_USERNAME: ${{ secrets.NACOS_DR_USERNAME }}
NACOS_DR_PASSWORD: ${{ secrets.NACOS_DR_PASSWORD }}
run: node .gitea/scripts/publish-nacos.mjs
- name: 下载已审核发布包
uses: actions/download-artifact@v4
with:
name: salon-prod-${{ inputs.release_sha }}
path: artifacts/release
repository: ${{ github.repository }}
run-id: ${{ inputs.prepare_run_id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: 写入临时 SSH 配置和目标清单
shell: bash
env:
SALON_PROD_SSH_PRIVATE_KEY: ${{ secrets.SALON_PROD_SSH_PRIVATE_KEY }}
SALON_PROD_KNOWN_HOSTS: ${{ secrets.SALON_PROD_KNOWN_HOSTS }}
run: |
set -euo pipefail
test -n "$SALON_PROD_SSH_PRIVATE_KEY"
test -n "$SALON_PROD_KNOWN_HOSTS"
test -n "$WEB_TARGET_HOST"
test -n "$WEB_TARGET_PORT"
test -n "$WEB_TARGET_USER"
test -n "$WEB_REMOTE_DIR"
mkdir -p "$RUNNER_TEMP/salon-prod"
printf '%s\n' "$SALON_PROD_SSH_PRIVATE_KEY" > "$RUNNER_TEMP/salon-prod/id_ed25519"
printf '%s\n' "$SALON_PROD_KNOWN_HOSTS" > "$RUNNER_TEMP/salon-prod/known_hosts"
chmod 600 "$RUNNER_TEMP/salon-prod/id_ed25519" "$RUNNER_TEMP/salon-prod/known_hosts"
cat > "$RUNNER_TEMP/salon-prod/servers.json" <<JSON
[
{"Name":"core01-01","Host":"192.168.10.24","Port":22,"User":"root","Purpose":"Salon Core","Environment":"prod"},
{"Name":"core01-02","Host":"192.168.10.25","Port":22,"User":"root","Purpose":"Salon Core","Environment":"prod"},
{"Name":"core01-03","Host":"192.168.20.16","Port":22,"User":"root","Purpose":"Salon Core","Environment":"prod"},
{"Name":"gateway01-01","Host":"192.168.10.26","Port":22,"User":"root","Purpose":"Salon Gateway","Environment":"prod"},
{"Name":"gateway01-02","Host":"192.168.10.27","Port":22,"User":"root","Purpose":"Salon Gateway","Environment":"prod"},
{"Name":"gateway01-03","Host":"192.168.20.18","Port":22,"User":"root","Purpose":"Salon Gateway","Environment":"prod"},
{"Name":"salon-web","Host":"${WEB_TARGET_HOST}","Port":${WEB_TARGET_PORT},"User":"${WEB_TARGET_USER}","Purpose":"Salon NGINX","Environment":"prod"}
]
JSON
- name: 人工确认后执行生产发布
shell: pwsh
env:
SSH_KEY_PATH: ${{ runner.temp }}/salon-prod/id_ed25519
KNOWN_HOSTS_PATH: ${{ runner.temp }}/salon-prod/known_hosts
SERVER_INFO_JSON: ${{ runner.temp }}/salon-prod/servers.json
run: >-
./docs/scripts/deploy-jayucloud-salon-production.ps1
-Deploy
-SkipDatabaseInit
-RequireSystemd
-PackageDirectory "artifacts/release"
-ServerInfoJsonPath "$env:SERVER_INFO_JSON"
-SshKeyPath "$env:SSH_KEY_PATH"
-KnownHostsPath "$env:KNOWN_HOSTS_PATH"
-WebTargetName salon-web
-WebRemoteDir "$env:WEB_REMOTE_DIR"
+47
View File
@@ -0,0 +1,47 @@
name: Salon 生产发布准备
on:
pull_request:
types:
- closed
branches:
- prod
jobs:
prepare:
if: >-
github.event.pull_request.merged == true &&
github.event.pull_request.base.ref == 'prod' &&
github.event.pull_request.head.ref == 'sit'
runs-on: jayucloud
steps:
- name: 检出合并提交
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.merge_commit_sha }}
fetch-depth: 0
- name: 准备 Java 17
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: '17'
cache: maven
- name: 准备 Node.js 20
uses: actions/setup-node@v4
with:
node-version: '20'
cache: npm
cache-dependency-path: JC-Salon-Frontend/package-lock.json
- name: 构建 Salon 发布包
shell: pwsh
run: >-
./docs/scripts/deploy-jayucloud-salon-production.ps1
-PackageOnly
-SkipDatabaseInit
- name: 上传发布包
uses: actions/upload-artifact@v4
with:
name: salon-prod-${{ github.event.pull_request.merge_commit_sha }}
path: artifacts/release/*
if-no-files-found: error
retention-days: 30
@@ -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 站点根目录。