function Read-TencentProdEnv { param([string]$Root) $env = @{ H5_IP = '119.45.34.106' API_IP = '146.56.223.139' API1_IP = '118.195.186.82' H5_PRIVATE_IP = '10.206.16.10' API_PRIVATE_IP = '10.206.16.9' } $envFile = Join-Path $Root '.env.tencent' if (-not (Test-Path $envFile)) { return $env } Get-Content $envFile | ForEach-Object { $line = $_.Trim() if (-not $line -or $line.StartsWith('#')) { return } $parts = $line -split '=', 2 if ($parts.Count -lt 2) { return } $key = $parts[0].Trim() $value = $parts[1].Trim() switch ($key) { 'TENCENT_PROD_H5_IP' { $env.H5_IP = $value } 'TENCENT_PROD_API_IP' { $env.API_IP = $value } 'TENCENT_PROD_API1_IP' { $env.API1_IP = $value } 'TENCENT_PROD_H5_PRIVATE_IP' { $env.H5_PRIVATE_IP = $value } 'TENCENT_PROD_API_PRIVATE_IP' { $env.API_PRIVATE_IP = $value } } } return $env } function Write-RenderedFrontendNginx { param( [string]$Root, [string]$ApiPrivateIp ) $template = Join-Path $Root 'deploy\nginx.canteen.conf' if (-not (Test-Path $template)) { Write-Error "Missing nginx template: $template" } $out = Join-Path $env:TEMP ("nginx.canteen.{0}.conf" -f ([guid]::NewGuid().ToString('N').Substring(0, 8))) $content = Get-Content $template -Raw -Encoding UTF8 $content = $content -replace '__API_INTERNAL_HOST__', $ApiPrivateIp $utf8NoBom = New-Object System.Text.UTF8Encoding $false [System.IO.File]::WriteAllText($out, $content, $utf8NoBom) return $out } # 仅供 deploy-prod-llm-env.ps1;常规后端 tar 不再打包 LLM 片段 function Write-LlmEnvSnippet { param( [string]$Root, [string]$OutFile ) $envPath = Join-Path $Root 'server\.env' if (-not (Test-Path $envPath)) { Write-Error "Missing $envPath" } $map = @{} Get-Content $envPath | ForEach-Object { $line = $_.Trim() if (-not $line -or $line.StartsWith('#')) { return } $parts = $line -split '=', 2 if ($parts.Count -lt 2) { return } $map[$parts[0].Trim()] = $parts[1].Trim() } $keys = @( 'LLM_PROVIDER', 'LLM_BASE_URL', 'LLM_API_KEY', 'LLM_MODEL', 'LLM_VISION_MODEL', 'SPEECH_API_KEY' ) $lines = New-Object System.Collections.Generic.List[string] foreach ($key in $keys) { $value = '' if ($map.ContainsKey($key)) { $value = [string]$map[$key] } if (-not $value) { continue } $lines.Add("$key=$value") | Out-Null } if ($lines.Count -eq 0) { Write-Error "server/.env 没有可同步的 LLM_* / SPEECH_API_KEY" } $utf8NoBom = New-Object System.Text.UTF8Encoding $false [System.IO.File]::WriteAllText($OutFile, (($lines -join "`n") + "`n"), $utf8NoBom) return $lines.Count } function New-BackendDeployTar { param( [string]$Root, [string]$OutFile ) # 迁移文件打包进 migrations/ 子目录,与 prod-apply-backend.sh 的读取路径一致。 # 历史事故:原先 `-C migrations .` 把 *.sql 留在包根,部署脚本却查 $STAGING/migrations, # `cp … || true` 吞掉错误 → 迁移静默不执行但部署显示成功 # (2026-09-14 线上看板报 column sk.low_stock_alert does not exist)。 # 部署脚本现已兼容两种结构;打包侧统一为 migrations/ 以消除歧义。 # 见 docs/design/20260914-deploy-migration-path-fix.md $migrationsDir = Join-Path $Root 'migrations' if (-not (Test-Path $migrationsDir)) { throw "Missing migrations directory: $migrationsDir" } $sqlCount = @(Get-ChildItem -Path $migrationsDir -Filter *.sql -File).Count if ($sqlCount -le 0) { throw "No .sql under $migrationsDir — refusing to build a deploy tar without migrations" } if (Test-Path $OutFile) { Remove-Item $OutFile -Force } tar -czf $OutFile ` -C (Join-Path $Root 'server') dist package.json data ` -C (Join-Path $Root '.') migrations ` -C (Join-Path $Root 'scripts') prod-apply-backend.sh if ($LASTEXITCODE -ne 0) { throw 'Failed to create backend-deploy.tar.gz' } }