38 lines
2.7 KiB
JavaScript
38 lines
2.7 KiB
JavaScript
import { createHash } from 'node:crypto';
|
|
import { readFile, readdir } from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { envName, latestSuccess, recordsRoot, writeJson } from './release-store.mjs';
|
|
|
|
const environment = envName(process.env.RELEASE_ENVIRONMENT);
|
|
const commitSha = process.env.RELEASE_SHA;
|
|
const nacos = await latestSuccess(environment, 'nacos');
|
|
const packageDir = process.env.PACKAGE_DIRECTORY;
|
|
if (!commitSha || nacos.commitSha !== commitSha || !packageDir) throw new Error('部署记录参数不完整');
|
|
if (nacos.status !== 'success' || !nacos.files?.length || !nacos.activeSnapshotId) throw new Error('Nacos 发布已回退或发布记录不完整');
|
|
const manifestPath = await findFile(packageDir, 'manifest.json');
|
|
const manifest = JSON.parse(await readFile(manifestPath, 'utf8'));
|
|
const artifactPaths = {};
|
|
for (const [name, fileName] of Object.entries({ core: 'JC-Salon-Core.jar', gateway: 'JC-Salon-Gateway.jar', frontend: 'jc-salon-frontend-dist.tar.gz' })) {
|
|
const artifactPath = await findFile(packageDir, fileName);
|
|
const sha256 = createHash('sha256').update(await readFile(artifactPath)).digest('hex');
|
|
const expected = manifest.artifacts?.find((item) => item.Path?.endsWith(fileName))?.Sha256?.toLowerCase();
|
|
if (!expected || expected !== sha256) throw new Error(`发布包摘要不匹配:${fileName}`);
|
|
artifactPaths[name] = { path: artifactPath, sha256 };
|
|
}
|
|
const previous = await optional(join(recordsRoot(environment), 'latest-success-deploy.json'));
|
|
const releaseId = `${environment}-${commitSha.slice(0, 12)}-${process.env.GITHUB_RUN_ID || 'manual'}`;
|
|
const record = { environment, status: 'success', commitSha, releaseId, nacosReleaseId: nacos.releaseId, snapshotId: nacos.snapshotId, activeSnapshotId: nacos.activeSnapshotId, packageDir, manifestPath, artifacts: artifactPaths, previousReleaseId: previous?.releaseId || null, runId: process.env.GITHUB_RUN_ID || 'manual', deployedAt: new Date().toISOString() };
|
|
await writeJson(join(recordsRoot(environment), 'deploy', `${releaseId}.json`), record);
|
|
await writeJson(join(recordsRoot(environment), 'latest-success-deploy.json'), record);
|
|
console.log(JSON.stringify(record));
|
|
async function optional(path) { try { return JSON.parse(await readFile(path, 'utf8')); } catch { return null; } }
|
|
async function findFile(root, name) {
|
|
for (const entry of await readdir(root, { withFileTypes: true })) {
|
|
const path = join(root, entry.name);
|
|
if (entry.isDirectory()) { const nested = await optionalFind(path, name); if (nested) return nested; }
|
|
else if (entry.name === name) return path;
|
|
}
|
|
throw new Error(`发布包缺少 ${name}`);
|
|
}
|
|
async function optionalFind(root, name) { try { return await findFile(root, name); } catch { return null; } }
|