-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (95 loc) · 2.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const http = require('http');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// 递归删除目录
function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function(file) {
const curPath = path + '/' + file;
if (fs.statSync(curPath).isDirectory()) {
// recurse
deleteFolderRecursive(curPath);
} else {
// delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
const resolvePost = (req) =>
new Promise((resolve, reject) => {
let body = '';
req.on('data', (chunk) => {
console.log('chunk --- ', chunk);
body += chunk;
});
req.on('end', () => {
try {
let res = decodeURIComponent(body);
if (res.startsWith('payload=')) {
resolve(JSON.parse(res.split('payload=')[1]));
} else {
resolve(JSON.parse(body));
}
} catch (error) {
reject(new Error('无法处理的返回值'));
}
});
});
http
.createServer(async (req, res) => {
console.log('receive request');
console.log('url', req.url);
if (req.method === 'POST' && req.url === '/') {
const data = await resolvePost(req);
if (!data || !data.repository) {
res.end(data.message);
}
console.log('resolvePost --- ', data);
const projectDir = path.resolve(`./${data.repository.name}`);
deleteFolderRecursive(projectDir);
// 拉取仓库最新代码
execSync(
`git clone https://github.com/zongyuan-vale/${data.repository.name}.git ${projectDir}`,
{
stdio: 'inherit',
}
);
// 复制 Dockerfile 到项目目录
// fs.copyFileSync(
// path.resolve(`./Dockerfile`),
// path.resolve(projectDir, './Dockerfile')
// );
// 复制 .dockerignore 到项目目录
// fs.copyFileSync(
// path.resolve(__dirname, `./.dockerignore`),
// path.resolve(projectDir, './.dockerignore')
// );
// 创建 docker 镜像
execSync(`docker build . -t ${data.repository.name}-image:latest `, {
stdio: 'inherit',
cwd: projectDir,
});
// 销毁 docker 容器
execSync(
`docker ps -a -f "name=^${data.repository.name}-container" --format="{{.Names}}" | xargs -r docker stop | xargs -r docker rm`,
{
stdio: 'inherit',
}
);
// 创建 docker 容器
execSync(
`docker run -d -p 8888:80 --name ${data.repository.name}-container ${data.repository.name}-image:latest`,
{
stdio: 'inherit',
}
);
console.log('deploy success');
res.end('ok');
}
})
.listen(3000, () => {
console.log('server is ready');
});