@noahyu/cd-cli 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +162 -42
- package/dist/src/index.js +35 -37
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 简易项目部署工具
|
|
2
2
|
|
|
3
3
|
一个简易的部署工具,支持版本管理和零停机部署。
|
|
4
4
|
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
- 🔄 零停机部署:使用软链接实现原子性切换
|
|
10
10
|
- ⏪ 快速回滚:一键回滚到任意历史版本
|
|
11
11
|
- 📝 版本列表:查看服务器上所有已部署版本
|
|
12
|
-
- 🔒
|
|
12
|
+
- 🔒 SSH 认证:支持密码、密钥
|
|
13
13
|
- 🔄 PM2 集成:自动重启 PM2 应用
|
|
14
14
|
- 📝 配置灵活:支持部署前后执行自定义命令
|
|
15
15
|
- 🎯 文件过滤:支持排除不需要的文件
|
|
@@ -92,65 +92,143 @@ PM2 配置始终指向软链接 `.output/server/index.mjs`,这样切换版本
|
|
|
92
92
|
```javascript
|
|
93
93
|
// pcli-cd 部署配置文件
|
|
94
94
|
export default {
|
|
95
|
-
|
|
95
|
+
/** 构建命令 (可选):构建项目 */
|
|
96
96
|
buildCommand: 'npm run build',
|
|
97
|
-
|
|
98
|
-
// 构建输出目录
|
|
97
|
+
/** 构建输出目录 */
|
|
99
98
|
buildDir: '.output',
|
|
100
|
-
|
|
101
|
-
// 版本号 (可选,不指定会在部署时询问)
|
|
99
|
+
/** 版本号 (可选,不指定会在部署时询问) */
|
|
102
100
|
version: 'v1.0.0',
|
|
103
|
-
|
|
104
|
-
// 服务器配置
|
|
101
|
+
/** 服务器配置 */
|
|
105
102
|
server: {
|
|
103
|
+
/** 服务器地址 */
|
|
106
104
|
host: '192.168.1.100',
|
|
105
|
+
/** 端口号 */
|
|
107
106
|
port: 22,
|
|
107
|
+
/** 用户名 */
|
|
108
108
|
username: 'root',
|
|
109
|
-
|
|
110
|
-
|
|
109
|
+
// SSH 认证方式(优先级:privateKey > privateKeyPath > password)
|
|
110
|
+
/** 私钥 */
|
|
111
|
+
privateKey: '-----BEGIN OPENSSH PRIVATE KEY-----\n...\n-----END OPENSSH PRIVATE KEY-----',
|
|
112
|
+
/** 私钥文件路径 */
|
|
113
|
+
privateKeyPath: '~/.ssh/id_rsa',
|
|
114
|
+
/** 密码 */
|
|
115
|
+
password: 'your-password',
|
|
116
|
+
/** 部署目录 */
|
|
111
117
|
deployPath: '/var/www/your-app',
|
|
112
118
|
},
|
|
113
|
-
|
|
114
|
-
// PM2 配置 (可选)
|
|
119
|
+
/** PM2 配置 (可选) */
|
|
115
120
|
pm2: {
|
|
121
|
+
/** 进程名称 */
|
|
116
122
|
appName: 'your-app-name',
|
|
123
|
+
/** 是否立即重启 */
|
|
117
124
|
restart: true,
|
|
118
125
|
},
|
|
119
126
|
|
|
120
|
-
|
|
121
|
-
|
|
127
|
+
/**
|
|
128
|
+
* 排除的文件 (可选) - 作用于构建产物目录
|
|
129
|
+
*
|
|
130
|
+
* 请根据构建输出的实际内容谨慎配置
|
|
131
|
+
*/
|
|
122
132
|
excludeFiles: [
|
|
123
133
|
// '**/*.map', // Source Map 文件
|
|
124
134
|
// '**/.DS_Store', // macOS 系统文件
|
|
125
135
|
// '**/Thumbs.db' // Windows 缩略图缓存
|
|
126
136
|
],
|
|
127
|
-
|
|
128
|
-
// 部署前命令 (可选)
|
|
137
|
+
/** 部署前命令 (可选) */
|
|
129
138
|
beforeDeploy: ['npm run test'],
|
|
130
|
-
|
|
131
|
-
// 部署后命令 (可选)
|
|
139
|
+
/** 部署后命令 (可选) */
|
|
132
140
|
afterDeploy: ['npm install --production'],
|
|
133
141
|
}
|
|
134
142
|
```
|
|
135
143
|
|
|
144
|
+
## SSH 认证配置
|
|
145
|
+
|
|
146
|
+
工具支持三种 SSH 认证方式,优先级为:`privateKey` > `privateKeyPath` > `password`
|
|
147
|
+
|
|
148
|
+
### 方式一:私钥内容(推荐用于 CI/CD)
|
|
149
|
+
|
|
150
|
+
适用于 CI/CD 环境,将私钥内容作为环境变量传入:
|
|
151
|
+
|
|
152
|
+
```javascript
|
|
153
|
+
export default {
|
|
154
|
+
server: {
|
|
155
|
+
host: '192.168.1.100',
|
|
156
|
+
username: 'root',
|
|
157
|
+
privateKey: process.env.SSH_PRIVATE_KEY, // 从环境变量读取
|
|
158
|
+
deployPath: '/var/www/app',
|
|
159
|
+
},
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### 方式二:私钥文件路径(推荐用于本地开发)
|
|
164
|
+
|
|
165
|
+
适用于本地开发环境,使用本地私钥文件:
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
export default {
|
|
169
|
+
server: {
|
|
170
|
+
host: '192.168.1.100',
|
|
171
|
+
username: 'root',
|
|
172
|
+
privateKeyPath: '~/.ssh/id_rsa',
|
|
173
|
+
deployPath: '/var/www/app',
|
|
174
|
+
},
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
- `~/.ssh/id_rsa` - 用户主目录下的私钥
|
|
179
|
+
- `/home/user/.ssh/id_ed25519` - 绝对路径
|
|
180
|
+
- `./keys/deploy_key` - 相对路径
|
|
181
|
+
|
|
182
|
+
### 方式三:密码认证
|
|
183
|
+
|
|
184
|
+
适用于简单测试环境(生产环境不推荐):
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
export default {
|
|
188
|
+
server: {
|
|
189
|
+
host: '192.168.1.100',
|
|
190
|
+
username: 'root',
|
|
191
|
+
password: 'your-password',
|
|
192
|
+
deployPath: '/var/www/app',
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### 优先级说明
|
|
198
|
+
|
|
199
|
+
当配置了多种认证方式时,工具会按以下优先级选择:
|
|
200
|
+
|
|
201
|
+
1. **privateKey**(私钥内容)- 最高优先级
|
|
202
|
+
2. **privateKeyPath**(私钥文件路径)- 中等优先级
|
|
203
|
+
3. **password**(密码)- 最低优先级
|
|
204
|
+
|
|
205
|
+
### 安全建议
|
|
206
|
+
|
|
207
|
+
- ✅ **生产环境**:使用私钥认证(`privateKey` 或 `privateKeyPath`)
|
|
208
|
+
- ✅ **CI/CD**:使用 `privateKey` + 环境变量
|
|
209
|
+
- ✅ **本地开发**:使用 `privateKeyPath`
|
|
210
|
+
- ⚠️ **测试环境**:可以使用密码认证
|
|
211
|
+
- ❌ **避免**:在配置文件中硬编码密码或私钥内容
|
|
212
|
+
|
|
136
213
|
## 配置选项
|
|
137
214
|
|
|
138
|
-
| 选项
|
|
139
|
-
|
|
|
140
|
-
| `buildCommand`
|
|
141
|
-
| `buildDir`
|
|
142
|
-
| `version`
|
|
143
|
-
| `server.host`
|
|
144
|
-
| `server.port`
|
|
145
|
-
| `server.username`
|
|
146
|
-
| `server.password`
|
|
147
|
-
| `server.privateKey`
|
|
148
|
-
| `server.
|
|
149
|
-
| `
|
|
150
|
-
| `pm2.
|
|
151
|
-
| `
|
|
152
|
-
| `
|
|
153
|
-
| `
|
|
215
|
+
| 选项 | 类型 | 必填 | 说明 |
|
|
216
|
+
| ----------------------- | -------- | ---- | ---------------------------------- |
|
|
217
|
+
| `buildCommand` | string | 否 | 构建命令,如 "npm run build" |
|
|
218
|
+
| `buildDir` | string | 是 | 构建输出目录,如 ".output" |
|
|
219
|
+
| `version` | string | 否 | 版本号,不指定会在部署时询问 |
|
|
220
|
+
| `server.host` | string | 是 | 服务器地址 |
|
|
221
|
+
| `server.port` | number | 否 | SSH 端口,默认 22 |
|
|
222
|
+
| `server.username` | string | 是 | 用户名 |
|
|
223
|
+
| `server.password` | string | 否 | SSH 密码 |
|
|
224
|
+
| `server.privateKey` | string | 否 | SSH 私钥内容(字符串),优先级最高 |
|
|
225
|
+
| `server.privateKeyPath` | string | 否 | SSH 私钥文件路径 |
|
|
226
|
+
| `server.deployPath` | string | 是 | 服务器部署路径 |
|
|
227
|
+
| `pm2.appName` | string | 否 | PM2 应用名称 |
|
|
228
|
+
| `pm2.restart` | boolean | 否 | 是否重启 PM2 应用 |
|
|
229
|
+
| `excludeFiles` | string[] | 否 | 排除的文件模式(相对于构建目录) |
|
|
230
|
+
| `beforeDeploy` | string[] | 否 | 部署前执行的命令 |
|
|
231
|
+
| `afterDeploy` | string[] | 否 | 部署后执行的命令 |
|
|
154
232
|
|
|
155
233
|
## 命令详解
|
|
156
234
|
|
|
@@ -212,7 +290,7 @@ export default {
|
|
|
212
290
|
server: {
|
|
213
291
|
host: 'your-server.com',
|
|
214
292
|
username: 'root',
|
|
215
|
-
|
|
293
|
+
privateKeyPath: '~/.ssh/id_rsa', // 推荐使用私钥认证
|
|
216
294
|
deployPath: '/var/www/nuxt-app',
|
|
217
295
|
},
|
|
218
296
|
pm2: {
|
|
@@ -333,12 +411,16 @@ pcli-cd rollback
|
|
|
333
411
|
|
|
334
412
|
- **全局安装后首次使用**:安装后可在任意目录使用 `pcli-cd` 命令
|
|
335
413
|
- **配置文件位置**:每个项目根目录需要有 `pcli-cd.config.js` 配置文件
|
|
336
|
-
-
|
|
337
|
-
-
|
|
338
|
-
- 私钥文件需要有正确的权限 (600)
|
|
339
|
-
-
|
|
340
|
-
-
|
|
341
|
-
-
|
|
414
|
+
- **SSH 认证**:
|
|
415
|
+
- 支持 OpenSSH 格式的 RSA、ECDSA、Ed25519 私钥
|
|
416
|
+
- 私钥文件需要有正确的权限 (600)
|
|
417
|
+
- 生产环境建议使用私钥认证而非密码
|
|
418
|
+
- CI/CD 环境推荐使用 `privateKey` + 环境变量
|
|
419
|
+
- **服务器要求**:
|
|
420
|
+
- 确保服务器已安装 `unzip` 命令
|
|
421
|
+
- 如果使用 PM2,确保服务器已安装 PM2
|
|
422
|
+
- **PM2 配置**:PM2 配置文件中的启动路径应该指向软链接而非具体版本目录
|
|
423
|
+
- **版本管理**:部署过程中会自动清理旧版本,默认保留最近3个版本
|
|
342
424
|
|
|
343
425
|
## 故障排除
|
|
344
426
|
|
|
@@ -362,6 +444,44 @@ ls pcli-cd.config.js
|
|
|
362
444
|
pcli-cd init
|
|
363
445
|
```
|
|
364
446
|
|
|
447
|
+
### SSH 连接失败
|
|
448
|
+
|
|
449
|
+
**1. 私钥文件不存在**
|
|
450
|
+
|
|
451
|
+
```bash
|
|
452
|
+
# 检查私钥文件是否存在
|
|
453
|
+
ls -la ~/.ssh/id_rsa
|
|
454
|
+
|
|
455
|
+
# 检查私钥文件权限
|
|
456
|
+
chmod 600 ~/.ssh/id_rsa
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
**2. 私钥格式错误**
|
|
460
|
+
|
|
461
|
+
```bash
|
|
462
|
+
# 检查私钥格式(应该以此开头)
|
|
463
|
+
head -1 ~/.ssh/id_rsa
|
|
464
|
+
# RSA: -----BEGIN RSA PRIVATE KEY-----
|
|
465
|
+
# OpenSSH: -----BEGIN OPENSSH PRIVATE KEY-----
|
|
466
|
+
# ECDSA: -----BEGIN EC PRIVATE KEY-----
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
**3. SSH 认证配置问题**
|
|
470
|
+
|
|
471
|
+
- 确保至少配置了 `password`、`privateKey` 或 `privateKeyPath` 之一
|
|
472
|
+
- 检查私钥文件路径是否正确
|
|
473
|
+
- 验证服务器用户名和地址是否正确
|
|
474
|
+
|
|
475
|
+
**4. 网络连接问题**
|
|
476
|
+
|
|
477
|
+
```bash
|
|
478
|
+
# 测试 SSH 连接
|
|
479
|
+
ssh -p 22 root@your-server.com
|
|
480
|
+
|
|
481
|
+
# 测试指定私钥连接
|
|
482
|
+
ssh -i ~/.ssh/id_rsa -p 22 root@your-server.com
|
|
483
|
+
```
|
|
484
|
+
|
|
365
485
|
## License
|
|
366
486
|
|
|
367
487
|
MIT
|
package/dist/src/index.js
CHANGED
|
@@ -71,14 +71,7 @@ async function deploy(config) {
|
|
|
71
71
|
await createZip(buildPath, zipPath, config.excludeFiles);
|
|
72
72
|
spinner.succeed('文件压缩完成');
|
|
73
73
|
spinner.start('正在连接服务器...');
|
|
74
|
-
const ssh =
|
|
75
|
-
await ssh.connect({
|
|
76
|
-
host: config.server.host,
|
|
77
|
-
port: config.server.port || 22,
|
|
78
|
-
username: config.server.username,
|
|
79
|
-
password: config.server.password,
|
|
80
|
-
privateKey: config.server.privateKey,
|
|
81
|
-
});
|
|
74
|
+
const ssh = await createSSHConnection(config.server);
|
|
82
75
|
spinner.succeed('服务器连接成功');
|
|
83
76
|
const versionDirName = `${buildDirName}-${version}`;
|
|
84
77
|
const versionPath = join(config.server.deployPath, versionDirName);
|
|
@@ -225,10 +218,9 @@ async function initConfig() {
|
|
|
225
218
|
validate: (input) => input.trim() !== '' || '请输入用户名',
|
|
226
219
|
},
|
|
227
220
|
{
|
|
228
|
-
type: '
|
|
229
|
-
name: '
|
|
230
|
-
message: '
|
|
231
|
-
mask: '*',
|
|
221
|
+
type: 'input',
|
|
222
|
+
name: 'privateKeyPath',
|
|
223
|
+
message: '私钥路径 (留空稍后填写):',
|
|
232
224
|
},
|
|
233
225
|
{
|
|
234
226
|
type: 'input',
|
|
@@ -249,7 +241,7 @@ async function initConfig() {
|
|
|
249
241
|
host: answers.host,
|
|
250
242
|
port: parseInt(answers.port),
|
|
251
243
|
username: answers.username,
|
|
252
|
-
|
|
244
|
+
privateKeyPath: answers.privateKeyPath || undefined,
|
|
253
245
|
deployPath: answers.deployPath,
|
|
254
246
|
},
|
|
255
247
|
excludeFiles: [],
|
|
@@ -283,14 +275,7 @@ async function listVersions(options) {
|
|
|
283
275
|
const spinner = ora('正在获取版本列表...');
|
|
284
276
|
spinner.start();
|
|
285
277
|
try {
|
|
286
|
-
const ssh =
|
|
287
|
-
await ssh.connect({
|
|
288
|
-
host: config.server.host,
|
|
289
|
-
port: config.server.port || 22,
|
|
290
|
-
username: config.server.username,
|
|
291
|
-
password: config.server.password,
|
|
292
|
-
privateKey: config.server.privateKey,
|
|
293
|
-
});
|
|
278
|
+
const ssh = await createSSHConnection(config.server);
|
|
294
279
|
const buildDirName = config.buildDir.split('/').pop() || 'build';
|
|
295
280
|
const currentLinkPath = join(config.server.deployPath, buildDirName);
|
|
296
281
|
const currentResult = await ssh.execCommand(`readlink ${currentLinkPath}`);
|
|
@@ -356,14 +341,7 @@ async function rollbackVersion(options) {
|
|
|
356
341
|
const buildDirName = config.buildDir.split('/').pop() || 'build';
|
|
357
342
|
let targetVersion = options.version;
|
|
358
343
|
if (!targetVersion) {
|
|
359
|
-
const ssh =
|
|
360
|
-
await ssh.connect({
|
|
361
|
-
host: config.server.host,
|
|
362
|
-
port: config.server.port || 22,
|
|
363
|
-
username: config.server.username,
|
|
364
|
-
password: config.server.password,
|
|
365
|
-
privateKey: config.server.privateKey,
|
|
366
|
-
});
|
|
344
|
+
const ssh = await createSSHConnection(config.server);
|
|
367
345
|
const result = await ssh.execCommand(`find ${config.server.deployPath} -maxdepth 1 -type d -name "${buildDirName}-*" | sort -V`);
|
|
368
346
|
if (result.code !== 0) {
|
|
369
347
|
console.log(chalk.red('❌ 无法获取版本列表'));
|
|
@@ -400,14 +378,7 @@ async function performRollback(config, targetVersion, buildDirName) {
|
|
|
400
378
|
const spinner = ora();
|
|
401
379
|
try {
|
|
402
380
|
spinner.start('正在连接服务器...');
|
|
403
|
-
const ssh =
|
|
404
|
-
await ssh.connect({
|
|
405
|
-
host: config.server.host,
|
|
406
|
-
port: config.server.port || 22,
|
|
407
|
-
username: config.server.username,
|
|
408
|
-
password: config.server.password,
|
|
409
|
-
privateKey: config.server.privateKey,
|
|
410
|
-
});
|
|
381
|
+
const ssh = await createSSHConnection(config.server);
|
|
411
382
|
spinner.succeed('服务器连接成功');
|
|
412
383
|
const versionDirName = `${buildDirName}-${targetVersion}`;
|
|
413
384
|
const versionPath = join(config.server.deployPath, versionDirName);
|
|
@@ -450,5 +421,32 @@ async function performRollback(config, targetVersion, buildDirName) {
|
|
|
450
421
|
process.exit(1);
|
|
451
422
|
}
|
|
452
423
|
}
|
|
424
|
+
async function createSSHConnection(server) {
|
|
425
|
+
const ssh = new NodeSSH();
|
|
426
|
+
const connectConfig = {
|
|
427
|
+
host: server.host,
|
|
428
|
+
port: server.port || 22,
|
|
429
|
+
username: server.username,
|
|
430
|
+
};
|
|
431
|
+
if (server.privateKey) {
|
|
432
|
+
connectConfig.privateKey = server.privateKey;
|
|
433
|
+
}
|
|
434
|
+
else if (server.privateKeyPath) {
|
|
435
|
+
connectConfig.privateKeyPath = server.privateKeyPath;
|
|
436
|
+
}
|
|
437
|
+
else if (server.password) {
|
|
438
|
+
connectConfig.password = server.password;
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
throw new Error('SSH 认证配置错误:必须提供 password、privateKey 或 privateKeyPath 之一');
|
|
442
|
+
}
|
|
443
|
+
try {
|
|
444
|
+
await ssh.connect(connectConfig);
|
|
445
|
+
return ssh;
|
|
446
|
+
}
|
|
447
|
+
catch (error) {
|
|
448
|
+
throw new Error(`SSH 连接失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
453
451
|
|
|
454
452
|
export { deployCommand, initConfig, listVersions, rollbackVersion };
|