/** * AGC 总版本号发号入口(CI 发号 Job 与本地手工兜底共用)。 * * 用法: * node scripts/issue-global-version.mjs --channel dev-win [--commit ] [--build-id ] [--out ] * node scripts/issue-global-version.mjs --seed-only * node scripts/issue-global-version.mjs --dry-run --channel dev-win # 只预览,不写回、不烧号 * * 输出固定为一行 `AGC_GLOBAL_VERSION=`,便于 Jenkins 直接读取。 */ import fs from 'node:fs'; import { issueGlobalVersion, previewNextGlobalVersion, readGlobalVersion, readReleaseDryRun, resolveSeedBaseline, writeGlobalVersion, } from './agc-global-version.mjs'; function parseArgs(argv) { const options = { channel: '', commit: '', buildId: '', repoVersion: '', out: '', seedOnly: false, dryRun: readReleaseDryRun(), }; for (let index = 0; index < argv.length; index += 1) { const arg = argv[index]; const readValue = (label) => { const value = argv[index + 1]; if (value == null || value.startsWith('--')) { throw new Error(`${label} 缺少取值`); } index += 1; return value; }; switch (arg) { case '--channel': options.channel = readValue('--channel'); break; case '--commit': options.commit = readValue('--commit'); break; case '--build-id': options.buildId = readValue('--build-id'); break; case '--repo-version': options.repoVersion = readValue('--repo-version'); break; case '--out': options.out = readValue('--out'); break; case '--seed-only': options.seedOnly = true; break; case '--dry-run': options.dryRun = true; break; default: throw new Error(`未知参数:${arg}`); } } return options; } function emit(version, options) { console.log(`AGC_GLOBAL_VERSION=${version}`); if (options.out) { fs.writeFileSync(options.out, `${version}\n`, 'utf8'); } } const options = parseArgs(process.argv.slice(2)); process.env.AGC_RELEASE_DRY_RUN = options.dryRun ? '1' : '0'; if (options.seedOnly) { const current = await readGlobalVersion(); if (current) { console.log( `[agc-global-version] 总号已存在(${current.version}),播种跳过;需要重新播种请先人工确认`, ); emit(current.version, options); } else { const baseline = await resolveSeedBaseline({ repoVersion: options.repoVersion || null, }); const payload = { version: baseline, updatedAt: new Date().toISOString(), channel: options.channel || 'seed', commit: options.commit || null, buildId: options.buildId || null, }; writeGlobalVersion(payload, { dryRun: options.dryRun }); console.log( `[agc-global-version] 播种基线 ${baseline}(尚未发号,首发为下一位)`, ); emit(baseline, options); } } else if (options.dryRun) { const preview = await previewNextGlobalVersion({ repoVersion: options.repoVersion || null, }); console.log( `[agc-global-version] 预览下一个总号 ${preview}(dry-run 不写回、不烧号)`, ); emit(preview, options); } else { const issued = await issueGlobalVersion({ channel: options.channel || 'manual', commit: options.commit || null, buildId: options.buildId || null, repoVersion: options.repoVersion || null, }); emit(issued, options); }