69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
/**
|
|
* 发版成功后写入 system_version。
|
|
* 须经 with-api-env.cjs 加载 DATABASE_URL;失败不阻断发版(exit 0)。
|
|
*/
|
|
const { execSync } = require('child_process');
|
|
const { resolve } = require('path');
|
|
|
|
const apiRoot = resolve(__dirname, '..');
|
|
const appRoot = process.env.APP_ROOT || resolve(apiRoot, '../..');
|
|
|
|
function git(cmd) {
|
|
try {
|
|
return execSync(cmd, { cwd: appRoot, encoding: 'utf8' }).trim();
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const commitId = git('git rev-parse HEAD');
|
|
if (!commitId) {
|
|
console.warn('[record-system-version] WARN: 无法读取 git HEAD,跳过');
|
|
return;
|
|
}
|
|
|
|
let commitMessage = git('git log -1 --pretty=%s') || '';
|
|
if (commitMessage.length > 512) {
|
|
commitMessage = commitMessage.slice(0, 512);
|
|
}
|
|
|
|
const gitTag = git('git describe --tags --exact-match') || null;
|
|
const branchRaw = git('git rev-parse --abbrev-ref HEAD');
|
|
const branch = branchRaw && branchRaw !== 'HEAD' ? branchRaw : null;
|
|
|
|
const trigger = (process.env.DEPLOY_TRIGGER || '').trim();
|
|
let deployedBy = 'manual';
|
|
if (trigger) {
|
|
if (trigger === 'manual' || trigger === 'admin') {
|
|
deployedBy = trigger;
|
|
} else {
|
|
deployedBy = 'webhook';
|
|
}
|
|
}
|
|
|
|
const { PrismaClient } = require('@prisma/client');
|
|
const prisma = new PrismaClient();
|
|
try {
|
|
const row = await prisma.systemVersion.create({
|
|
data: {
|
|
gitTag,
|
|
commitId,
|
|
commitMessage: commitMessage || '(no message)',
|
|
branch,
|
|
deployedBy,
|
|
},
|
|
});
|
|
console.log(
|
|
`[record-system-version] ok id=${row.id} commit=${commitId.slice(0, 7)} tag=${gitTag || '-'} by=${deployedBy}`,
|
|
);
|
|
} finally {
|
|
await prisma.$disconnect();
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.warn('[record-system-version] WARN:', err instanceof Error ? err.message : err);
|
|
process.exit(0);
|
|
});
|