mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 09:09:19 +08:00
feat(部署): 优化部署目录
This commit is contained in:
@@ -2,7 +2,7 @@ import { cpSync, existsSync, mkdirSync, readdirSync, rmSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { Client } from 'ssh2';
|
||||
import { getArtifactDir, getArtifactsRoot } from './config.js';
|
||||
import { getArtifactDir, getArtifactsRoot, getBackendDir } from './config.js';
|
||||
import { createTarGz, getLocalTempPath } from './platform.js';
|
||||
import { ensureRemoteDir, execRemote, uploadFile } from './ssh.js';
|
||||
import { printInfo } from './ui/render.js';
|
||||
@@ -25,10 +25,14 @@ export function clearArtifactDir(service: Service): void {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
/** 同步后端 JAR 到 artifacts/backend/app.jar */
|
||||
/** 同步后端 JAR 与 static 到 artifacts/backend/ */
|
||||
export function syncBackendArtifact(jarPath: string): string {
|
||||
clearArtifactDir('backend');
|
||||
cpSync(jarPath, join(getArtifactDir('backend'), 'app.jar'));
|
||||
const staticSrc = join(getBackendDir(), 'src/main/resources/static');
|
||||
if (existsSync(staticSrc)) {
|
||||
cpSync(staticSrc, join(getArtifactDir('backend'), 'static'), { recursive: true });
|
||||
}
|
||||
return getArtifactDir('backend');
|
||||
}
|
||||
|
||||
@@ -87,3 +91,25 @@ export async function uploadArtifactDirectory(
|
||||
);
|
||||
if (code !== 0) throw new Error(`远程解压失败: ${stderr}`);
|
||||
}
|
||||
|
||||
/** 将本地目录打包解压到远程目录(合并,不清空目标) */
|
||||
export async function uploadDirectoryMerge(
|
||||
conn: Client,
|
||||
localDir: string,
|
||||
remoteDir: string,
|
||||
): Promise<void> {
|
||||
if (!existsSync(localDir)) return;
|
||||
await ensureRemoteDir(conn, remoteDir);
|
||||
const parentDir = remoteDir.replace(/\/+$/, '');
|
||||
const tmpTar = getLocalTempPath(`ecom-merge-${Date.now()}.tar.gz`);
|
||||
const remoteTar = `${parentDir}/merge-upload.tar.gz`;
|
||||
|
||||
createTarGz(localDir, tmpTar);
|
||||
await uploadFile(conn, tmpTar, remoteTar);
|
||||
|
||||
const { code, stderr } = await execRemote(
|
||||
conn,
|
||||
`mkdir -p "${remoteDir}" && tar -xzf "${remoteTar}" -C "${remoteDir}" && rm -f "${remoteTar}"`,
|
||||
);
|
||||
if (code !== 0) throw new Error(`远程合并解压失败: ${stderr}`);
|
||||
}
|
||||
|
||||
@@ -3,10 +3,15 @@ import { existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { Client } from 'ssh2';
|
||||
import { syncBackendArtifact } from '../artifacts.js';
|
||||
import { getArtifactDir, getBackendDir, getRemoteAppDir } from '../config.js';
|
||||
import { syncBackendArtifact, uploadDirectoryMerge } from '../artifacts.js';
|
||||
import {
|
||||
getArtifactDir,
|
||||
getBackendDir,
|
||||
getRemoteAppDir,
|
||||
resolveSpringProfile,
|
||||
} from '../config.js';
|
||||
import { findMainJar, resolveMavenCommand } from '../platform.js';
|
||||
import { ensureRemoteDir, uploadFile } from '../ssh.js';
|
||||
import { ensureRemoteDir, execRemote, uploadFile } from '../ssh.js';
|
||||
import { printInfo, printSuccess } from '../ui/render.js';
|
||||
import type { Environment, Service, ServiceEnvConfig } from '../types.js';
|
||||
|
||||
@@ -31,11 +36,52 @@ export async function build(_cfg: ServiceEnvConfig): Promise<string> {
|
||||
return artifactDir;
|
||||
}
|
||||
|
||||
/** 上传 artifacts/backend 到远程挂载目录 */
|
||||
/** 远程尚无文件时初始化环境 yml(app 根目录)与 config/ 下的 license 相关文件 */
|
||||
async function seedRemoteConfigIfMissing(
|
||||
conn: Client,
|
||||
cfg: ServiceEnvConfig,
|
||||
remoteAppDir: string,
|
||||
): Promise<void> {
|
||||
const profile = resolveSpringProfile(cfg.springProfile);
|
||||
const backendDir = getBackendDir();
|
||||
const envYml = `application-${profile}.yml`;
|
||||
const seeds: { local: string; remote: string }[] = [
|
||||
{
|
||||
local: join(backendDir, 'src/main/resources', envYml),
|
||||
remote: `${remoteAppDir}/${envYml}`,
|
||||
},
|
||||
{
|
||||
local: join(backendDir, 'src/main/resources/config/channel-pdd.yml'),
|
||||
remote: `${remoteAppDir}/config/channel-pdd.yml`,
|
||||
},
|
||||
];
|
||||
|
||||
await ensureRemoteDir(conn, `${remoteAppDir}/config`);
|
||||
for (const { local, remote } of seeds) {
|
||||
if (!existsSync(local)) continue;
|
||||
const { code } = await execRemote(conn, `test -f "${remote}"`);
|
||||
if (code === 0) continue;
|
||||
await uploadFile(conn, local, remote);
|
||||
printInfo(`已初始化配置(仅首次): ${remote}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** 上传 artifacts/backend 到远程 app/(jar + static;config/uploads 保留宿主机数据) */
|
||||
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, env: Environment): Promise<void> {
|
||||
const artifactDir = getArtifactDir('backend');
|
||||
const remoteAppDir = getRemoteAppDir(cfg, 'backend', env);
|
||||
|
||||
await ensureRemoteDir(conn, remoteAppDir);
|
||||
await ensureRemoteDir(conn, `${remoteAppDir}/uploads`);
|
||||
await seedRemoteConfigIfMissing(conn, cfg, remoteAppDir);
|
||||
|
||||
await uploadFile(conn, join(artifactDir, 'app.jar'), `${remoteAppDir}/app.jar`);
|
||||
|
||||
const localStatic = join(artifactDir, 'static');
|
||||
if (existsSync(localStatic)) {
|
||||
printInfo('同步 static/ 到远程(合并,不删除 uploads/ 与宿主机配置)...');
|
||||
await uploadDirectoryMerge(conn, localStatic, `${remoteAppDir}/static`);
|
||||
}
|
||||
|
||||
printSuccess(`后端产物已上传到 ${remoteAppDir}`);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,16 @@ function resolveBridgePorts(service: Service, cfg: ServiceEnvConfig, env: Enviro
|
||||
return { hostPort: cfg.port, containerPort: cfg.port };
|
||||
}
|
||||
|
||||
function backendComposeVolumes(profile: string): string {
|
||||
return [
|
||||
' - ./app/app.jar:/app/app.jar:ro',
|
||||
` - ./app/application-${profile}.yml:/app/application-${profile}.yml:ro`,
|
||||
' - ./app/config:/app/config:ro',
|
||||
' - ./app/uploads:/app/uploads:rw',
|
||||
' - ./app/static:/app/static:ro',
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
export function generateComposeFile(service: Service, cfg: ServiceEnvConfig, env: Environment): string {
|
||||
const { composeService, containerName, imageTag } = getDockerResourceNames(service, env);
|
||||
const { hostPort, containerPort } = resolveBridgePorts(service, cfg, env);
|
||||
@@ -53,6 +63,10 @@ export function generateComposeFile(service: Service, cfg: ServiceEnvConfig, env
|
||||
].filter(Boolean).join('\n');
|
||||
}
|
||||
|
||||
const volumeBlock = service === 'backend'
|
||||
? backendComposeVolumes(resolveSpringProfile(cfg.springProfile))
|
||||
: ' - ./app:/app:ro';
|
||||
|
||||
return `services:
|
||||
${composeService}:
|
||||
image: ${imageTag}
|
||||
@@ -61,7 +75,7 @@ export function generateComposeFile(service: Service, cfg: ServiceEnvConfig, env
|
||||
ports:
|
||||
- "${hostPort}:${containerPort}"
|
||||
volumes:
|
||||
- ./app:/app:ro
|
||||
${volumeBlock}
|
||||
environment:
|
||||
${envBlock}
|
||||
restart: unless-stopped
|
||||
@@ -165,12 +179,10 @@ export async function composeRestartAfterUpload(
|
||||
const running = psOut.trim().length > 0;
|
||||
|
||||
if (running) {
|
||||
const { code, stdout, stderr } = await execRemote(
|
||||
conn,
|
||||
`${composeCmd(cfg, env, service)} restart ${composeService} 2>&1`,
|
||||
);
|
||||
if (code !== 0) throw new Error(`${service} 重启失败:\n${stdout || stderr}`);
|
||||
printSuccess(`${composeService} 已重启`);
|
||||
const upCmd = `${composeCmd(cfg, env, service)} up -d ${composeService} 2>&1`;
|
||||
const { code, stdout, stderr } = await execRemote(conn, upCmd);
|
||||
if (code !== 0) throw new Error(`${service} 应用 compose 失败:\n${stdout || stderr}`);
|
||||
printSuccess(`${composeService} 已更新并运行`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user