forked from jhonzya/git-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
149 lines (125 loc) · 4.25 KB
/
actions.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const fs = require('fs');
const shell = require('shelljs');
const _ = require('lodash');
var moment = require('moment');
if( process.env.SLACK_HOOK ){
var slack = require('slack-notify')(process.env.SLACK_HOOK);
}
moment.locale('es');
var self = module.exports = {
/**
* type: log, info, error, warning
*/
logger: (str, type='log', notify=true, attachments=[]) => {
let time = moment().format('DD/MM/YYYY HH:mm:ss');
const types = {
'log': 'note',
'info': 'success',
'error': 'bug',
'warning': 'alert'
};
if( !(type in types) ) type = 'log';
console[type](time, str);
// Send slack message
if( notify && process.env.SLACK_HOOK ){
const text = str;
if( attachments.length === 0 ){
slack[types[type]](text);
}
else{
slack[types[type]]({text, attachments});
}
}
},
deploy: (dir, data) => {
self.logger(`Se ha iniciado deploy en ${data.repo}, rama ${data.target}.`, 'info');
var response = {
status: 200,
text: 'success'
};
const file = dir+'/.deployfile';
var commands = null;
// Read .deployfile
try{
if( fs.existsSync(file) ){
commands = fs.readFileSync(file, 'utf8');
commands = JSON.parse(commands).update;
}
else{
response.text = `Deployfile (${file}) for repo ${data.repo} and branch (${data.target}) doesn't exist`;
response.status = 404;
}
}catch(error){
response.text = error.message;
response.status = 500;
}
// .deployfile not found
if( commands === null ){
self.finish_deploy(response, data);
return;
}
// Exec commands
process.chdir(dir);
var results = [];
commands.reduce((p, command) => {
return p.then( () => {
return self.exec_command(command).then((code) => {
results.push(code);
});
} );
}, Promise.resolve()).then( () => {
data['results'] = results;
self.finish_deploy(response, data);
}, (error) => {
response.text = error;
response.status = 500;
self.finish_deploy(response, data);
} );
},
exec_command: (cmd) => {
self.logger(">"+cmd, 'log', false);
return new Promise((resolve, reject) => {
shell.exec(cmd, {silent: true}, (code, stdout, stderr) => {
resolve(code === 0 ? {cmd, code, stdout} : {cmd, code, stderr});
});
});
},
finish_deploy(response, data){
let time = moment().format('DD/MM/YYYY HH:mm:ss');
let dif = moment.utc(
moment(time,"DD/MM/YYYY HH:mm:ss")
.diff(moment(data['started'],"DD/MM/YYYY HH:mm:ss"))
).format("mm [m] ss [s]");
let message = `(${dif}) Ha finalizado deploy en ${data.repo}, rama ${data.target}.`;
let type = 'info';
let attachments = [];
if(response.status !== 200){
type = 'error';
attachments.push({
title: 'Ha ocurrido un error',
text: response.text,
color: 'danger'
});
}
else{
process.chdir(__dirname);
var filename = process.env.name+"_"+moment().format('DD-MM-YYYY_HH-mm-ss')+".log";
fs.writeFile('./logs/'+filename, JSON.stringify(data['results'], 0, 2), (error) => {
if(error) self.logger(error, 'error', false);
});
var fields = [];
_.each(data.commits, commit => {
fields.push({
title: commit.message,
value: `${commit.author.name} (<${commit.url}|ver commit>)`,
short: false
});
});
attachments.push({
color: "good",
fields: fields
});
}
self.logger(message, type, true, attachments);
}
};