refacotr: 重构部署实现,不每次都重新构建镜像,只在第一次构建,后面只将编译产物放入挂载目录重启容器

This commit is contained in:
ah--desktop
2026-07-12 17:13:45 +08:00
parent 7a2498cef2
commit b9a75af67c
13 changed files with 523 additions and 130 deletions
+26 -30
View File
@@ -1,12 +1,12 @@
import { execSync } from 'child_process';
import { writeFileSync, readFileSync, existsSync, mkdirSync, rmSync } from 'fs';
import { existsSync, mkdirSync, rmSync } from 'fs';
import { join } from 'path';
import { Client } from 'ssh2';
import { getLicenseDir, getLicenseDeployDir } from '../config.js';
import { copyBuildArtifacts, createTarGz, getLocalTempPath } from '../platform.js';
import { execRemote, uploadFile, ensureRemoteDir } from '../ssh.js';
import { remoteBuildImage } from '../docker.js';
import { syncLicenseArtifacts, uploadArtifactDirectory } from '../artifacts.js';
import { getArtifactDir, getLicenseDir, getRemoteDataDir } from '../config.js';
import { copyBuildArtifacts, getLocalTempPath } from '../platform.js';
import { ensureRemoteDir } from '../ssh.js';
import { printInfo, printSuccess } from '../ui/render.js';
import type { Environment, Service, ServiceEnvConfig } from '../types.js';
@@ -19,8 +19,8 @@ export async function build(): Promise<string> {
printInfo('构建许可证 (NestJS)...');
execSync('pnpm install && pnpm build', { stdio: 'inherit', cwd: serverDir });
printSuccess('许可证构建完成');
// 先收集到临时目录,再同步到 artifacts/license
const tmpDir = getLocalTempPath('ecom-license-build');
if (existsSync(tmpDir)) rmSync(tmpDir, { recursive: true });
mkdirSync(tmpDir, { recursive: true });
@@ -31,30 +31,26 @@ export async function build(): Promise<string> {
copyBuildArtifacts(join(serverDir, 'keys'), join(tmpDir, 'keys'), { optional: true });
copyBuildArtifacts(join(serverDir, '.env'), join(tmpDir, '.env'), { optional: true });
return tmpDir;
const artifactDir = syncLicenseArtifacts(tmpDir);
rmSync(tmpDir, { recursive: true, force: true });
// 本地安装生产依赖,一并打入产物目录
printInfo('安装许可证生产依赖...');
execSync('corepack enable && pnpm install --prod --frozen-lockfile', {
stdio: 'inherit',
cwd: artifactDir,
shell: process.platform === 'win32' ? (process.env.COMSPEC || 'cmd.exe') : undefined,
});
printSuccess(`许可证构建完成,产物: ${artifactDir}`);
return artifactDir;
}
export async function deploy(conn: Client, cfg: ServiceEnvConfig, buildDir: string, env: Environment): Promise<void> {
const serviceDir = `${cfg.server.deployPath}/license`;
await ensureRemoteDir(conn, serviceDir);
const tmpTar = getLocalTempPath('ecom-license-build.tar.gz');
createTarGz(buildDir, tmpTar);
const dockerfileTmp = getLocalTempPath('ecom-license.Dockerfile');
const dockerfileSrc = join(getLicenseDeployDir(), 'Dockerfile');
if (!existsSync(dockerfileSrc)) {
printInfo('许可证 deploy/Dockerfile 不存在,使用默认模板');
writeFileSync(dockerfileTmp, 'FROM node:22-alpine\nWORKDIR /app\nRUN addgroup -S app && adduser -S app -G app\nCOPY package.json pnpm-lock.yaml ./\nRUN corepack enable && pnpm install --prod --frozen-lockfile 2>/dev/null; exit 0\nCOPY dist ./dist\nCOPY keys ./keys\nCOPY .env ./.env\nRUN chown -R app:app /app\nUSER app\nEXPOSE 3100\nCMD ["node", "dist/main.js"]\n');
} else {
writeFileSync(dockerfileTmp, readFileSync(dockerfileSrc, 'utf8'));
}
await uploadFile(conn, tmpTar, `${serviceDir}/build.tar.gz`);
await uploadFile(conn, dockerfileTmp, `${serviceDir}/Dockerfile`);
const { code, stderr } = await execRemote(conn, `cd "${serviceDir}" && tar -xzf build.tar.gz && rm build.tar.gz`);
if (code !== 0) throw new Error(`解压失败: ${stderr}`);
await remoteBuildImage(conn, 'license', serviceDir, env);
/** 上传 artifacts/license 到远程挂载目录 */
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, _env: Environment): Promise<void> {
const artifactDir = getArtifactDir('license');
const remoteDataDir = getRemoteDataDir(cfg, 'license');
await ensureRemoteDir(conn, remoteDataDir);
await uploadArtifactDirectory(conn, artifactDir, remoteDataDir);
printSuccess(`许可证产物已上传到 ${remoteDataDir}`);
}