@taole/deploy-helper 0.7.3 → 0.7.4-beta.2
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/index.mjs +96 -50
- package/lib/offlinePkg.mjs +16 -8
- package/lib/pipelineApi.mjs +1 -0
- package/lib/tagRelease.mjs +225 -0
- package/lib/util.mjs +64 -39
- package/package.json +3 -2
package/index.mjs
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import { Client } from 'node-scp'
|
|
4
3
|
import fs from 'fs';
|
|
5
4
|
import { join, basename, dirname } from "path";
|
|
6
5
|
import { simpleGit } from 'simple-git';
|
|
7
|
-
import { homedir } from 'os'
|
|
8
6
|
import { runPipeline, checkYunxiaoToken, triggerPipeline } from './lib/pipelineApi.mjs';
|
|
9
|
-
import { setDebug, log, getUserDeployHelperConfig } from './lib/util.mjs';
|
|
7
|
+
import { setDebug, log, getUserDeployHelperConfig, TEST_SERVER_HOST, getScpClient } from './lib/util.mjs';
|
|
10
8
|
import path from 'path';
|
|
11
9
|
import { fileURLToPath } from 'url';
|
|
12
10
|
import { checkOfflinePkg, syncApi as syncOfflinePkgApi } from './lib/offlinePkg.mjs';
|
|
11
|
+
import tagRelease from './lib/tagRelease.mjs';
|
|
12
|
+
|
|
13
|
+
|
|
13
14
|
|
|
14
15
|
const __filename = fileURLToPath(import.meta.url);
|
|
15
16
|
const __dirname = path.dirname(__filename);
|
|
16
17
|
|
|
17
18
|
|
|
18
|
-
const TEST_SERVER_HOST = "192.168.0.35";
|
|
19
19
|
|
|
20
20
|
function fmtAssetsCommit(msg) {
|
|
21
21
|
if (msg.startsWith("-#")) {
|
|
@@ -28,13 +28,16 @@ function fmtAssetsCommit(msg) {
|
|
|
28
28
|
* 加载配置
|
|
29
29
|
* @returns 配置对象
|
|
30
30
|
*/
|
|
31
|
-
async function loadConfig() {
|
|
32
|
-
|
|
31
|
+
async function loadConfig(configFileName) {
|
|
32
|
+
if(!configFileName){
|
|
33
|
+
configFileName = 'deploy.config.json';
|
|
34
|
+
}
|
|
35
|
+
const configJsonPath = join(process.cwd(), configFileName);
|
|
33
36
|
if (fs.existsSync(configJsonPath)) {
|
|
34
37
|
const configJson = JSON.parse(fs.readFileSync(configJsonPath, "utf-8"));
|
|
35
38
|
return configJson;
|
|
36
39
|
}
|
|
37
|
-
log(
|
|
40
|
+
log(`文件${configFileName}不存在, 请先创建`);
|
|
38
41
|
return null;
|
|
39
42
|
}
|
|
40
43
|
|
|
@@ -84,25 +87,6 @@ function hasChangeMe(content) {
|
|
|
84
87
|
return content.includes("CHANGE_ME");
|
|
85
88
|
}
|
|
86
89
|
|
|
87
|
-
async function getScpClient() {
|
|
88
|
-
let scpClient = null;
|
|
89
|
-
// 不能放密码。。残念
|
|
90
|
-
const scpClientConfig = {
|
|
91
|
-
host: TEST_SERVER_HOST,
|
|
92
|
-
username: 'root',
|
|
93
|
-
tryKeyboard: false,
|
|
94
|
-
password: 'tuwan123!@#',
|
|
95
|
-
}
|
|
96
|
-
// const sshKeyPath = join(homedir(), ".ssh/id_rsa");
|
|
97
|
-
// if (fs.existsSync(sshKeyPath)) {
|
|
98
|
-
// scpClientConfig.privateKey = fs.readFileSync(sshKeyPath, "utf-8");
|
|
99
|
-
// } else {
|
|
100
|
-
// log(`${sshKeyPath}不存在, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
|
|
101
|
-
// }
|
|
102
|
-
scpClient = await Client(scpClientConfig);
|
|
103
|
-
return scpClient;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
90
|
async function getVersion() {
|
|
107
91
|
const packageJsonPath = join(__dirname, "package.json");
|
|
108
92
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
|
|
@@ -124,16 +108,17 @@ async function main() {
|
|
|
124
108
|
// return;
|
|
125
109
|
// other commands
|
|
126
110
|
let command = process.argv[2];
|
|
127
|
-
if (!["init", "prod", "test", "scp", "scpevt", "pipeline", "offlinepkgrm"].includes(command)) {
|
|
111
|
+
if (!["init", "prod", "test", "scp", "scpevt", "pipeline", "offlinepkgrm", "rollback"].includes(command)) {
|
|
128
112
|
command = "help";
|
|
129
113
|
}
|
|
130
114
|
|
|
131
115
|
if (command === "help") {
|
|
132
116
|
console.log(`deploy-helper v${version}`);
|
|
133
117
|
console.log(`usage: deploy-helper [command]`);
|
|
134
|
-
console.log(`command: init 初始化配置`);
|
|
135
|
-
console.log(`command: prod 生产环境部署`);
|
|
118
|
+
console.log(`command: init [-c {configFileName}] 初始化配置`);
|
|
119
|
+
console.log(`command: prod [-c {configFileName}] 生产环境部署`);
|
|
136
120
|
console.log(`command: test 测试环境部署`);
|
|
121
|
+
console.log(`command: rollback {prod|test} {tag} 回滚到指定tag, 注意不处理离线包`);
|
|
137
122
|
console.log(`command: scp {file} 复制文件{file}到OfficialSite测试服务器{file}`);
|
|
138
123
|
console.log(`command: scp {file} {dest} 复制文件{file}到OfficialSite测试服务器{dest}`);
|
|
139
124
|
console.log(`command: scpevt {file} 复制文件{file}到events测试服务器{file}`);
|
|
@@ -144,6 +129,12 @@ async function main() {
|
|
|
144
129
|
}
|
|
145
130
|
log(`deploy-helper v${version} start`);
|
|
146
131
|
|
|
132
|
+
const workDir = process.cwd(); // 当前项目根目录
|
|
133
|
+
let mode = '';
|
|
134
|
+
let configFileName = '';
|
|
135
|
+
let isDoRollback = false;
|
|
136
|
+
let rollbackCleanFunc = null;
|
|
137
|
+
|
|
147
138
|
// 其他命令
|
|
148
139
|
if (command === "init") {
|
|
149
140
|
await initConfigJson();
|
|
@@ -154,7 +145,6 @@ async function main() {
|
|
|
154
145
|
log(`file参数不能为空`);
|
|
155
146
|
process.exit(1);
|
|
156
147
|
}
|
|
157
|
-
const workDir = process.cwd(); // 当前项目根目录
|
|
158
148
|
|
|
159
149
|
const srcFilePath = join(workDir, file);
|
|
160
150
|
if (!fs.existsSync(srcFilePath)) {
|
|
@@ -224,15 +214,46 @@ async function main() {
|
|
|
224
214
|
}
|
|
225
215
|
await syncOfflinePkgApi(userDeployHelperConfig, mode, { name, remove: true, platform }, null);
|
|
226
216
|
process.exit(0);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
217
|
+
} else if(command === "rollback") {
|
|
218
|
+
// 回滚
|
|
219
|
+
mode = process.argv[3] === 'prod' ? 'prod' : 'test'; // 部署环境
|
|
220
|
+
if(!['prod', 'test'].includes(mode)){
|
|
221
|
+
log(`mode参数只能是prod或test`);
|
|
222
|
+
process.exit(1);
|
|
223
|
+
}
|
|
224
|
+
let tag = process.argv[4];
|
|
225
|
+
if(!tag){
|
|
226
|
+
tag = await tagRelease.getTag(workDir, mode);
|
|
227
|
+
if(!tag){
|
|
228
|
+
log(`未找到tag`);
|
|
229
|
+
process.exit(1);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// 做好回滚操作
|
|
233
|
+
rollbackCleanFunc = await tagRelease.rollback(workDir, tag);
|
|
234
|
+
isDoRollback = true;
|
|
235
|
+
} else {
|
|
236
|
+
// 部署
|
|
237
|
+
mode = process.argv[2] === 'prod' ? 'prod' : 'test'; // 部署环境
|
|
231
238
|
log(`deploy mode: ${mode}`);
|
|
232
239
|
log(`workDir: ${workDir}`);
|
|
233
|
-
|
|
240
|
+
if(process.argv[3] === '-c'){
|
|
241
|
+
// 尝试从-c中获取配置文件名
|
|
242
|
+
configFileName = process.argv[4];
|
|
243
|
+
if(!configFileName){
|
|
244
|
+
log(`-c参数必须指定配置文件名`);
|
|
245
|
+
process.exit(1);
|
|
246
|
+
} else if(configFileName){
|
|
247
|
+
log(`将使用配置文件: ${configFileName}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
try {
|
|
234
252
|
// 读取配置
|
|
235
|
-
const config = await loadConfig();
|
|
253
|
+
const config = await loadConfig(configFileName);
|
|
254
|
+
if(!config){
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
236
257
|
// log(`config`, config);
|
|
237
258
|
|
|
238
259
|
const result = hasChangeMe(JSON.stringify(config));
|
|
@@ -241,8 +262,15 @@ async function main() {
|
|
|
241
262
|
process.exit(1);
|
|
242
263
|
}
|
|
243
264
|
|
|
265
|
+
if(!isDoRollback){ // 正常部署的时候,才会处理打tag操作
|
|
266
|
+
const canDoTag = await tagRelease.check(workDir, config, mode);
|
|
267
|
+
if(canDoTag){
|
|
268
|
+
await tagRelease.createRelease(workDir,config, mode);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
244
271
|
|
|
245
|
-
|
|
272
|
+
// rollback的时候,只处理entry文件的回滚操作,assets文件不处理
|
|
273
|
+
const needHandleAssets = !isDoRollback && config.assets && config.assets.dest;
|
|
246
274
|
|
|
247
275
|
// 需要处理entry
|
|
248
276
|
const needHandleEntry = config.entry && config.entry.dest && (mode === 'prod' || (mode === 'test' && !config.entry.onlyProd));
|
|
@@ -257,6 +285,7 @@ async function main() {
|
|
|
257
285
|
const offlinePkgResult = checkOfflinePkg({
|
|
258
286
|
workDir: workDir,
|
|
259
287
|
mode,
|
|
288
|
+
isDoRollback,
|
|
260
289
|
});
|
|
261
290
|
if (!offlinePkgResult.canBuild && offlinePkgResult.errorMsg) {
|
|
262
291
|
log(`${offlinePkgResult.errorMsg}`);
|
|
@@ -383,9 +412,12 @@ async function main() {
|
|
|
383
412
|
log(`entry: ${srcPath}是目录,不支持entry为目录的部署`);
|
|
384
413
|
return;
|
|
385
414
|
}
|
|
386
|
-
const
|
|
387
|
-
|
|
388
|
-
|
|
415
|
+
const dests = Array.isArray(dest) ? dest : [dest];
|
|
416
|
+
for (const dest of dests) {
|
|
417
|
+
const destPath = join(entryDest, dest);
|
|
418
|
+
fs.copyFileSync(srcPath, destPath);
|
|
419
|
+
log(`entry copy: ${srcPath} -> ${destPath}`);
|
|
420
|
+
}
|
|
389
421
|
}
|
|
390
422
|
// log(`entry copy done.`);
|
|
391
423
|
}
|
|
@@ -451,25 +483,39 @@ async function main() {
|
|
|
451
483
|
const scpClient = await getScpClient();
|
|
452
484
|
for (const [src, dest] of Object.entries(syncTestFiles)) {
|
|
453
485
|
const srcPath = join(workDir, src);
|
|
454
|
-
const
|
|
455
|
-
|
|
456
|
-
|
|
486
|
+
const dests = Array.isArray(dest) ? dest : [dest];
|
|
487
|
+
for (const dest of dests) {
|
|
488
|
+
const destPath = "/home/web/website/tuwan_www/templets/static/play/" + dest;
|
|
489
|
+
log(`scp: ${srcPath} -> ${TEST_SERVER_HOST}:${destPath}`);
|
|
490
|
+
await scpClient.uploadFile(srcPath, destPath)
|
|
491
|
+
}
|
|
457
492
|
}
|
|
458
493
|
}
|
|
459
494
|
} catch (error) {
|
|
460
495
|
log(`scp error: ${error}`);
|
|
461
|
-
log(`复制文件到测试环境服务器失败, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
|
|
462
|
-
if (assetsFilesCount > 0) {
|
|
463
|
-
|
|
464
|
-
}
|
|
496
|
+
// log(`复制文件到测试环境服务器失败, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
|
|
497
|
+
// if (assetsFilesCount > 0) {
|
|
498
|
+
// log(`不过请放心,构建产物的assets已经处理完成,只要手动处理index.html文件即可。`);
|
|
499
|
+
// }
|
|
465
500
|
process.exit(1);
|
|
466
501
|
}
|
|
467
502
|
|
|
503
|
+
if(isDoRollback && rollbackCleanFunc){
|
|
504
|
+
await rollbackCleanFunc();
|
|
505
|
+
}
|
|
468
506
|
|
|
469
|
-
|
|
470
|
-
|
|
507
|
+
try {
|
|
508
|
+
// 处理触发流水线的任务
|
|
509
|
+
await runPipeline(config, mode);
|
|
510
|
+
} catch (error) {
|
|
511
|
+
log(`触发流水线失败: `, error);
|
|
512
|
+
}
|
|
471
513
|
|
|
472
|
-
|
|
514
|
+
if(isDoRollback){
|
|
515
|
+
log(`deploy-helper rollback done.`);
|
|
516
|
+
} else {
|
|
517
|
+
log(`deploy-helper deploy done.`);
|
|
518
|
+
}
|
|
473
519
|
process.exit(0);
|
|
474
520
|
|
|
475
521
|
} catch (error) {
|
package/lib/offlinePkg.mjs
CHANGED
|
@@ -154,9 +154,17 @@ if(item.attrs.id=='vite-legacy-polyfill')childNode.onload=function(){System.impo
|
|
|
154
154
|
* @param {Object} config 配置
|
|
155
155
|
* @param {string} config.workDir 工作目录
|
|
156
156
|
* @param {"prod"|"test"} config.mode 部署模式
|
|
157
|
+
* @param {boolean} config.isDoRollback 是否是回滚操作
|
|
157
158
|
* @returns {CheckOfflinePkgResult} 结果
|
|
158
159
|
*/
|
|
159
160
|
export function checkOfflinePkg(config) {
|
|
161
|
+
if(config.isDoRollback){
|
|
162
|
+
log(`【注意】回滚操作,不处理离线包`);
|
|
163
|
+
return {
|
|
164
|
+
canBuild: false,
|
|
165
|
+
errorMsg: "",
|
|
166
|
+
}
|
|
167
|
+
}
|
|
160
168
|
let canBuildOfflinePkg = true;
|
|
161
169
|
let errorMsg = "";
|
|
162
170
|
const offlineConfig = getJsonConfig(config.workDir, "offline.config.json");
|
|
@@ -207,14 +215,6 @@ export function checkOfflinePkg(config) {
|
|
|
207
215
|
canBuildOfflinePkg = false;
|
|
208
216
|
}
|
|
209
217
|
}
|
|
210
|
-
let ossToken = "";
|
|
211
|
-
if (canBuildOfflinePkg) {
|
|
212
|
-
ossToken = getOssToken();
|
|
213
|
-
if (!ossToken) {
|
|
214
|
-
errorMsg = "获取ossToken失败";
|
|
215
|
-
canBuildOfflinePkg = false;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
218
|
if (willSkip) {
|
|
219
219
|
if (!offlineConfig) {
|
|
220
220
|
return {
|
|
@@ -235,6 +235,14 @@ export function checkOfflinePkg(config) {
|
|
|
235
235
|
}
|
|
236
236
|
}
|
|
237
237
|
}
|
|
238
|
+
let ossToken = "";
|
|
239
|
+
if (canBuildOfflinePkg) {
|
|
240
|
+
ossToken = getOssToken();
|
|
241
|
+
if (!ossToken) {
|
|
242
|
+
errorMsg = "获取ossToken失败";
|
|
243
|
+
canBuildOfflinePkg = false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
238
246
|
if (!canBuildOfflinePkg) {
|
|
239
247
|
return {
|
|
240
248
|
canBuild: false,
|
package/lib/pipelineApi.mjs
CHANGED
|
@@ -355,6 +355,7 @@ export async function runPipeline(config, mode) {
|
|
|
355
355
|
const token = getYunxiaoToken(pipelineConfig);
|
|
356
356
|
if (!token) {
|
|
357
357
|
log(`未设置云效token, 此次流水线未自动执行: ${JSON.stringify(pipelineConfig)}`);
|
|
358
|
+
return;
|
|
358
359
|
}
|
|
359
360
|
process.env.YUNXIAO_ACCESS_TOKEN = token;
|
|
360
361
|
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// 处理项目发布时的构建产物问题
|
|
2
|
+
|
|
3
|
+
import { log } from "./util.mjs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import simpleGit from "simple-git";
|
|
7
|
+
import rawlist from "@inquirer/rawlist";
|
|
8
|
+
|
|
9
|
+
async function getTag(workDir, mode) {
|
|
10
|
+
const packageJson = JSON.parse(
|
|
11
|
+
fs.readFileSync(path.join(workDir, "package.json"), "utf-8")
|
|
12
|
+
);
|
|
13
|
+
const prefix = getTagNamePrefix(packageJson, mode);
|
|
14
|
+
const git = simpleGit(workDir);
|
|
15
|
+
const tags = await git.tags();
|
|
16
|
+
console.log(`tags: ${JSON.stringify(tags)}`);
|
|
17
|
+
let tagList = tags.all.filter((tag) => tag.startsWith(prefix)).slice(0, 15);
|
|
18
|
+
let tag = null;
|
|
19
|
+
if (tagList.length > 0) {
|
|
20
|
+
const propmtConfig = {
|
|
21
|
+
message: "请选择要回滚的tag版本: ",
|
|
22
|
+
choices: tagList.map((item) => ({
|
|
23
|
+
name: item,
|
|
24
|
+
value: item,
|
|
25
|
+
})),
|
|
26
|
+
};
|
|
27
|
+
try {
|
|
28
|
+
tag = await rawlist(propmtConfig);
|
|
29
|
+
} catch (error) {
|
|
30
|
+
log(`选择获取tag失败`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (tag) {
|
|
34
|
+
log(`选择tag: ${tag}`);
|
|
35
|
+
}
|
|
36
|
+
return tag;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 前置条件,项目当前是干净的,没有未提交的修改
|
|
40
|
+
|
|
41
|
+
function getTagNamePrefix(packageJson, mode) {
|
|
42
|
+
return `release_${mode}_${packageJson.name}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function getTagName(packageJson, commitId, mode) {
|
|
46
|
+
const prefix = getTagNamePrefix(packageJson, mode);
|
|
47
|
+
const date = new Date();
|
|
48
|
+
const timeStr =
|
|
49
|
+
date.getMonth() +
|
|
50
|
+
1 +
|
|
51
|
+
"_" +
|
|
52
|
+
date.getDate() +
|
|
53
|
+
"_" +
|
|
54
|
+
date.getHours() +
|
|
55
|
+
"_" +
|
|
56
|
+
date.getMinutes() +
|
|
57
|
+
"_" +
|
|
58
|
+
date.getSeconds();
|
|
59
|
+
return `${prefix}@${packageJson.version}_${commitId.slice(0, 8)}_${timeStr}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 检查是否需要打tag
|
|
64
|
+
* @param {*} config 配置对象
|
|
65
|
+
* @returns 是否需要打tag
|
|
66
|
+
*/
|
|
67
|
+
async function check(workDir, config, mode) {
|
|
68
|
+
const tagConfig = config.tag;
|
|
69
|
+
if (!tagConfig) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
let needTag = false;
|
|
73
|
+
if (tagConfig.test && mode === "test") {
|
|
74
|
+
needTag = true;
|
|
75
|
+
}
|
|
76
|
+
if (tagConfig.prod && mode === "prod") {
|
|
77
|
+
needTag = true;
|
|
78
|
+
}
|
|
79
|
+
const skipCleanCheck = false;
|
|
80
|
+
if (needTag && !skipCleanCheck) {
|
|
81
|
+
// 检查当前仓库是否干净
|
|
82
|
+
const git = simpleGit(workDir);
|
|
83
|
+
const status = await git.status();
|
|
84
|
+
if (!status.isClean()) {
|
|
85
|
+
log(`【tag】当前仓库有未提交的修改,请先提交`);
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return needTag;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 回滚到指定tag
|
|
93
|
+
* @param {string} workDir 工作目录
|
|
94
|
+
* @param {string} mode 部署模式
|
|
95
|
+
* @param {string} tag 要回滚的tag
|
|
96
|
+
* @returns Promise<function> 回滚后需要执行的清理函数
|
|
97
|
+
*/
|
|
98
|
+
async function rollback(workDir, tag) {
|
|
99
|
+
const git = simpleGit(workDir);
|
|
100
|
+
const commitId = await git.revparse("HEAD");
|
|
101
|
+
const branchInfo = await git.branch();
|
|
102
|
+
const status = await git.status();
|
|
103
|
+
if (!status.isClean()) {
|
|
104
|
+
log(`【rollback】回滚前请确保当前仓库是干净的`);
|
|
105
|
+
process.exit(1);
|
|
106
|
+
}
|
|
107
|
+
// log("回滚前状态:");
|
|
108
|
+
log(`branch: ${branchInfo.current}`);
|
|
109
|
+
log(`commitId: ${commitId}`);
|
|
110
|
+
const cleanfunc = async () => {
|
|
111
|
+
await git.reset(["--hard", commitId]);
|
|
112
|
+
await git.checkout(branchInfo.current);
|
|
113
|
+
};
|
|
114
|
+
try {
|
|
115
|
+
await git.checkout(tag);
|
|
116
|
+
const releaseDir = path.join(process.cwd(), ".taole_release");
|
|
117
|
+
if (!fs.existsSync(releaseDir)) {
|
|
118
|
+
log(`【rollback】.taole_release目录不存在,无法回滚`);
|
|
119
|
+
await cleanfunc();
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
const config = JSON.parse(
|
|
123
|
+
fs.readFileSync(path.join(releaseDir, "deploy.config.json"), "utf-8")
|
|
124
|
+
);
|
|
125
|
+
const entryFiles = config.entry.files;
|
|
126
|
+
Object.keys(entryFiles).forEach((key) => {
|
|
127
|
+
const src = `_${key}`;
|
|
128
|
+
const dest = key;
|
|
129
|
+
log(`rollback file: ${dest}`);
|
|
130
|
+
fs.cpSync(path.join(releaseDir, src), path.join(workDir, dest), {
|
|
131
|
+
recursive: true,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
fs.cpSync(
|
|
135
|
+
path.join(releaseDir, "deploy.config.json"),
|
|
136
|
+
path.join(workDir, "deploy.config.json"),
|
|
137
|
+
{
|
|
138
|
+
recursive: true,
|
|
139
|
+
force: true,
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
log(`rollback file: deploy.config.json`);
|
|
143
|
+
log(`ready to re-deploy`);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
await cleanfunc();
|
|
146
|
+
log(`【rollback】回滚失败: ${error}`);
|
|
147
|
+
process.exit(1);
|
|
148
|
+
}
|
|
149
|
+
return cleanfunc;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* 创建tag记录,用于后续的回退
|
|
154
|
+
* @param {*} config
|
|
155
|
+
*/
|
|
156
|
+
async function createRelease(workDir, config, mode) {
|
|
157
|
+
// 操作步骤:
|
|
158
|
+
// 0. 记录当前git的commit id, 记为commitId
|
|
159
|
+
// 1. 在项目目录检查是否有.taole_release目录,有则删除然后新建
|
|
160
|
+
// 2. 将配置中entry.files定义的后缀为html的文件复制到.taole_release目录下
|
|
161
|
+
// 3. 提交.taole_release目录到git,commmit 信息为 -#DH-2 整理构建产物
|
|
162
|
+
// 4. 打tag,tag名称为 v{package.json的version}.{commitId}.{yyyy_mm_dd_HH_MM}, 并推到远程仓库
|
|
163
|
+
// 5. 回退当前git的commit id到commitId
|
|
164
|
+
|
|
165
|
+
const git = simpleGit(workDir);
|
|
166
|
+
const commitId = await git.revparse("HEAD");
|
|
167
|
+
log(`commitId: ${commitId}`);
|
|
168
|
+
|
|
169
|
+
const cleanfunc = async () => {
|
|
170
|
+
await git.reset(["--hard", commitId]);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
// 1. 在项目目录检查是否有.taole_release目录,有则删除然后新建
|
|
175
|
+
const releaseDir = path.join(process.cwd(), ".taole_release");
|
|
176
|
+
if (fs.existsSync(releaseDir)) {
|
|
177
|
+
fs.rmSync(releaseDir, { recursive: true });
|
|
178
|
+
}
|
|
179
|
+
fs.mkdirSync(releaseDir);
|
|
180
|
+
|
|
181
|
+
// 2. 将配置中entry.files定义的后缀为html的文件涉及的所有html文件复制到.taole_release目录下
|
|
182
|
+
const entryFiles = config.entry.files;
|
|
183
|
+
Object.keys(entryFiles).forEach((key) => {
|
|
184
|
+
const file = key;
|
|
185
|
+
const htmlFilePath = path.join(workDir, file);
|
|
186
|
+
log(`htmlFilePath: ${htmlFilePath}`);
|
|
187
|
+
if (fs.existsSync(htmlFilePath)) {
|
|
188
|
+
const destFile = `_${file}`;
|
|
189
|
+
fs.cpSync(htmlFilePath, path.join(releaseDir, destFile), {
|
|
190
|
+
recursive: true,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
// 2.1 将当前的config也写入.taole_release目录下
|
|
195
|
+
fs.writeFileSync(
|
|
196
|
+
path.join(releaseDir, "deploy.config.json"),
|
|
197
|
+
JSON.stringify(config, null, 2)
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
await git.add(releaseDir);
|
|
201
|
+
await git.commit("-#DH-2 整理构建产物");
|
|
202
|
+
// log(`commitResult: ${JSON.stringify(commitResult)}`);
|
|
203
|
+
|
|
204
|
+
const packageJson = JSON.parse(
|
|
205
|
+
fs.readFileSync(path.join(workDir, "package.json"), "utf-8")
|
|
206
|
+
);
|
|
207
|
+
const tagName = getTagName(packageJson, commitId, mode);
|
|
208
|
+
// log(`tagName: ${tagName}`);
|
|
209
|
+
await git.tag([tagName]);
|
|
210
|
+
await git.push("origin", tagName);
|
|
211
|
+
log(`打tag成功: ${tagName}, 可用于后续回滚`);
|
|
212
|
+
await cleanfunc();
|
|
213
|
+
} catch (error) {
|
|
214
|
+
log(`创建tag记录失败: ${error}`);
|
|
215
|
+
await cleanfunc();
|
|
216
|
+
process.exit(1);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export default {
|
|
221
|
+
getTag,
|
|
222
|
+
check,
|
|
223
|
+
createRelease,
|
|
224
|
+
rollback,
|
|
225
|
+
};
|
package/lib/util.mjs
CHANGED
|
@@ -1,46 +1,49 @@
|
|
|
1
|
-
import fs from
|
|
2
|
-
import { join } from "node:path"
|
|
3
|
-
import { homedir } from "node:os"
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { Client } from "node-scp";
|
|
4
5
|
|
|
5
6
|
let _isDebug = false;
|
|
6
7
|
export function setDebug(debug) {
|
|
7
|
-
|
|
8
|
+
_isDebug = debug;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
11
|
export function isDebug() {
|
|
11
|
-
|
|
12
|
+
return _isDebug;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export function log(...args) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
if (_isDebug) {
|
|
17
|
+
// 打印时间
|
|
18
|
+
console.log(`[${new Date().toLocaleTimeString()}]`, ...args);
|
|
19
|
+
} else {
|
|
20
|
+
console.log(...args);
|
|
21
|
+
}
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
export function getOssToken() {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
let token = "";
|
|
26
|
+
token = process.env.TW_DH_OSS_TOKEN || "";
|
|
27
|
+
if (token) {
|
|
28
|
+
log(`将使用来自环境变量TW_DH_OSS_TOKEN的token`);
|
|
29
|
+
return token;
|
|
30
|
+
}
|
|
31
|
+
const userDeployHelperDir = join(homedir(), "deploy-helper.config.json");
|
|
32
|
+
if (fs.existsSync(userDeployHelperDir)) {
|
|
33
|
+
try {
|
|
34
|
+
const userDeployHelperConfig = JSON.parse(
|
|
35
|
+
fs.readFileSync(userDeployHelperDir, "utf-8")
|
|
36
|
+
);
|
|
37
|
+
token = userDeployHelperConfig.ossToken || "";
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error(`读取${userDeployHelperDir}配置失败: ${error}`);
|
|
29
40
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const userDeployHelperConfig = JSON.parse(fs.readFileSync(userDeployHelperDir, "utf-8"));
|
|
34
|
-
token = userDeployHelperConfig.ossToken || "";
|
|
35
|
-
} catch (error) {
|
|
36
|
-
console.error(`读取${userDeployHelperDir}配置失败: ${error}`);
|
|
37
|
-
}
|
|
38
|
-
if (token) {
|
|
39
|
-
log(`将使用来自${userDeployHelperDir}的ossToken`);
|
|
40
|
-
return token;
|
|
41
|
-
}
|
|
41
|
+
if (token) {
|
|
42
|
+
log(`将使用来自${userDeployHelperDir}的ossToken`);
|
|
43
|
+
return token;
|
|
42
44
|
}
|
|
43
|
-
|
|
45
|
+
}
|
|
46
|
+
return token;
|
|
44
47
|
}
|
|
45
48
|
|
|
46
49
|
/**
|
|
@@ -48,14 +51,36 @@ export function getOssToken() {
|
|
|
48
51
|
* @returns {Object|null} 用户配置
|
|
49
52
|
*/
|
|
50
53
|
export function getUserDeployHelperConfig() {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
const userDeployHelperDir = join(homedir(), "deploy-helper.config.json");
|
|
55
|
+
if (fs.existsSync(userDeployHelperDir)) {
|
|
56
|
+
try {
|
|
57
|
+
const userDeployHelperConfig = JSON.parse(
|
|
58
|
+
fs.readFileSync(userDeployHelperDir, "utf-8")
|
|
59
|
+
);
|
|
60
|
+
return userDeployHelperConfig;
|
|
61
|
+
} catch (error) {
|
|
62
|
+
console.error(`读取${userDeployHelperDir}配置失败: ${error}`);
|
|
59
63
|
}
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const TEST_SERVER_HOST = "192.168.0.35";
|
|
69
|
+
export async function getScpClient() {
|
|
70
|
+
let scpClient = null;
|
|
71
|
+
// 不能放密码。。残念
|
|
72
|
+
const scpClientConfig = {
|
|
73
|
+
host: TEST_SERVER_HOST,
|
|
74
|
+
username: "root",
|
|
75
|
+
tryKeyboard: false,
|
|
76
|
+
password: "tuwan123!@#",
|
|
77
|
+
};
|
|
78
|
+
// const sshKeyPath = join(homedir(), ".ssh/id_rsa");
|
|
79
|
+
// if (fs.existsSync(sshKeyPath)) {
|
|
80
|
+
// scpClientConfig.privateKey = fs.readFileSync(sshKeyPath, "utf-8");
|
|
81
|
+
// } else {
|
|
82
|
+
// log(`${sshKeyPath}不存在, 建议配置本机ssh免密登录, 参考地址:https://foochane.cn/article/2019061601.html`);
|
|
83
|
+
// }
|
|
84
|
+
scpClient = await Client(scpClientConfig);
|
|
85
|
+
return scpClient;
|
|
86
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taole/deploy-helper",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.4-beta.2",
|
|
4
4
|
"description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
|
|
5
5
|
"main": "index.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -24,8 +24,9 @@
|
|
|
24
24
|
"author": "",
|
|
25
25
|
"license": "ISC",
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"
|
|
27
|
+
"@inquirer/rawlist": "^5.2.0",
|
|
28
28
|
"ali-oss": "^6.23.0",
|
|
29
|
+
"alibabacloud-devops-mcp-server": "*",
|
|
29
30
|
"archiver": "^7.0.1",
|
|
30
31
|
"md5-file": "^5.0.0",
|
|
31
32
|
"node-scp": "^0.0.25",
|