feat: 新增部署脚本

This commit is contained in:
小蟑螂
2026-07-07 23:15:10 +08:00
parent dd8cba9777
commit 8af2e9194b
15 changed files with 2323 additions and 0 deletions
+92
View File
@@ -0,0 +1,92 @@
import { writeFileSync } from 'fs';
import { Client } from 'ssh2';
import { resolveSpringProfile } from './config.js';
import { execRemote, ensureRemoteDir, uploadFile } from './ssh.js';
import { printSuccess } from './ui/render.js';
import type { Service, ServiceEnvConfig } from './types.js';
export function generateComposeFile(service: Service, cfg: ServiceEnvConfig): string {
const tag = `ecom-${service}:latest`;
let envBlock = '';
if (service === 'frontend') {
envBlock = ` - NGINX_PORT=${cfg.port}`;
} else if (service === 'backend') {
envBlock = [
` - SERVER_PORT=${cfg.port}`,
` - SPRING_PROFILES_ACTIVE=${resolveSpringProfile(cfg.springProfile)}`,
cfg.dbUrl ? ` - DB_URL=${cfg.dbUrl}` : '',
cfg.redisUrl ? ` - REDIS_URL=${cfg.redisUrl}` : '',
].filter(Boolean).join('\n');
} else if (service === 'license') {
envBlock = [
` - NODE_ENV=${cfg.nodeEnv || 'production'}`,
` - PORT=${cfg.port}`,
cfg.licenseDbUrl ? ` - DB_URL=${cfg.licenseDbUrl}` : '',
].filter(Boolean).join('\n');
}
return `services:
${service}:
image: ${tag}
container_name: ecom-${service}
network_mode: host
environment:
${envBlock}
restart: unless-stopped
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"
`;
}
export async function remoteBuildImage(conn: Client, service: Service, serviceDir: string): Promise<void> {
const tag = `ecom-${service}:latest`;
const { code, stdout, stderr } = await execRemote(conn, `cd "${serviceDir}" && docker build -t ${tag} . 2>&1`);
if (code !== 0) {
throw new Error(`${service} 镜像构建失败:\n${stdout || stderr}`);
}
printSuccess(`${service} 镜像构建完成`);
}
export async function uploadComposeFile(conn: Client, service: Service, cfg: ServiceEnvConfig): Promise<void> {
const deployPath = cfg.server.deployPath;
const tmpCompose = `/tmp/ecom-${service}-docker-compose.yml`;
writeFileSync(tmpCompose, generateComposeFile(service, cfg), 'utf8');
await ensureRemoteDir(conn, deployPath);
await uploadFile(conn, tmpCompose, `${deployPath}/docker-compose.yml`);
}
export async function composeUp(conn: Client, cfg: ServiceEnvConfig, service: Service): Promise<void> {
const { code, stdout, stderr } = await execRemote(conn, `cd "${cfg.server.deployPath}" && docker compose up -d ${service} 2>&1`);
if (code !== 0) {
throw new Error(`${service} 启动失败:\n${stdout || stderr}`);
}
printSuccess(`${service} 已启动`);
}
export async function composeDown(conn: Client, cfg: ServiceEnvConfig, service: Service): Promise<void> {
await execRemote(conn, `cd "${cfg.server.deployPath}" && docker compose down ${service}`);
printSuccess(`${service} 已停止`);
}
export async function composeRestart(conn: Client, cfg: ServiceEnvConfig, service: Service): Promise<void> {
const { code, stdout, stderr } = await execRemote(conn, `cd "${cfg.server.deployPath}" && docker compose restart ${service} 2>&1`);
if (code !== 0) {
throw new Error(`${service} 重启失败:\n${stdout || stderr}`);
}
printSuccess(`${service} 已重启`);
}
export async function composeLogs(conn: Client, cfg: ServiceEnvConfig, service: Service, tail = 50): Promise<string> {
const { stdout } = await execRemote(conn, `cd "${cfg.server.deployPath}" && docker compose logs --tail=${tail} ${service}`);
return stdout;
}
export async function composeStatus(conn: Client, cfg: ServiceEnvConfig): Promise<string> {
const { stdout } = await execRemote(conn, `cd "${cfg.server.deployPath}" && docker compose ps`);
return stdout;
}