完善生产与测试环境Nacos配置
This commit is contained in:
@@ -3,15 +3,21 @@ import { readFile, readdir } from 'node:fs/promises';
|
||||
import { join, relative, sep } from 'node:path';
|
||||
|
||||
const repositoryRoot = process.cwd();
|
||||
const configRoot = join(repositoryRoot, 'nacos', 'namespaces', 'prod');
|
||||
const nacosUrls = requiredEnvironment('NACOS_URLS').split(',').map((value) => value.trim()).filter(Boolean);
|
||||
const username = requiredEnvironment('NACOS_USERNAME');
|
||||
const password = requiredEnvironment('NACOS_PASSWORD');
|
||||
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 group = 'DEFAULT_GROUP';
|
||||
const verificationNodeCount = environment === 'prod' ? 2 : 1;
|
||||
|
||||
if (nacosUrls.length < verificationNodeCount) {
|
||||
throw new Error(`${environment} 环境至少需要 ${verificationNodeCount} 个 Nacos 地址用于发布校验`);
|
||||
}
|
||||
|
||||
const configFiles = await collectYamlFiles(configRoot);
|
||||
if (configFiles.length === 0) {
|
||||
throw new Error('nacos/namespaces/prod 下没有可发布的 YAML 配置');
|
||||
throw new Error(`nacos/namespaces/${environment} 下没有可发布的 YAML 配置`);
|
||||
}
|
||||
|
||||
const token = await login(nacosUrls[0], username, password);
|
||||
@@ -23,8 +29,8 @@ for (const filePath of configFiles) {
|
||||
const [namespace, , dataId] = relativePath;
|
||||
const content = await readFile(filePath, 'utf8');
|
||||
await publish(nacosUrls[0], token, namespace, group, dataId, content);
|
||||
await verify(nacosUrls.slice(0, 2), token, namespace, group, dataId, content);
|
||||
console.log(`已发布并校验 ${namespace}/${group}/${dataId}`);
|
||||
await verify(nacosUrls.slice(0, verificationNodeCount), token, namespace, group, dataId, content);
|
||||
console.log(`已发布并校验 ${environment}/${namespace}/${group}/${dataId}`);
|
||||
}
|
||||
|
||||
function requiredEnvironment(name) {
|
||||
@@ -35,6 +41,14 @@ function requiredEnvironment(name) {
|
||||
return value;
|
||||
}
|
||||
|
||||
function requiredNacosEnvironment(name) {
|
||||
const environment = requiredEnvironment(name);
|
||||
if (environment !== 'prod' && environment !== 'sit') {
|
||||
throw new Error(`不支持的 Nacos 发布环境:${environment};只允许 prod 或 sit`);
|
||||
}
|
||||
return environment;
|
||||
}
|
||||
|
||||
async function collectYamlFiles(directory) {
|
||||
const entries = await readdir(directory, { withFileTypes: true });
|
||||
const files = [];
|
||||
|
||||
Reference in New Issue
Block a user