...
|
...
|
@@ -32,9 +32,11 @@ class Build { |
|
|
this.branch = branch;
|
|
|
let buildTime = moment().format('YYYYMMDDHHmmss')
|
|
|
this.buildTime = buildTime;
|
|
|
this.version = '--';
|
|
|
|
|
|
return {
|
|
|
buildTime: buildTime,
|
|
|
version: this.version,
|
|
|
distFile: path.join(this.project.name, buildTime, `${this.project.name}.tar.gz`)
|
|
|
};
|
|
|
}
|
...
|
...
|
@@ -45,9 +47,15 @@ class Build { |
|
|
this._prebuild();
|
|
|
this.startTime = (new Date()).getTime();
|
|
|
return this._cloneCode(this.branch).then(() => {
|
|
|
// set version
|
|
|
self.version = require(path.join(this.codePath, 'package.json')).version;
|
|
|
|
|
|
return self._installdep();
|
|
|
}).then(() => {
|
|
|
return self._buildScript();
|
|
|
}).then(() => {
|
|
|
self._zipBuild();
|
|
|
return self._cloneToDeploy();
|
|
|
}).then(() => {
|
|
|
self._state('success');
|
|
|
let diff = (new Date()).getTime() - self.startTime;
|
|
|
let costTime = moment.duration(diff, 'ms').humanize();
|
...
|
...
|
@@ -58,6 +66,10 @@ class Build { |
|
|
});
|
|
|
}
|
|
|
|
|
|
get codePath() {
|
|
|
return path.join(config.codeDir, this.project.name);
|
|
|
}
|
|
|
|
|
|
get buildPath() {
|
|
|
return path.join(config.buildDir, this.project.name, this.buildTime, this.project.name);
|
|
|
}
|
...
|
...
|
@@ -74,22 +86,37 @@ class Build { |
|
|
sh.mkdir('-p', config.buildDir);
|
|
|
}
|
|
|
|
|
|
if (!sh.test('-e', config.codeDir)) {
|
|
|
sh.mkdir('-p', config.codeDir);
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
_cloneCode() {
|
|
|
var self = this;
|
|
|
this._state('cloning_code');
|
|
|
let clone_script = `git clone -b ${branch} ${this.project.gitlab}`;
|
|
|
this._log(`>>>>>>>>> ${clone_script} >>>>>>>>>>>`);
|
|
|
var syncCodeScript;
|
|
|
|
|
|
return new Promise((reslove, reject) => {
|
|
|
sh.cd(self.rootPath);
|
|
|
let child = sh.exec(clone_script, {
|
|
|
|
|
|
if(sh.ls(config.codeDir).indexOf(this.project.name) > -1) {
|
|
|
syncCodeScript = `git submodule update ${this.project.name}`;
|
|
|
} else {
|
|
|
syncCodeScript = `git submodule add ${this.project.gitlab}`;
|
|
|
}
|
|
|
|
|
|
// let clone_script = `git clone -b develop ${this.project.gitlab}`;
|
|
|
this._log(`>>>>>>>>> ${syncCodeScript} >>>>>>>>>>>`);
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
this._state('sync code');
|
|
|
sh.cd(config.codeDir);
|
|
|
|
|
|
let child = sh.exec(syncCodeScript, {
|
|
|
silent: self.silent,
|
|
|
async: true
|
|
|
});
|
...
|
...
|
@@ -108,33 +135,58 @@ class Build { |
|
|
|
|
|
child.on('close', (code) => {
|
|
|
if (code == 0) {
|
|
|
console.log('clone code success');
|
|
|
reslove();
|
|
|
console.log('sync code success');
|
|
|
resolve();
|
|
|
} else {
|
|
|
reject(new Error(`clone code fail`));
|
|
|
reject(new Error(`sync code fail`));
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
|
|
|
_installdep() {
|
|
|
var self = this;
|
|
|
this._log('>>>>>>>> install dependencies >>>>>>>');
|
|
|
return new Promise((resolve, reject) => {
|
|
|
self._state('installing dependencies');
|
|
|
sh.cd(self.codePath);
|
|
|
|
|
|
|
|
|
var child = sh.exec('npm i --only=dev', {
|
|
|
silent: self.silent,
|
|
|
async: true
|
|
|
});
|
|
|
|
|
|
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('install dependencies success');
|
|
|
} else {
|
|
|
// reject(new Error(`build code fail`));
|
|
|
}
|
|
|
resolve();
|
|
|
});
|
|
|
})
|
|
|
}
|
|
|
|
|
|
_buildScript() {
|
|
|
var self = this;
|
|
|
this._log(`>>>>>>>>> ${this.project.scripts.build} >>>>>>>>>>>`);
|
|
|
this._log(`>>>>>>>>> build static >>>>>>>>>>>`);
|
|
|
return new Promise((reslove, reject) => {
|
|
|
self._state('script_building');
|
|
|
sh.cd(self.buildPath);
|
|
|
var child = sh.exec(self.project.scripts.build, {
|
|
|
self._state('script building');
|
|
|
sh.cd(self.codePath);
|
|
|
|
|
|
var child = sh.exec('gulp ge --cwd=public', {
|
|
|
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'
|
|
|
}));
|
...
|
...
|
@@ -153,20 +205,34 @@ class Build { |
|
|
});
|
|
|
}
|
|
|
|
|
|
_zipBuild() {
|
|
|
this._state('gziping');
|
|
|
let target = this.buildPath;
|
|
|
let dist = path.join(this.rootPath, `${this.project.name}.tar.gz`);
|
|
|
return Tar.gzip(target, dist);
|
|
|
_cloneToDeploy() {
|
|
|
var self = this;
|
|
|
this._log('>>>>>>>>> clone to deploy folder >>>>>>>>>>');
|
|
|
return new Promise((resolve, reject) => {
|
|
|
let projectRoot = `public/dist/${self.project.name}/`;
|
|
|
|
|
|
self._state('clone to deploy');
|
|
|
|
|
|
// assets folder & version folder
|
|
|
var child = sh.cp('-r', path.join(self.codePath, projectRoot), self.buildPath);
|
|
|
|
|
|
if (child.code === 0) {
|
|
|
console.log('cope to deploy success');
|
|
|
resolve();
|
|
|
} else {
|
|
|
reject(new Error(`copy static fail`));
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
async _state(state) {
|
|
|
ws.broadcast(`/building/${this.project._id}`, {
|
|
|
bid: this.bid,
|
|
|
state: state
|
|
|
state: state,
|
|
|
version: this.version
|
|
|
});
|
|
|
this._log(`>>>>>>>>> ${state} >>>>>>>>>>>`);
|
|
|
await Building.updateState(this.bid, state);
|
|
|
await Building.updateState(this.bid, state, this.version);
|
|
|
}
|
|
|
|
|
|
_log(line) {
|
...
|
...
|
|