feat: 完善部署脚本

This commit is contained in:
ah--desktop
2026-07-08 23:44:05 +08:00
parent 8af2e9194b
commit 8aee7f407d
16 changed files with 325 additions and 109 deletions
+34 -18
View File
@@ -1,5 +1,5 @@
import { connectWithRetry, installKeyAfterPasswordAuth, execRemote } from '../ssh.js';
import { composeUp, composeRestart, composeLogs, composeStatus, composeDown, uploadComposeFile } from '../docker.js';
import { composeDeploy, composeRestart, composeLogs, composeStatus, composeDown, uploadComposeFile } from '../docker.js';
import { loadConfig, getServiceConfig, syncServerKeyAuth } from '../config.js';
import { printError, printInfo, printSuccess } from '../ui/render.js';
import type { Environment, Service, ServiceEnvConfig } from '../types.js';
@@ -8,7 +8,10 @@ import * as frontendDeploy from './frontend.js';
import * as backendDeploy from './backend.js';
import * as licenseDeploy from './license.js';
const deployers: Record<Service, { build: (cfg?: ServiceEnvConfig) => Promise<string>; deploy: (conn: any, cfg: ServiceEnvConfig, buildDir: string) => Promise<void> }> = {
const deployers: Record<Service, {
build: (cfg?: ServiceEnvConfig) => Promise<string>;
deploy: (conn: any, cfg: ServiceEnvConfig, buildDir: string, env: Environment) => Promise<void>;
}> = {
frontend: frontendDeploy as any,
backend: backendDeploy as any,
license: licenseDeploy as any,
@@ -23,11 +26,17 @@ export async function buildService(service: Service, cfg: ServiceEnvConfig): Pro
return buildDir;
}
export async function deployService(conn: any, service: Service, cfg: ServiceEnvConfig, buildDir: string): Promise<void> {
export async function deployService(
conn: any,
service: Service,
cfg: ServiceEnvConfig,
buildDir: string,
env: Environment,
): Promise<void> {
const deployer = deployers[service];
if (!deployer) throw new Error(`未知服务: ${service}`);
printInfo(`部署 ${service} 到 ${cfg.server.host}...`);
await deployer.deploy(conn, cfg, buildDir);
await deployer.deploy(conn, cfg, buildDir, env);
}
/** 连接 SSH;密码登录成功后自动安装公钥并持久化到 config.json */
@@ -59,7 +68,8 @@ export async function buildAndDeployServices(
): Promise<void> {
for (const service of services) {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const env = config.currentEnv;
const cfg = getServiceConfig(config, service, env);
console.clear();
const buildDir = await buildService(service, cfg);
@@ -71,11 +81,12 @@ export async function buildAndDeployServices(
try {
if (cfg.extractDir) {
await deployService(conn, service, cfg, buildDir);
await deployService(conn, service, cfg, buildDir, env);
} else {
await uploadComposeFile(conn, service, cfg);
await deployService(conn, service, cfg, buildDir);
await composeUp(conn, cfg, service);
await uploadComposeFile(conn, service, cfg, env);
await deployService(conn, service, cfg, buildDir, env);
// 镜像重建后强制 recreate,否则 :latest 内容变了容器也不会换
await composeDeploy(conn, cfg, service, env);
}
} finally {
conn.end();
@@ -86,35 +97,40 @@ export async function buildAndDeployServices(
export async function restartService(service: Service, onPassword?: () => Promise<string>): Promise<void> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const env = config.currentEnv;
const cfg = getServiceConfig(config, service, env);
const conn = await connectForService(cfg, onPassword);
try { await composeRestart(conn, cfg, service); } finally { conn.end(); }
try { await composeRestart(conn, cfg, service, env); } finally { conn.end(); }
}
export async function stopService(service: Service, onPassword?: () => Promise<string>): Promise<void> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const env = config.currentEnv;
const cfg = getServiceConfig(config, service, env);
const conn = await connectForService(cfg, onPassword);
try { await composeDown(conn, cfg, service); } finally { conn.end(); }
try { await composeDown(conn, cfg, service, env); } finally { conn.end(); }
}
export async function serviceLogs(service: Service, tail = 50, onPassword?: () => Promise<string>): Promise<string> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const env = config.currentEnv;
const cfg = getServiceConfig(config, service, env);
const conn = await connectForService(cfg, onPassword);
try { return await composeLogs(conn, cfg, service, tail); } finally { conn.end(); }
try { return await composeLogs(conn, cfg, service, env, tail); } finally { conn.end(); }
}
export async function serviceStatus(service: Service, onPassword?: () => Promise<string>): Promise<string> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const env = config.currentEnv;
const cfg = getServiceConfig(config, service, env);
const conn = await connectForService(cfg, onPassword);
try { return await composeStatus(conn, cfg); } finally { conn.end(); }
try { return await composeStatus(conn, cfg, service, env); } finally { conn.end(); }
}
export async function checkRemoteDocker(service: Service, onPassword?: () => Promise<string>): Promise<boolean> {
const config = await loadConfig();
const cfg = getServiceConfig(config, service);
const env = config.currentEnv;
const cfg = getServiceConfig(config, service, env);
try {
const conn = await connectForService(cfg, onPassword);
try {