mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 15:09:19 +08:00
refacotr: 重构部署实现,不每次都重新构建镜像,只在第一次构建,后面只将编译产物放入挂载目录重启容器
This commit is contained in:
@@ -1,6 +1,16 @@
|
||||
import { connectWithRetry, installKeyAfterPasswordAuth, execRemote } from '../ssh.js';
|
||||
import { composeDeploy, composeRestart, composeLogs, composeStatus, composeDown, uploadComposeFile } from '../docker.js';
|
||||
import { loadConfig, getServiceConfig, syncServerKeyAuth } from '../config.js';
|
||||
import {
|
||||
composeRestartAfterUpload,
|
||||
initServiceRuntime,
|
||||
remoteImageExists,
|
||||
uploadComposeFile,
|
||||
composeRestart,
|
||||
composeLogs,
|
||||
composeStatus,
|
||||
composeDown,
|
||||
} from '../docker.js';
|
||||
import { assertArtifactsExist, ensureArtifactsDirs } from '../artifacts.js';
|
||||
import { getArtifactDir, loadConfig, getServiceConfig, syncServerKeyAuth } from '../config.js';
|
||||
import { printError, printInfo, printSuccess } from '../ui/render.js';
|
||||
import type { Environment, Service, ServiceEnvConfig } from '../types.js';
|
||||
|
||||
@@ -18,11 +28,12 @@ const deployers: Record<Service, {
|
||||
};
|
||||
|
||||
export async function buildService(service: Service, cfg: ServiceEnvConfig): Promise<string> {
|
||||
ensureArtifactsDirs();
|
||||
const deployer = deployers[service];
|
||||
if (!deployer) throw new Error(`未知服务: ${service}`);
|
||||
printInfo(`构建 ${service}...`);
|
||||
const buildDir = await deployer.build(cfg);
|
||||
printSuccess(`${service} 构建完成`);
|
||||
printSuccess(`${service} 构建完成 → ${getArtifactDir(service)}`);
|
||||
return buildDir;
|
||||
}
|
||||
|
||||
@@ -39,7 +50,7 @@ export async function deployService(
|
||||
await deployer.deploy(conn, cfg, buildDir, env);
|
||||
}
|
||||
|
||||
/** 连接 SSH;密码登录成功后自动安装公钥并持久化到 config.json */
|
||||
/** 连接 SSH;密码登录成功后自动安装公钥 */
|
||||
async function connectForService(
|
||||
cfg: ServiceEnvConfig,
|
||||
onPassword?: () => Promise<string>,
|
||||
@@ -62,18 +73,41 @@ async function connectForService(
|
||||
return conn;
|
||||
}
|
||||
|
||||
export async function buildAndDeployServices(
|
||||
/** Docker 服务:上传产物后,首次 init 镜像,后续仅重启 */
|
||||
async function finishDockerDeploy(conn: any, service: Service, cfg: ServiceEnvConfig, env: Environment): Promise<void> {
|
||||
const exists = await remoteImageExists(conn, service, env);
|
||||
if (!exists) {
|
||||
await initServiceRuntime(conn, service, cfg, env);
|
||||
} else {
|
||||
await uploadComposeFile(conn, service, cfg, env);
|
||||
await composeRestartAfterUpload(conn, cfg, service, env);
|
||||
}
|
||||
}
|
||||
|
||||
/** 仅编译到 artifacts/,不上传 */
|
||||
export async function buildServicesOnly(services: Service[]): Promise<void> {
|
||||
ensureArtifactsDirs();
|
||||
for (const service of services) {
|
||||
const config = await loadConfig();
|
||||
const cfg = getServiceConfig(config, service);
|
||||
console.clear();
|
||||
await buildService(service, cfg);
|
||||
}
|
||||
}
|
||||
|
||||
/** 从 artifacts/ 上传并重启(不编译) */
|
||||
export async function deployArtifactsOnly(
|
||||
services: Service[],
|
||||
onPassword?: (service: Service) => Promise<string>,
|
||||
): Promise<void> {
|
||||
ensureArtifactsDirs();
|
||||
for (const service of services) {
|
||||
assertArtifactsExist(service);
|
||||
const config = await loadConfig();
|
||||
const env = config.currentEnv;
|
||||
const cfg = getServiceConfig(config, service, env);
|
||||
console.clear();
|
||||
const buildDir = await buildService(service, cfg);
|
||||
|
||||
// 若同 host 已配置密钥,connectWithRetry 会直接免密,不再触发 onPassword
|
||||
const conn = await connectForService(
|
||||
cfg,
|
||||
onPassword ? () => onPassword(service) : undefined,
|
||||
@@ -81,12 +115,42 @@ export async function buildAndDeployServices(
|
||||
|
||||
try {
|
||||
if (cfg.extractDir) {
|
||||
await deployService(conn, service, cfg, buildDir, env);
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
} else {
|
||||
await uploadComposeFile(conn, service, cfg, env);
|
||||
await deployService(conn, service, cfg, buildDir, env);
|
||||
// 镜像重建后强制 recreate,否则 :latest 内容变了容器也不会换
|
||||
await composeDeploy(conn, cfg, service, env);
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
await finishDockerDeploy(conn, service, cfg, env);
|
||||
}
|
||||
} finally {
|
||||
conn.end();
|
||||
}
|
||||
printSuccess(`${service} 产物已上传并重启`);
|
||||
}
|
||||
}
|
||||
|
||||
/** 自动部署:编译 → 上传 → 重启(不重建镜像) */
|
||||
export async function buildAndDeployServices(
|
||||
services: Service[],
|
||||
onPassword?: (service: Service) => Promise<string>,
|
||||
): Promise<void> {
|
||||
ensureArtifactsDirs();
|
||||
for (const service of services) {
|
||||
const config = await loadConfig();
|
||||
const env = config.currentEnv;
|
||||
const cfg = getServiceConfig(config, service, env);
|
||||
console.clear();
|
||||
await buildService(service, cfg);
|
||||
|
||||
const conn = await connectForService(
|
||||
cfg,
|
||||
onPassword ? () => onPassword(service) : undefined,
|
||||
);
|
||||
|
||||
try {
|
||||
if (cfg.extractDir) {
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
} else {
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
await finishDockerDeploy(conn, service, cfg, env);
|
||||
}
|
||||
} finally {
|
||||
conn.end();
|
||||
|
||||
Reference in New Issue
Block a user