-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (72 loc) · 2.47 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
#!/usr/bin/env node
const express = require('express');
const bodyParser = require('body-parser');
const { spawn } = require('child_process');
const winston = require('winston');
const fs = require("fs");
const configFile = './config.json';
const config = JSON.parse(fs.readFileSync(configFile));
const app = express();
const port = config.port || 3000;
// Winston 配置
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: './logs/webhook.log' })
]
});
// 中间件
app.use(bodyParser.json());
app.get('/webhook/health', (req, res) => {
logger.info(`======= received health =======`);
res.status(200).send('health');
})
// 路由
app.post('/webhook/:project', (req, res) => {
const project = req.params.project;
logger.info(`======= received webhook request for project ${project} =======`);
if (!config[project]) {
logger.error(`没有在配置文件中找到项目: ${project},请检查名称是否正确!`);
res.status(200).send(`Webhook received, but Not Found project ${project}.`);
return
}
const payload = config[project]
const deployPath = `${payload.path}/deploy.sh`
logger.info(`======= deploying ${payload.name} project at ${deployPath} =======`);
const deploy = spawn('sh', [deployPath], {
shell: true
});
deploy.stdout.on('data', data => {
logger.info(data.toString());
});
deploy.stderr.on('data', data => {
logger.error(data.toString());
});
// 执行自定义的命令
deploy.on('close', () => {
logger.info(`======= deploy.sh done =======`);
if (payload.command) {
logger.info(`======= run command: ${payload.command} =======`);
const postDeploy = spawn('sh', ['-c', payload.command]);
postDeploy.stdout.on('data', data => {
logger.info(data.toString());
});
postDeploy.stderr.on('data', data => {
logger.error(data.toString());
});
postDeploy.on('close', () => {
logger.info(`======= ${payload.name} command done =======`)
})
}
});
res.status(200).send('Webhook received');
});
// 启动服务
app.listen(port, () => {
logger.info(`Webhook server listening on port ${port}`);
});