mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: 调整部署脚本
This commit is contained in:
@@ -2,8 +2,37 @@
|
||||
|
||||
部署采用 **运行时镜像 + 目录挂载** 模式:
|
||||
|
||||
- **首次部署**:在服务器构建一次运行时镜像(仅 JRE/Node 环境,不含业务代码),启动容器并挂载 `data/` 目录。
|
||||
- **后续更新**:只需上传编译产物到 `data/`,然后 **重启容器**,无需重新 `docker build`。
|
||||
- **首次部署**:在服务器构建一次运行时镜像(仅 JRE/Node 环境,不含业务代码),启动容器并挂载 `app/` 目录。
|
||||
- **后续更新**:只需上传编译产物到 `app/`,然后 **重启容器**,无需重新 `docker build`。
|
||||
|
||||
## 远程目录结构
|
||||
|
||||
`deployPath` 为部署根目录。
|
||||
|
||||
**后端 / 授权(Docker)**:
|
||||
|
||||
```
|
||||
{deployPath}/ecom-dmb/{service}/{env}/
|
||||
├── docker-compose.yml
|
||||
├── Dockerfile
|
||||
└── app/
|
||||
```
|
||||
|
||||
**前端(静态,用目录名区分环境)**:
|
||||
|
||||
```
|
||||
{deployPath}/ecom-dmb-{env}/index/
|
||||
```
|
||||
|
||||
示例(根目录 `/opt`,test):
|
||||
|
||||
```
|
||||
/opt/ecom-dmb/backend/test/app/app.jar
|
||||
/opt/ecom-dmb-test/index/ # 前端站点根
|
||||
/opt/ecom-dmb/license/test/app/
|
||||
```
|
||||
|
||||
Docker 资源名同样带环境后缀:`ecom-backend-test` / `ecom-backend-prod` 等。
|
||||
|
||||
本地编译产物统一放在 `ecom-deploy/artifacts/`(已 gitignore):
|
||||
|
||||
@@ -65,35 +94,36 @@ pnpm install --prod --frozen-lockfile
|
||||
|
||||
### 2. 上传到服务器
|
||||
|
||||
**后端**(以 test 环境为例,部署路径 `/opt/ecom-backend`):
|
||||
**后端**(根目录 `/opt`,test 环境):
|
||||
|
||||
```bash
|
||||
scp artifacts/backend/app.jar root@192.168.3.101:/opt/ecom-backend/backend/data/app.jar
|
||||
scp artifacts/backend/app.jar user@192.168.3.101:/opt/ecom-dmb/backend/test/app/app.jar
|
||||
```
|
||||
|
||||
**许可证**:
|
||||
|
||||
```bash
|
||||
# 打包上传
|
||||
tar -czf license.tar.gz -C artifacts/license .
|
||||
scp license.tar.gz root@host:/opt/ecom-license/license/
|
||||
ssh root@host 'cd /opt/ecom-license/license && rm -rf data/* && mkdir -p data && tar -xzf upload.tar.gz -C data'
|
||||
scp license.tar.gz user@host:/opt/ecom-dmb/license/test/
|
||||
ssh user@host 'cd /opt/ecom-dmb/license/test && rm -rf app/* && mkdir -p app && tar -xzf license.tar.gz -C app && rm license.tar.gz'
|
||||
```
|
||||
|
||||
**前端**(静态目录部署):
|
||||
**前端**(静态目录,按环境后缀区分):
|
||||
|
||||
```bash
|
||||
tar -czf frontend.tar.gz -C artifacts/frontend .
|
||||
scp frontend.tar.gz root@host:/var/www/ecom-frontend/
|
||||
# 在服务器解压到 frontend 子目录
|
||||
scp frontend.tar.gz user@host:/opt/ecom-dmb-test/
|
||||
ssh user@host 'cd /opt/ecom-dmb-test && rm -rf index && mkdir index && tar -xzf frontend.tar.gz -C index && rm frontend.tar.gz'
|
||||
```
|
||||
|
||||
Nginx 站点根目录一般为 `/opt/ecom-dmb-{env}/index`(如 test → `/opt/ecom-dmb-test/index`)。
|
||||
|
||||
### 3. 重启容器
|
||||
|
||||
```bash
|
||||
# 后端 test 环境
|
||||
ssh root@192.168.3.101
|
||||
cd /opt/ecom-backend
|
||||
ssh user@192.168.3.101
|
||||
cd /opt/ecom-dmb/backend/test
|
||||
docker compose -p ecom-backend-test restart ecom-backend-test
|
||||
```
|
||||
|
||||
@@ -115,16 +145,15 @@ docker compose -p ecom-backend-test restart ecom-backend-test
|
||||
|
||||
使用 TUI **自动部署** 或 **上传产物并重启** 即可自动完成:
|
||||
|
||||
1. 上传产物到 `{deployPath}/{service}/data/`
|
||||
1. 上传产物到 `{deployPath}/ecom-dmb/{service}/{env}/app/`
|
||||
2. 构建运行时 Dockerfile 镜像
|
||||
3. 生成 docker-compose.yml 并启动容器
|
||||
3. 在同目录生成 docker-compose.yml 并启动容器
|
||||
|
||||
若需手动初始化后端运行时镜像:
|
||||
|
||||
```bash
|
||||
cd /opt/ecom-backend/backend
|
||||
cd /opt/ecom-dmb/backend/test
|
||||
docker build -t ecom-backend-test:latest .
|
||||
cd /opt/ecom-backend
|
||||
docker compose -p ecom-backend-test up -d ecom-backend-test
|
||||
```
|
||||
|
||||
|
||||
@@ -3,38 +3,78 @@
|
||||
"services": {
|
||||
"frontend": {
|
||||
"test": {
|
||||
"server": { "host": "192.168.3.100", "port": 22, "username": "root", "deployPath": "/var/www/ecom-frontend", "authType": "password" },
|
||||
"server": {
|
||||
"host": "192.168.3.101",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt/1panel/www/sites",
|
||||
"authType": "key",
|
||||
"keyPath": "/home/qinghua/.ssh/ecom-deploy"
|
||||
},
|
||||
"port": 80,
|
||||
"extractDir": "/var/www/ecom-frontend"
|
||||
"staticDeploy": true
|
||||
},
|
||||
"pre": {
|
||||
"server": { "host": "192.168.3.100", "port": 22, "username": "root", "deployPath": "/var/www/ecom-frontend", "authType": "password" },
|
||||
"server": {
|
||||
"host": "192.168.3.101",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt/1panel/www/sites",
|
||||
"authType": "key",
|
||||
"keyPath": "/home/qinghua/.ssh/ecom-deploy"
|
||||
},
|
||||
"port": 80,
|
||||
"extractDir": "/var/www/ecom-frontend"
|
||||
"staticDeploy": true
|
||||
},
|
||||
"prod": {
|
||||
"server": { "host": "frontend.prod.com", "port": 22, "username": "root", "deployPath": "/var/www/ecom-frontend", "authType": "password" },
|
||||
"server": {
|
||||
"host": "frontend.prod.com",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt/1panel/www/sites",
|
||||
"authType": "password"
|
||||
},
|
||||
"port": 80,
|
||||
"extractDir": "/var/www/ecom-frontend"
|
||||
"staticDeploy": true
|
||||
}
|
||||
},
|
||||
"backend": {
|
||||
"test": {
|
||||
"server": { "host": "192.168.3.100", "port": 22, "username": "root", "deployPath": "/opt/ecom-backend", "authType": "password" },
|
||||
"server": {
|
||||
"host": "192.168.3.101",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt",
|
||||
"authType": "key",
|
||||
"keyPath": "/home/qinghua/.ssh/ecom-deploy"
|
||||
},
|
||||
"port": 8101,
|
||||
"springProfile": "test",
|
||||
"dbUrl": "",
|
||||
"redisUrl": ""
|
||||
},
|
||||
"pre": {
|
||||
"server": { "host": "192.168.3.100", "port": 22, "username": "root", "deployPath": "/opt/ecom-backend", "authType": "password" },
|
||||
"server": {
|
||||
"host": "192.168.3.101",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt",
|
||||
"authType": "key",
|
||||
"keyPath": "/home/qinghua/.ssh/ecom-deploy"
|
||||
},
|
||||
"port": 8102,
|
||||
"springProfile": "pre",
|
||||
"dbUrl": "",
|
||||
"redisUrl": ""
|
||||
},
|
||||
"prod": {
|
||||
"server": { "host": "backend.prod.com", "port": 22, "username": "root", "deployPath": "/opt/ecom-backend", "authType": "password" },
|
||||
"server": {
|
||||
"host": "backend.prod.com",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt",
|
||||
"authType": "password"
|
||||
},
|
||||
"port": 8103,
|
||||
"springProfile": "prod",
|
||||
"dbUrl": "",
|
||||
@@ -43,23 +83,43 @@
|
||||
},
|
||||
"license": {
|
||||
"test": {
|
||||
"server": { "host": "192.168.3.100", "port": 22, "username": "root", "deployPath": "/opt/ecom-license", "authType": "password" },
|
||||
"server": {
|
||||
"host": "192.168.3.100",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt",
|
||||
"authType": "key",
|
||||
"keyPath": "/home/qinghua/.ssh/ecom-deploy"
|
||||
},
|
||||
"port": 3100,
|
||||
"nodeEnv": "test",
|
||||
"licenseDbUrl": ""
|
||||
},
|
||||
"pre": {
|
||||
"server": { "host": "192.168.3.100", "port": 22, "username": "root", "deployPath": "/opt/ecom-license", "authType": "password" },
|
||||
"server": {
|
||||
"host": "192.168.3.100",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt",
|
||||
"authType": "key",
|
||||
"keyPath": "/home/qinghua/.ssh/ecom-deploy"
|
||||
},
|
||||
"port": 3100,
|
||||
"nodeEnv": "pre",
|
||||
"licenseDbUrl": ""
|
||||
},
|
||||
"prod": {
|
||||
"server": { "host": "license.prod.com", "port": 22, "username": "root", "deployPath": "/opt/ecom-license", "authType": "password" },
|
||||
"server": {
|
||||
"host": "license.prod.com",
|
||||
"port": 22,
|
||||
"username": "root",
|
||||
"deployPath": "/opt",
|
||||
"authType": "password"
|
||||
},
|
||||
"port": 3100,
|
||||
"nodeEnv": "prod",
|
||||
"licenseDbUrl": ""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,24 +66,24 @@ export function assertArtifactsExist(service: Service): void {
|
||||
}
|
||||
}
|
||||
|
||||
/** 打包并上传目录到远程 data 目录 */
|
||||
/** 打包并上传目录到远程 app 目录 */
|
||||
export async function uploadArtifactDirectory(
|
||||
conn: Client,
|
||||
localDir: string,
|
||||
remoteDataDir: string,
|
||||
remoteAppDir: string,
|
||||
): Promise<void> {
|
||||
await ensureRemoteDir(conn, remoteDataDir);
|
||||
const parentDir = remoteDataDir.replace(/\/data\/?$/, '');
|
||||
await ensureRemoteDir(conn, remoteAppDir);
|
||||
const parentDir = remoteAppDir.replace(/\/app\/?$/, '');
|
||||
const tmpTar = getLocalTempPath(`ecom-upload-${Date.now()}.tar.gz`);
|
||||
const remoteTar = `${parentDir}/upload.tar.gz`;
|
||||
|
||||
printInfo(`上传产物到 ${remoteDataDir} ...`);
|
||||
printInfo(`上传产物到 ${remoteAppDir} ...`);
|
||||
createTarGz(localDir, tmpTar);
|
||||
await uploadFile(conn, tmpTar, remoteTar);
|
||||
|
||||
const { code, stderr } = await execRemote(
|
||||
conn,
|
||||
`mkdir -p "${remoteDataDir}" && rm -rf "${remoteDataDir}"/* && tar -xzf "${remoteTar}" -C "${remoteDataDir}" && rm -f "${remoteTar}"`,
|
||||
`mkdir -p "${remoteAppDir}" && rm -rf "${remoteAppDir}"/* && tar -xzf "${remoteTar}" -C "${remoteAppDir}" && rm -f "${remoteTar}"`,
|
||||
);
|
||||
if (code !== 0) throw new Error(`远程解压失败: ${stderr}`);
|
||||
}
|
||||
|
||||
+60
-13
@@ -14,12 +14,14 @@ export function getSshKeyPath(): string {
|
||||
return SSH_KEY_PATH;
|
||||
}
|
||||
|
||||
function defaultServiceConfig(host: string, port: number, extractDir?: string): ServiceEnvConfig {
|
||||
const deployPath = extractDir || '/opt/ecom-dmb';
|
||||
/** 远程统一根目录名:{deployPath}/ecom-dmb/{service}/{env} */
|
||||
export const REMOTE_APP_ROOT = 'ecom-dmb';
|
||||
|
||||
function defaultServiceConfig(host: string, port: number, options?: { staticDeploy?: boolean }): ServiceEnvConfig {
|
||||
return {
|
||||
server: { host, port: 22, username: 'root', deployPath, authType: 'password' },
|
||||
server: { host, port: 22, username: 'root', deployPath: '/opt', authType: 'password' },
|
||||
port,
|
||||
...(extractDir ? { extractDir } : {}),
|
||||
...(options?.staticDeploy ? { staticDeploy: true } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -28,9 +30,9 @@ function defaultConfig(): DeployConfig {
|
||||
currentEnv: 'test',
|
||||
services: {
|
||||
frontend: {
|
||||
test: defaultServiceConfig('192.168.1.100', 80, '/var/www/ecom-frontend'),
|
||||
pre: defaultServiceConfig('192.168.1.101', 80, '/var/www/ecom-frontend'),
|
||||
prod: defaultServiceConfig('frontend.prod.com', 80, '/var/www/ecom-frontend'),
|
||||
test: defaultServiceConfig('192.168.1.100', 80, { staticDeploy: true }),
|
||||
pre: defaultServiceConfig('192.168.1.101', 80, { staticDeploy: true }),
|
||||
prod: defaultServiceConfig('frontend.prod.com', 80, { staticDeploy: true }),
|
||||
},
|
||||
backend: {
|
||||
test: { ...defaultServiceConfig('192.168.1.100', 8101), springProfile: 'test', dbUrl: '', redisUrl: '' },
|
||||
@@ -100,14 +102,59 @@ export function getArtifactDir(service: Service): string {
|
||||
return join(ARTIFACTS_ROOT, service);
|
||||
}
|
||||
|
||||
/** 远程服务目录(含 Dockerfile、data 挂载点) */
|
||||
export function getRemoteServiceDir(cfg: ServiceEnvConfig, service: Service): string {
|
||||
return `${cfg.server.deployPath}/${service}`;
|
||||
/** 规范化部署根目录(去掉末尾斜杠) */
|
||||
export function normalizeDeployRoot(deployPath: string): string {
|
||||
return deployPath.replace(/\/+$/, '') || '/';
|
||||
}
|
||||
|
||||
/** 远程挂载目录,容器内映射为 /data */
|
||||
export function getRemoteDataDir(cfg: ServiceEnvConfig, service: Service): string {
|
||||
return `${getRemoteServiceDir(cfg, service)}/data`;
|
||||
/**
|
||||
* 远程环境部署目录(后端 / 授权):
|
||||
* {deployPath}/ecom-dmb/{service}/{env}
|
||||
* 例:/opt/ecom-dmb/backend/test
|
||||
*/
|
||||
export function resolveEnvDeployDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||||
return `${normalizeDeployRoot(cfg.server.deployPath)}/${REMOTE_APP_ROOT}/${service}/${env}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 前端静态部署目录(按环境后缀区分,不走 service 子路径):
|
||||
* {deployPath}/ecom-dmb-{env}/index
|
||||
* 例:/opt/ecom-dmb-test/index
|
||||
*/
|
||||
export function resolveFrontendDeployDir(cfg: ServiceEnvConfig, env: Environment): string {
|
||||
return `${normalizeDeployRoot(cfg.server.deployPath)}/${REMOTE_APP_ROOT}-${env}`;
|
||||
}
|
||||
|
||||
export function resolveFrontendIndexDir(cfg: ServiceEnvConfig, env: Environment): string {
|
||||
return `${resolveFrontendDeployDir(cfg, env)}/index`;
|
||||
}
|
||||
|
||||
/** 展示用:解析服务实际部署路径 */
|
||||
export function resolveServiceDeployPath(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||||
if (service === 'frontend') {
|
||||
return resolveFrontendIndexDir(cfg, env);
|
||||
}
|
||||
return resolveEnvDeployDir(cfg, service, env);
|
||||
}
|
||||
|
||||
/** 远程服务目录(含 Dockerfile、compose、data),等价于环境部署目录 */
|
||||
export function getRemoteServiceDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||||
return resolveEnvDeployDir(cfg, service, env);
|
||||
}
|
||||
|
||||
/** 远程业务产物目录(宿主 app/,挂载到容器 /app) */
|
||||
export function getRemoteAppDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||||
return `${resolveEnvDeployDir(cfg, service, env)}/app`;
|
||||
}
|
||||
|
||||
/** @deprecated 使用 getRemoteAppDir */
|
||||
export function getRemoteDataDir(cfg: ServiceEnvConfig, service: Service, env: Environment): string {
|
||||
return getRemoteAppDir(cfg, service, env);
|
||||
}
|
||||
|
||||
/** 是否仅静态部署(不走 Docker),兼容旧字段 extractDir */
|
||||
export function isStaticDeploy(cfg: ServiceEnvConfig): boolean {
|
||||
return cfg.staticDeploy === true || Boolean(cfg.extractDir);
|
||||
}
|
||||
|
||||
/** 与 application-{profile}.yml 中 server.port 保持一致 */
|
||||
|
||||
@@ -4,7 +4,7 @@ import { join } from 'path';
|
||||
|
||||
import { Client } from 'ssh2';
|
||||
import { syncBackendArtifact } from '../artifacts.js';
|
||||
import { getArtifactDir, getBackendDir, getRemoteDataDir } from '../config.js';
|
||||
import { getArtifactDir, getBackendDir, getRemoteAppDir } from '../config.js';
|
||||
import { findMainJar, resolveMavenCommand } from '../platform.js';
|
||||
import { ensureRemoteDir, uploadFile } from '../ssh.js';
|
||||
import { printInfo, printSuccess } from '../ui/render.js';
|
||||
@@ -32,10 +32,10 @@ export async function build(_cfg: ServiceEnvConfig): Promise<string> {
|
||||
}
|
||||
|
||||
/** 上传 artifacts/backend 到远程挂载目录 */
|
||||
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, _env: Environment): Promise<void> {
|
||||
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, env: Environment): Promise<void> {
|
||||
const artifactDir = getArtifactDir('backend');
|
||||
const remoteDataDir = getRemoteDataDir(cfg, 'backend');
|
||||
await ensureRemoteDir(conn, remoteDataDir);
|
||||
await uploadFile(conn, join(artifactDir, 'app.jar'), `${remoteDataDir}/app.jar`);
|
||||
printSuccess(`后端产物已上传到 ${remoteDataDir}`);
|
||||
const remoteAppDir = getRemoteAppDir(cfg, 'backend', env);
|
||||
await ensureRemoteDir(conn, remoteAppDir);
|
||||
await uploadFile(conn, join(artifactDir, 'app.jar'), `${remoteAppDir}/app.jar`);
|
||||
printSuccess(`后端产物已上传到 ${remoteAppDir}`);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { join } from 'path';
|
||||
|
||||
import { Client } from 'ssh2';
|
||||
import { syncFrontendArtifacts } from '../artifacts.js';
|
||||
import { getArtifactDir, getFrontendDir } from '../config.js';
|
||||
import { getArtifactDir, getFrontendDir, resolveFrontendDeployDir, resolveFrontendIndexDir } from '../config.js';
|
||||
import { createTarGz, getLocalTempPath } from '../platform.js';
|
||||
import { execRemote, uploadFile, ensureRemoteDir } from '../ssh.js';
|
||||
import { printInfo, printSuccess } from '../ui/render.js';
|
||||
@@ -28,34 +28,24 @@ export async function build(): Promise<string> {
|
||||
return artifactDir;
|
||||
}
|
||||
|
||||
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, _env: Environment): Promise<void> {
|
||||
/**
|
||||
* 上传静态产物到 {deployPath}/ecom-dmb-{env}/index/
|
||||
* 例:/opt/ecom-dmb-test/index
|
||||
*/
|
||||
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, env: Environment): Promise<void> {
|
||||
const buildDir = getArtifactDir('frontend');
|
||||
|
||||
if (cfg.extractDir) {
|
||||
await ensureRemoteDir(conn, cfg.extractDir);
|
||||
|
||||
const tmpTar = getLocalTempPath('ecom-frontend-build.tar.gz');
|
||||
createTarGz(buildDir, tmpTar);
|
||||
await uploadFile(conn, tmpTar, `${cfg.extractDir}/frontend.tar.gz`);
|
||||
|
||||
const { code, stderr } = await execRemote(
|
||||
conn,
|
||||
`cd "${cfg.extractDir}" && rm -rf frontend && mkdir frontend && tar -xzf frontend.tar.gz -C frontend && rm frontend.tar.gz`,
|
||||
);
|
||||
if (code !== 0) throw new Error(`解压失败: ${stderr}`);
|
||||
printSuccess(`前端文件已部署到 ${cfg.extractDir}/frontend`);
|
||||
return;
|
||||
}
|
||||
|
||||
const serviceDir = `${cfg.server.deployPath}/frontend`;
|
||||
await ensureRemoteDir(conn, serviceDir);
|
||||
const siteDir = resolveFrontendDeployDir(cfg, env);
|
||||
const indexDir = resolveFrontendIndexDir(cfg, env);
|
||||
await ensureRemoteDir(conn, siteDir);
|
||||
|
||||
const tmpTar = getLocalTempPath('ecom-frontend-build.tar.gz');
|
||||
createTarGz(buildDir, tmpTar);
|
||||
await uploadFile(conn, tmpTar, `${serviceDir}/build.tar.gz`);
|
||||
await uploadFile(conn, tmpTar, `${siteDir}/frontend.tar.gz`);
|
||||
|
||||
const { code, stderr } = await execRemote(conn, `cd "${serviceDir}" && tar -xzf build.tar.gz && rm build.tar.gz`);
|
||||
const { code, stderr } = await execRemote(
|
||||
conn,
|
||||
`cd "${siteDir}" && rm -rf index && mkdir index && tar -xzf frontend.tar.gz -C index && rm frontend.tar.gz`,
|
||||
);
|
||||
if (code !== 0) throw new Error(`解压失败: ${stderr}`);
|
||||
|
||||
printSuccess(`前端文件已部署到 ${serviceDir}`);
|
||||
printSuccess(`前端文件已部署到 ${indexDir}`);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
composeDown,
|
||||
} from '../docker.js';
|
||||
import { assertArtifactsExist, ensureArtifactsDirs } from '../artifacts.js';
|
||||
import { getArtifactDir, loadConfig, getServiceConfig, syncServerKeyAuth } from '../config.js';
|
||||
import { getArtifactDir, loadConfig, getServiceConfig, syncServerKeyAuth, isStaticDeploy } from '../config.js';
|
||||
import { printError, printInfo, printSuccess } from '../ui/render.js';
|
||||
import type { Environment, Service, ServiceEnvConfig } from '../types.js';
|
||||
|
||||
@@ -114,7 +114,7 @@ export async function deployArtifactsOnly(
|
||||
);
|
||||
|
||||
try {
|
||||
if (cfg.extractDir) {
|
||||
if (isStaticDeploy(cfg)) {
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
} else {
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
@@ -146,7 +146,7 @@ export async function buildAndDeployServices(
|
||||
);
|
||||
|
||||
try {
|
||||
if (cfg.extractDir) {
|
||||
if (isStaticDeploy(cfg)) {
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
} else {
|
||||
await deployService(conn, service, cfg, getArtifactDir(service), env);
|
||||
|
||||
@@ -4,7 +4,7 @@ import { join } from 'path';
|
||||
|
||||
import { Client } from 'ssh2';
|
||||
import { syncLicenseArtifacts, uploadArtifactDirectory } from '../artifacts.js';
|
||||
import { getArtifactDir, getLicenseDir, getRemoteDataDir } from '../config.js';
|
||||
import { getArtifactDir, getLicenseDir, getRemoteAppDir } from '../config.js';
|
||||
import { copyBuildArtifacts, getLocalTempPath } from '../platform.js';
|
||||
import { ensureRemoteDir } from '../ssh.js';
|
||||
import { printInfo, printSuccess } from '../ui/render.js';
|
||||
@@ -47,10 +47,10 @@ export async function build(): Promise<string> {
|
||||
}
|
||||
|
||||
/** 上传 artifacts/license 到远程挂载目录 */
|
||||
export async function deploy(conn: Client, cfg: ServiceEnvConfig, _buildDir: string, _env: Environment): Promise<void> {
|
||||
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}`);
|
||||
const remoteAppDir = getRemoteAppDir(cfg, 'license', env);
|
||||
await ensureRemoteDir(conn, remoteAppDir);
|
||||
await uploadArtifactDirectory(conn, artifactDir, remoteAppDir);
|
||||
printSuccess(`许可证产物已上传到 ${remoteAppDir}`);
|
||||
}
|
||||
|
||||
+13
-15
@@ -8,6 +8,7 @@ import {
|
||||
getLicenseDeployDir,
|
||||
getRemoteServiceDir,
|
||||
resolveBackendPort,
|
||||
resolveEnvDeployDir,
|
||||
resolveSpringProfile,
|
||||
} from './config.js';
|
||||
import { getLocalTempPath } from './platform.js';
|
||||
@@ -15,10 +16,11 @@ import { execRemote, ensureRemoteDir, uploadFile } from './ssh.js';
|
||||
import { printInfo, printSuccess } from './ui/render.js';
|
||||
import type { Environment, Service, ServiceEnvConfig } from './types.js';
|
||||
|
||||
/** 远程 compose 命令前缀 */
|
||||
/** 远程 compose 命令前缀(工作目录为环境部署目录) */
|
||||
function composeCmd(cfg: ServiceEnvConfig, env: Environment, service: Service): string {
|
||||
const { composeProject } = getDockerResourceNames(service, env);
|
||||
return `cd "${cfg.server.deployPath}" && docker compose -p ${composeProject}`;
|
||||
const envDir = resolveEnvDeployDir(cfg, service, env);
|
||||
return `cd "${envDir}" && docker compose -p ${composeProject}`;
|
||||
}
|
||||
|
||||
/** 桥接模式端口 */
|
||||
@@ -30,11 +32,6 @@ function resolveBridgePorts(service: Service, cfg: ServiceEnvConfig, env: Enviro
|
||||
return { hostPort: cfg.port, containerPort: cfg.port };
|
||||
}
|
||||
|
||||
/** 挂载目录(相对 deployPath) */
|
||||
function getVolumeMapping(service: Service): string {
|
||||
return `./${service}/data:/data:ro`;
|
||||
}
|
||||
|
||||
export function generateComposeFile(service: Service, cfg: ServiceEnvConfig, env: Environment): string {
|
||||
const { composeService, containerName, imageTag } = getDockerResourceNames(service, env);
|
||||
const { hostPort, containerPort } = resolveBridgePorts(service, cfg, env);
|
||||
@@ -64,7 +61,7 @@ export function generateComposeFile(service: Service, cfg: ServiceEnvConfig, env
|
||||
ports:
|
||||
- "${hostPort}:${containerPort}"
|
||||
volumes:
|
||||
- ${getVolumeMapping(service)}
|
||||
- ./app:/app:ro
|
||||
environment:
|
||||
${envBlock}
|
||||
restart: unless-stopped
|
||||
@@ -90,13 +87,14 @@ function readRuntimeDockerfile(service: Service): string {
|
||||
return readFileSync(dockerfileSrc, 'utf8');
|
||||
}
|
||||
|
||||
/** 上传运行时 Dockerfile 到远程服务目录 */
|
||||
/** 上传运行时 Dockerfile 到远程环境目录 */
|
||||
export async function uploadRuntimeDockerfile(
|
||||
conn: Client,
|
||||
service: Service,
|
||||
cfg: ServiceEnvConfig,
|
||||
env: Environment,
|
||||
): Promise<void> {
|
||||
const serviceDir = getRemoteServiceDir(cfg, service);
|
||||
const serviceDir = getRemoteServiceDir(cfg, service, env);
|
||||
await ensureRemoteDir(conn, serviceDir);
|
||||
const tmp = getLocalTempPath(`ecom-${service}-Dockerfile`);
|
||||
writeFileSync(tmp, readRuntimeDockerfile(service), 'utf8');
|
||||
@@ -118,7 +116,7 @@ export async function remoteBuildRuntimeImage(
|
||||
env: Environment,
|
||||
): Promise<void> {
|
||||
const { imageTag } = getDockerResourceNames(service, env);
|
||||
const serviceDir = getRemoteServiceDir(cfg, service);
|
||||
const serviceDir = getRemoteServiceDir(cfg, service, env);
|
||||
const { code, stdout, stderr } = await execRemote(
|
||||
conn,
|
||||
`cd "${serviceDir}" && docker build -t ${imageTag} . 2>&1`,
|
||||
@@ -135,11 +133,11 @@ export async function uploadComposeFile(
|
||||
cfg: ServiceEnvConfig,
|
||||
env: Environment,
|
||||
): Promise<void> {
|
||||
const deployPath = cfg.server.deployPath;
|
||||
const envDir = resolveEnvDeployDir(cfg, service, env);
|
||||
const tmpCompose = getLocalTempPath(`ecom-${service}-${env}-docker-compose.yml`);
|
||||
writeFileSync(tmpCompose, generateComposeFile(service, cfg, env), 'utf8');
|
||||
await ensureRemoteDir(conn, deployPath);
|
||||
await uploadFile(conn, tmpCompose, `${deployPath}/docker-compose.yml`);
|
||||
await ensureRemoteDir(conn, envDir);
|
||||
await uploadFile(conn, tmpCompose, `${envDir}/docker-compose.yml`);
|
||||
}
|
||||
|
||||
/** 首次启动容器 */
|
||||
@@ -188,7 +186,7 @@ export async function initServiceRuntime(
|
||||
env: Environment,
|
||||
): Promise<void> {
|
||||
printInfo(`${service} 首次部署,初始化运行时镜像...`);
|
||||
await uploadRuntimeDockerfile(conn, service, cfg);
|
||||
await uploadRuntimeDockerfile(conn, service, cfg, env);
|
||||
await uploadComposeFile(conn, service, cfg, env);
|
||||
await remoteBuildRuntimeImage(conn, service, cfg, env);
|
||||
await composeUp(conn, cfg, service, env);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
import { input, password as pwPrompt, select } from '@inquirer/prompts';
|
||||
|
||||
import { loadConfig, saveConfig, updateServiceServerConfig, getServiceConfig, getSshKeyPath, syncServerKeyAuth } from './config.js';
|
||||
import { loadConfig, saveConfig, updateServiceServerConfig, getServiceConfig, getSshKeyPath, syncServerKeyAuth, resolveServiceDeployPath } from './config.js';
|
||||
import {
|
||||
buildAndDeployServices,
|
||||
buildServicesOnly,
|
||||
@@ -61,12 +61,14 @@ async function selectServices(): Promise<Service[]> {
|
||||
return [selected as Service];
|
||||
}
|
||||
|
||||
function showServiceHosts(config: { services: Record<string, Record<string, { server: { host: string; deployPath: string } }>>; currentEnv: string }): string {
|
||||
function showServiceHosts(config: Awaited<ReturnType<typeof loadConfig>>): string {
|
||||
const env = config.currentEnv;
|
||||
const services: Service[] = ['frontend', 'backend', 'license'];
|
||||
return services.map((s) =>
|
||||
` ${SERVICE_LABELS[s]}: ${config.services[s][env].server.host}:${config.services[s][env].server.deployPath}`
|
||||
).join('\n');
|
||||
return services.map((s) => {
|
||||
const cfg = config.services[s][env];
|
||||
const resolved = resolveServiceDeployPath(cfg, s, env);
|
||||
return ` ${SERVICE_LABELS[s]}: ${cfg.server.host} → ${resolved}`;
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
async function configureService() {
|
||||
@@ -80,7 +82,8 @@ async function configureService() {
|
||||
printBox(`${SERVICE_LABELS[service]} · ${ENV_LABELS[env]} 配置`, [
|
||||
`服务器: ${srv.host}:${srv.port}`,
|
||||
`用户: ${srv.username}`,
|
||||
`部署目录: ${srv.deployPath}`,
|
||||
`部署根目录: ${srv.deployPath}`,
|
||||
`实际目录: ${resolveServiceDeployPath(cfg, service, env)}`,
|
||||
`端口: ${cfg.port}`,
|
||||
`认证方式: ${srv.authType === 'key' ? '密钥' : '密码'}`,
|
||||
].join('\n'));
|
||||
@@ -92,7 +95,7 @@ async function configureService() {
|
||||
{ name: '修改端口', value: 'port' },
|
||||
{ name: '修改服务端口', value: 'svcPort' },
|
||||
{ name: '修改用户名', value: 'username' },
|
||||
{ name: '修改部署目录', value: 'deployPath' },
|
||||
{ name: '修改部署根目录', value: 'deployPath' },
|
||||
{ name: '测试 SSH 连接', value: 'test' },
|
||||
{ name: '配置 SSH 密钥 (首次免密)', value: 'setupKey' },
|
||||
{ name: '返回', value: 'back' },
|
||||
@@ -122,7 +125,13 @@ async function configureService() {
|
||||
break;
|
||||
}
|
||||
case 'deployPath': {
|
||||
const d = await input({ message: '部署目录', default: srv.deployPath });
|
||||
const hint = service === 'frontend'
|
||||
? `部署根目录(实际为 根目录/ecom-dmb-${env}/index)`
|
||||
: `部署根目录(实际为 根目录/ecom-dmb/${service}/${env})`;
|
||||
const d = await input({
|
||||
message: hint,
|
||||
default: srv.deployPath,
|
||||
});
|
||||
await updateServiceServerConfig(service, env, { deployPath: d });
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface ServerConfig {
|
||||
host: string;
|
||||
port: number;
|
||||
username: string;
|
||||
/** 部署根目录;实际路径为 {deployPath}/ecom-dmb/{service}/{env} */
|
||||
deployPath: string;
|
||||
authType: 'password' | 'key';
|
||||
keyPath?: string;
|
||||
@@ -19,6 +20,9 @@ export interface ServiceEnvConfig {
|
||||
dbUrl?: string;
|
||||
redisUrl?: string;
|
||||
licenseDbUrl?: string;
|
||||
/** 仅静态文件部署(不启动 Docker),前端常用 */
|
||||
staticDeploy?: boolean;
|
||||
/** @deprecated 改用 staticDeploy;若存在则仍视为静态部署 */
|
||||
extractDir?: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# 运行时镜像:业务 JAR 通过 volume 挂载 /data/app.jar,更新部署无需重建镜像
|
||||
# 运行时镜像:业务 JAR 通过 volume 挂载 /app/app.jar,更新部署无需重建镜像
|
||||
ARG BASE_IMAGE=docker.m.daocloud.io/library/eclipse-temurin:17-jre-alpine
|
||||
FROM ${BASE_IMAGE}
|
||||
|
||||
@@ -8,4 +8,4 @@ RUN addgroup -S app && adduser -S app -G app
|
||||
USER app
|
||||
# 实际端口由 Spring profile 决定(test:8101 / pre:8102 / prod:8103)
|
||||
EXPOSE 8101 8102 8103
|
||||
ENTRYPOINT ["java", "-jar", "/data/app.jar"]
|
||||
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
|
||||
|
||||
@@ -5,9 +5,9 @@ server:
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://192.168.3.100:3306/ecom_dmb_test?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
|
||||
username: ecom_dmb_test
|
||||
password: ecom_dmb_test
|
||||
url: jdbc:mysql://192.168.3.100:3306/ecom_dmb_dev?characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
|
||||
username: ecom_dmb_dev
|
||||
password: ecom_dmb_dev
|
||||
data:
|
||||
redis:
|
||||
host: 192.168.3.100
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# 运行时镜像:业务代码通过 volume 挂载 /data,更新部署无需重建镜像
|
||||
# 运行时镜像:业务代码通过 volume 挂载 /app,更新部署无需重建镜像
|
||||
ARG BASE_IMAGE=docker.m.daocloud.io/library/node:22-alpine
|
||||
FROM ${BASE_IMAGE}
|
||||
|
||||
WORKDIR /data
|
||||
WORKDIR /app
|
||||
RUN addgroup -S app && adduser -S app -G app && corepack enable
|
||||
|
||||
USER app
|
||||
|
||||
Reference in New Issue
Block a user