提交
This commit is contained in:
@@ -5,6 +5,29 @@ import test from 'node:test'
|
||||
const read = async (path) => (await readFile(new URL(`../${path}`, import.meta.url), 'utf8'))
|
||||
.replaceAll('\r\n', '\n')
|
||||
|
||||
test('local, development and test Nacos publishing remain environment-isolated', async () => {
|
||||
const publisher = await read('scripts/publish-nacos-config.ps1')
|
||||
const local = [
|
||||
await read('config/nacos/local/gateway-overseas.yml'),
|
||||
await read('config/nacos/local/lastleg-service.yml'),
|
||||
await read('config/nacos/local/warehouse-overseas-service.yml'),
|
||||
].join('\n')
|
||||
const remote = [
|
||||
await read('config/nacos/overseas/gateway-overseas.yml'),
|
||||
await read('config/nacos/overseas/lastleg-service.yml'),
|
||||
await read('config/nacos/overseas/warehouse-overseas-service.yml'),
|
||||
].join('\n')
|
||||
|
||||
assert.match(publisher, /ValidateSet\('local', 'dev', 'development', 'test', 'production'\)/)
|
||||
assert.match(publisher, /必须显式指定非 public 的 Nacos namespace/)
|
||||
assert.match(local, /127\.0\.0\.1:9100/)
|
||||
assert.match(local, /127\.0\.0\.1:9303/)
|
||||
assert.doesNotMatch(remote, /127\.0\.0\.1|password:\s*123456/)
|
||||
assert.match(remote, /http:\/\/lastleg-service:9303/)
|
||||
assert.match(remote, /\$\{LASTLEG_DB_URL\}/)
|
||||
assert.match(remote, /\$\{WAREHOUSE_DB_URL\}/)
|
||||
})
|
||||
|
||||
test('Jenkins pipelines recognize all overseas deployable modules', async () => {
|
||||
const controller = await read('Jenkinsfile')
|
||||
const service = await read('Jenkinsfile.service')
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
param(
|
||||
[ValidateSet('local', 'dev', 'development', 'test', 'production')]
|
||||
[string]$Environment,
|
||||
[ValidateSet('cn', 'overseas')]
|
||||
[string]$Region = 'cn',
|
||||
[string]$ServerAddr = '127.0.0.1:8848',
|
||||
[string]$Group = 'BAIYUE',
|
||||
[string]$Namespace
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$projectRoot = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
|
||||
|
||||
if ($Environment -eq 'local') {
|
||||
if ($Namespace -and $Namespace -ne 'public') {
|
||||
throw 'local 环境只能发布到 public namespace'
|
||||
}
|
||||
$Namespace = 'public'
|
||||
$configProfile = 'local'
|
||||
} else {
|
||||
if (-not $Namespace -or $Namespace -eq 'public') {
|
||||
throw "$Environment/$Region 必须显式指定非 public 的 Nacos namespace"
|
||||
}
|
||||
$configProfile = $Region
|
||||
}
|
||||
|
||||
$configRoot = (Resolve-Path (Join-Path $projectRoot "config\nacos\$configProfile")).Path
|
||||
$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 Nacos configuration found: $configRoot"
|
||||
}
|
||||
|
||||
$published = 0
|
||||
foreach ($file in $files) {
|
||||
$resolvedFile = (Resolve-Path -LiteralPath $file.FullName).Path
|
||||
if (-not $resolvedFile.StartsWith($configRoot, [System.StringComparison]::OrdinalIgnoreCase)) {
|
||||
throw "Unexpected config path: $resolvedFile"
|
||||
}
|
||||
$body = @{
|
||||
dataId = $file.Name
|
||||
group = $Group
|
||||
content = Get-Content -LiteralPath $resolvedFile -Raw -Encoding UTF8
|
||||
type = 'yaml'
|
||||
}
|
||||
if ($Namespace -ne 'public') {
|
||||
$body.tenant = $Namespace
|
||||
}
|
||||
$pairs = foreach ($entry in $body.GetEnumerator()) {
|
||||
[uri]::EscapeDataString([string]$entry.Key) + '=' + [uri]::EscapeDataString([string]$entry.Value)
|
||||
}
|
||||
$result = Invoke-RestMethod -Method Post -Uri $publishUrl `
|
||||
-ContentType 'application/x-www-form-urlencoded; charset=UTF-8' `
|
||||
-Body ($pairs -join '&') -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 $Environment/$Region Nacos configuration: $published items."
|
||||
@@ -0,0 +1,74 @@
|
||||
param(
|
||||
[ValidateSet('local', 'dev', 'development', 'test')]
|
||||
[string]$Environment,
|
||||
[ValidateSet('cn', 'overseas')]
|
||||
[string]$Region = 'cn',
|
||||
[string]$NacosServerAddr,
|
||||
[string]$NacosNamespace,
|
||||
[string]$NacosUsername,
|
||||
[string]$NacosPassword
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$target = if ($Environment -eq 'development') { 'dev' } else { $Environment }
|
||||
|
||||
$defaults = @{
|
||||
local = @{ Server = '127.0.0.1:8848'; CnNamespace = 'public' }
|
||||
dev = @{ Server = '8.163.49.32:8848'; CnNamespace = $env:DEV_NACOS_NAMESPACE }
|
||||
test = @{ Server = '8.163.30.128:8848'; CnNamespace = 'd99d7998-5bb0-4ffe-a2a0-1a564ce14463' }
|
||||
}
|
||||
$selected = $defaults[$target]
|
||||
$serverAddr = if ($NacosServerAddr) { $NacosServerAddr } else { $selected.Server }
|
||||
$namespace = if ($NacosNamespace) {
|
||||
$NacosNamespace
|
||||
} elseif ($target -eq 'local') {
|
||||
'public'
|
||||
} elseif ($Region -eq 'overseas') {
|
||||
$regionNamespace = [Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_OVERSEAS_NACOS_NAMESPACE")
|
||||
if ($regionNamespace) {
|
||||
$regionNamespace
|
||||
} else {
|
||||
[Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_NAMESPACE")
|
||||
}
|
||||
} else {
|
||||
$selected.CnNamespace
|
||||
}
|
||||
$username = if ($NacosUsername) {
|
||||
$NacosUsername
|
||||
} elseif ([Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_USERNAME")) {
|
||||
[Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_USERNAME")
|
||||
} else {
|
||||
'nacos'
|
||||
}
|
||||
$password = if ($NacosPassword) {
|
||||
$NacosPassword
|
||||
} elseif ([Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_PASSWORD")) {
|
||||
[Environment]::GetEnvironmentVariable("$($target.ToUpperInvariant())_NACOS_PASSWORD")
|
||||
} elseif ($target -eq 'local') {
|
||||
'nacos'
|
||||
} else {
|
||||
$null
|
||||
}
|
||||
|
||||
if (-not $namespace) {
|
||||
throw "$target/$Region 必须通过 -NacosNamespace 或对应的 *_NACOS_NAMESPACE 环境变量指定 namespace"
|
||||
}
|
||||
if (-not $password) {
|
||||
throw "$target 必须通过 -NacosPassword 或 $($target.ToUpperInvariant())_NACOS_PASSWORD 提供 Nacos 密码"
|
||||
}
|
||||
|
||||
$env:NACOS_SERVER_ADDR = $serverAddr
|
||||
$env:NACOS_NAMESPACE = $namespace
|
||||
$env:NACOS_GROUP = 'BAIYUE'
|
||||
$env:NACOS_USERNAME = $username
|
||||
$env:NACOS_PASSWORD = $password
|
||||
$env:SPRING_CLOUD_NACOS_CONFIG_NAMESPACE = $namespace
|
||||
$env:SPRING_CLOUD_NACOS_DISCOVERY_NAMESPACE = $namespace
|
||||
$env:SPRING_PROFILES_ACTIVE = if ($Region -eq 'overseas') { 'overseas' } elseif ($target -eq 'local') { 'dev' } else { 'cn' }
|
||||
$env:BAIYUE_RUNTIME_ENVIRONMENT = $target
|
||||
$env:BAIYUE_RUNTIME_REGION = $Region
|
||||
|
||||
Write-Host "Runtime environment: $target / $Region"
|
||||
Write-Host "Nacos: $serverAddr; namespace: $namespace; group: BAIYUE"
|
||||
Write-Host "Spring profiles: $env:SPRING_PROFILES_ACTIVE"
|
||||
Write-Host 'Nacos password is loaded into the current process and is not printed.'
|
||||
Reference in New Issue
Block a user