实现双中心Nacos顺序发布流水线

This commit is contained in:
2026-08-26 09:55:28 +08:00
parent 0d30999768
commit 65193c6b6c
5 changed files with 68 additions and 40 deletions
+41 -27
View File
@@ -3,34 +3,39 @@ import { readFile, readdir } from 'node:fs/promises';
import { join, relative, sep } from 'node:path';
const repositoryRoot = process.cwd();
const environment = requiredNacosEnvironment('NACOS_ENVIRONMENT');
const configRoot = join(repositoryRoot, 'nacos', 'namespaces', environment);
const nacosUrls = requiredEnvironment(`NACOS_${environment.toUpperCase()}_URLS`).split(',').map((value) => value.trim()).filter(Boolean);
const username = requiredEnvironment(`NACOS_${environment.toUpperCase()}_USERNAME`);
const password = requiredEnvironment(`NACOS_${environment.toUpperCase()}_PASSWORD`);
const target = requiredNacosTarget('NACOS_ENVIRONMENT');
const configRoot = join(repositoryRoot, 'nacos', 'namespaces', 'prod');
const group = 'DEFAULT_GROUP';
const verificationNodeCount = environment === 'prod' ? 2 : 1;
const configFiles = await collectYamlFiles(target.environment === 'sit' ? join(repositoryRoot, 'nacos', 'namespaces', 'sit') : configRoot);
if (nacosUrls.length < verificationNodeCount) {
throw new Error(`${environment} 环境至少需要 ${verificationNodeCount} 个 Nacos 地址用于发布校验`);
}
const configFiles = await collectYamlFiles(configRoot);
if (configFiles.length === 0) {
throw new Error(`nacos/namespaces/${environment} 下没有可发布的 YAML 配置`);
throw new Error(`nacos/namespaces/${target.environment} 下没有可发布的 YAML 配置`);
}
const token = await login(nacosUrls[0], username, password);
for (const filePath of configFiles) {
const relativePath = relative(configRoot, filePath).split(sep);
if (relativePath.length !== 3 || relativePath[1] !== group) {
throw new Error(`配置路径必须是 <namespace>/${group}/<dataId>.yaml:${relativePath.join('/')}`);
for (const destination of target.destinations) {
await publishDestination(destination, configFiles);
}
async function publishDestination(destination, files) {
const nacosUrls = requiredEnvironment(`${destination.prefix}_URLS`).split(',').map((value) => value.trim()).filter(Boolean);
if (nacosUrls.length < destination.verificationNodeCount) {
throw new Error(`${destination.name} 至少需要 ${destination.verificationNodeCount} 个 Nacos 地址用于发布校验`);
}
const username = requiredEnvironment(`${destination.prefix}_USERNAME`);
const password = requiredEnvironment(`${destination.prefix}_PASSWORD`);
const token = await login(nacosUrls[0], username, password);
for (const filePath of files) {
const root = join(repositoryRoot, 'nacos', 'namespaces', destination.environment);
const relativePath = relative(root, filePath).split(sep);
if (relativePath.length !== 3 || relativePath[1] !== group) {
throw new Error(`配置路径必须是 <namespace>/${group}/<dataId>.yaml:${relativePath.join('/')}`);
}
const [namespace, , dataId] = relativePath;
const content = await readFile(filePath, 'utf8');
await publish(nacosUrls[0], token, namespace, group, dataId, content);
await verify(nacosUrls.slice(0, destination.verificationNodeCount), token, namespace, group, dataId, content);
console.log(`已发布并校验 ${destination.name}/${namespace}/${group}/${dataId}`);
}
const [namespace, , dataId] = relativePath;
const content = await readFile(filePath, 'utf8');
await publish(nacosUrls[0], token, namespace, group, dataId, content);
await verify(nacosUrls.slice(0, verificationNodeCount), token, namespace, group, dataId, content);
console.log(`已发布并校验 ${environment}/${namespace}/${group}/${dataId}`);
}
function requiredEnvironment(name) {
@@ -41,12 +46,21 @@ function requiredEnvironment(name) {
return value;
}
function requiredNacosEnvironment(name) {
const environment = requiredEnvironment(name);
if (environment !== 'prod' && environment !== 'sit') {
throw new Error(`不支持的 Nacos 发布环境:${environment};只允许 prod 或 sit`);
function requiredNacosTarget(name) {
const value = requiredEnvironment(name);
const destinations = {
sit: [{ name: 'sit', environment: 'sit', prefix: 'NACOS_SIT', verificationNodeCount: 1 }],
'prod-center1': [{ name: 'prod-center1', environment: 'prod', prefix: 'NACOS_PROD', verificationNodeCount: 2 }],
'prod-center2': [{ name: 'prod-center2', environment: 'prod', prefix: 'NACOS_DR', verificationNodeCount: 1 }],
'prod-both': [
{ name: 'prod-center1', environment: 'prod', prefix: 'NACOS_PROD', verificationNodeCount: 2 },
{ name: 'prod-center2', environment: 'prod', prefix: 'NACOS_DR', verificationNodeCount: 1 },
],
}[value];
if (!destinations) {
throw new Error(`不支持的 Nacos 发布目标:${value}`);
}
return environment;
return { environment: destinations[0].environment, destinations };
}
async function collectYamlFiles(directory) {