oms/tools/src/commands/rollback.ts
liufumingren 406e5f3b58 refactor(tools): 发布工具统一到 tools(前后端同时发布 + git 约束 + 共享配置)
- 新增 tools TypeScript 发布 CLI:deploy / deploy:backend / deploy:frontend / rollback / init
- 删除 backend/script 与 frontend/scripts 旧发布脚本,前后端 npm scripts 转发到 tools
- 发布配置独立为 tools/config/deploy.config.local.cjs(模板入库,真实配置与生产 env 不入库)
- 发布受限于 git:工作区有未提交改动默认中止(--allow-dirty 放行),版本记录含 commit/branch
- 后端配置集中到 <deployRoot>/shared-config,本地 env 发布时覆盖共享配置;端口由 EGG_PORT 注入
- 前端服务器端脚本迁至 tools/remote,失败时清理未上线版本目录
- .gitignore 改为忽略 tools 本地配置与临时发布包

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-30 23:04:54 +08:00

59 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { parseArgs } from '../lib/args';
import { buildBackendRollbackScript, readRemoteCurrentVersion, resolveRollbackTarget } from '../lib/backend-remote';
import { loadDeployConfig } from '../lib/config';
import { buildFrontendRollbackScript } from '../lib/frontend-remote';
import { ensureCommandAvailable } from '../lib/shell';
import { runSsh } from '../lib/ssh';
import type { DeployTargets } from './deploy';
/**
* 回滚(后端切回另一套版本目录 + 前端 current 软链切回上一个版本)
*
* npm run rollback # 前后端一起回滚
* npm run rollback -- --to=v1 # 指定后端回滚到 v1(前端仍回上一个版本)
* npm run rollback -- --dry-run # 只打印远端脚本
*/
export function runRollback(args: string[], targets: DeployTargets): void {
const parsed = parseArgs(args);
const dryRun = parsed.flags.has('dry-run');
const config = loadDeployConfig(parsed.env, { allowMissingSecrets: dryRun });
const backend = targets.backend ? config.backend : null;
const frontend = targets.frontend ? config.frontend : null;
if (targets.backend && !backend) throw new Error('当前配置未启用 backend,无法回滚后端。');
if (targets.frontend && !frontend) throw new Error('当前配置未启用 frontend,无法回滚前端。');
if (!dryRun) ensureCommandAvailable('ssh', '请先安装 OpenSSH Client 并确保 ssh 在 PATH 中。');
const requested = parsed.values.to || '';
let backendScript = '';
if (backend) {
if (dryRun) {
backendScript = buildBackendRollbackScript(config, requested);
console.log(`[rollback] 后端(dry-run): 目标 ${requested || '另一套版本'}`);
} else {
const currentVersion = readRemoteCurrentVersion(config);
const targetVersion = resolveRollbackTarget(currentVersion, requested);
backendScript = buildBackendRollbackScript(config, targetVersion);
console.log(`[rollback] 后端: ${currentVersion} → ${targetVersion}`);
}
}
const frontendScript = frontend ? buildFrontendRollbackScript(frontend) : '';
if (frontend) console.log('[rollback] 前端: current → releases 里的上一个版本');
const remoteScript = [ backendScript, frontendScript ].filter(Boolean).join('\n\n');
if (dryRun) {
console.log('\n[rollback] --dry-run:远端脚本如下:\n');
console.log('============================================================');
console.log(remoteScript);
console.log('============================================================');
return;
}
runSsh(config, remoteScript);
console.log('\n[rollback] ✅ 回滚完成');
}