build.js
6.13 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* 本地项目构建
* clone >> build >> zip
*/
'use strict';
const sh = require('shelljs');
const moment = require('moment');
const path = require('path');
const fsp = require('fs-promise');
const fs = require('fs');
const config = require('../../config/config');
const Tar = require('./tar');
const ws = require('../../lib/ws');
const {
Building
} = require('../models');
const _ = require('lodash');
class Build {
constructor(project) {
this.project = project;
this.state = 'waiting building';
this.silent = true;
}
build(branch) {
if (typeof config.buildDir === 'undefined') {
throw new Error('please config the buildDir');
}
this.branch = branch;
let buildTime = moment().format('YYYYMMDDHHmmss')
this.buildTime = buildTime;
return {
buildTime: buildTime,
distFile: path.join(this.project.name, buildTime, `${this.project.name}.tar.gz`)
};
}
run(bid, callback) {
this.bid = bid;
let self = this;
this._prebuild();
this.startTime = (new Date()).getTime();
return this._cloneCode(this.branch).then(() => {
return self._buildScript();
}).then(() => {
return self._ignoreFile();
}).then(() => {
return self._zipBuild();
}).then(() => {
self._state('success');
let diff = (new Date()).getTime() - self.startTime;
let costTime = moment.duration(diff, 'ms').humanize();
self._log(`\n\n========== build success. cost: ${costTime} =======================`);
if (callback && typeof callback === 'function') {
callback();
}
}).catch((e) => {
console.error(e);
self._state('fail');
});
}
get buildPath() {
return path.join(config.buildDir, this.project.name, this.buildTime, this.project.name);
}
get rootPath() {
return path.join(config.buildDir, this.project.name, this.buildTime);
}
/**
* do some folder check
*/
_prebuild() {
if (!sh.test('-e', config.buildDir)) {
sh.mkdir('-p', config.buildDir);
}
if (!sh.test('-e', this.rootPath)) {
sh.mkdir('-p', this.rootPath);
}
this.logFile = path.join(this.rootPath, 'building.log');
sh.touch(this.logFile);
}
_cloneCode(branch) {
var self = this;
this._state('cloning_code');
let clone_script = `git clone --depth 1 -b ${branch} ${this.project.gitlab}`;
this._log(`>>>>>>>>> ${clone_script} >>>>>>>>>>>`);
return new Promise((reslove, reject) => {
sh.cd(self.rootPath);
let child = sh.exec(clone_script, {
silent: self.silent,
async: true
});
child.stdout.pipe(fs.createWriteStream(self.logFile, {
flags: 'a'
}));
child.stderr.pipe(fs.createWriteStream(self.logFile, {
flags: 'a'
}));
// child.stderr.on('data', (data) => {
// self._log(data);
// });
child.on('close', (code) => {
if (code == 0) {
console.log('clone code success');
reslove();
} else {
reject(new Error(`clone code fail`));
}
});
});
}
_buildScript() {
var self = this;
if (this.project.scripts && this.project.scripts.build) {
this._log(`>>>>>>>>> ${this.project.scripts.build} >>>>>>>>>>>`);
return new Promise((reslove, reject) => {
self._state('script_building');
sh.cd(self.buildPath);
var child = sh.exec(self.project.scripts.build, {
silent: self.silent,
async: true
});
// child.stdout.on('data', (data) => {
// self._log(data);
// });
// child.stderr.on('data', (data) => {
// self._log(data);
// });
child.stdout.pipe(fs.createWriteStream(self.logFile, {
flags: 'a'
}));
child.stderr.pipe(fs.createWriteStream(self.logFile, {
flags: 'a'
}));
child.on('close', (code) => {
if (code == 0) {
console.log('build success');
reslove();
} else {
reject(new Error(`build code fail`));
}
});
});
} else {
this._log(`>>>>>>>>> no build script >>>>>>>>>>>`);
return Promise.resolve();
}
}
_ignoreFile() {
if (this.project.ignore) {
let files = this.project.ignore.split(';');
sh.cd(this.buildPath);
_.forEach(files, f => {
if (f) {
let fDir = path.join(this.buildPath, f);
if (fDir.indexOf(this.buildPath) === 0) {
sh.rm('-rf', fDir);
} else {
this.log(`Illegal ignore path: ${f}`);
}
}
});
}
}
_zipBuild() {
this._state('gziping');
let target = this.buildPath;
let dist = path.join(this.rootPath, `${this.project.name}.tar.gz`);
return Tar.gzip(target, dist).then(filecode => {
Building.updateMd5(this.bid, filecode);
});
}
async _state(state) {
ws.broadcast(`/building/${this.project._id}`, {
bid: this.bid,
state: state
});
this._log(`>>>>>>>>> ${state} >>>>>>>>>>>`);
await Building.updateState(this.bid, state);
}
_log(line) {
// ws.broadcast(`/building/${this.project._id}/log`, line);
console.log(line);
fsp.appendFile(this.logFile, line + '\n');
}
}
module.exports = Build;