Files
baiyue-firstleg/scripts/publish-local-nacos-config.ps1
T
2026-08-01 12:02:01 +08:00

50 lines
1.7 KiB
PowerShell

param(
[string]$ServerAddr = '127.0.0.1:8848',
[string]$Group = 'BAIYUE',
[string]$Namespace = 'public'
)
$ErrorActionPreference = 'Stop'
$projectRoot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
$configRoot = Join-Path $projectRoot 'config\nacos\local'
$healthUrl = "http://$ServerAddr/nacos/v1/console/health/readiness"
$publishUrl = "http://$ServerAddr/nacos/v1/cs/configs"
$health = Invoke-WebRequest -UseBasicParsing -Uri $healthUrl -TimeoutSec 5
if ($health.StatusCode -ne 200 -or $health.Content.Trim() -ne 'OK') {
throw "Nacos not ready: $healthUrl"
}
$files = Get-ChildItem -LiteralPath $configRoot -File -Filter '*.yml' | Sort-Object Name
if ($files.Count -eq 0) {
throw "No local Nacos configuration found: $configRoot"
}
$published = 0
foreach ($file in $files) {
$body = @{
dataId = $file.Name
group = $Group
content = Get-Content -LiteralPath $file.FullName -Raw -Encoding UTF8
type = 'yaml'
}
if ($Namespace -and $Namespace -ne 'public') {
$body.tenant = $Namespace
}
$pairs = foreach ($entry in $body.GetEnumerator()) {
[uri]::EscapeDataString([string]$entry.Key) + '=' + [uri]::EscapeDataString([string]$entry.Value)
}
$form = $pairs -join '&'
$result = Invoke-RestMethod -Method Post -Uri $publishUrl `
-ContentType 'application/x-www-form-urlencoded; charset=UTF-8' `
-Body $form -TimeoutSec 10
if ([string]$result -ne 'true') {
throw "Publish failed: $($file.Name), Nacos response: $result"
}
$published++
Write-Host "Published $($file.Name) -> $Namespace / $Group"
}
Write-Host "Published local Nacos configuration: $published items."