-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployer.js
73 lines (63 loc) · 2.15 KB
/
deployer.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
var async = require('async');
var shell = require('shelljs');
var mkdirp = require('mkdirp');
var projects = require('./projects');
var config = require('./config');
exports.deploy = function (repository, branch, callback) {
var checkRepository = function (project, cb) {
return cb(project.repository === repository.full_name && project.branch_to_watch === branch);
}
console.log('Searching for', repository.full_name + '...');
async.detect(projects, checkRepository, function (project) {
if (!project) return callback('No matching project with received GitHub hook or reference branch and branch to watch does not match.');
console.log('Project found.');
var root_deployment_path = config.root_deployment_path;
var project_directory_path = root_deployment_path + project.name;
async.waterfall([
// Go to project directory, if it exists
function (cb) {
shell.cd(project_directory_path);
cb();
},
// If project directory does not exist, create and pull the repository
function (cb) {
if (shell.error()) {
mkdirp(project_directory_path, null, function (err, made) {
if (err) return console.log(err);
console.log('Created', made);
shell.cd(project_directory_path);
shell.exec('git init');
shell.exec('git remote add origin [email protected]:' + project.repository + '.git');
shell.exec('git pull -u origin ' + project.branch_to_watch, function (code, output) {
shell.exec('git branch --set-upstream-to=origin/' + project.branch_to_watch);
cb();
});
});
} else {
cb();
}
},
// Pull updates from GitHub
function (cb) {
shell.exec('git pull', function (code, output) {
cb();
});
},
// Execute build commands
function (cb) {
var commands = project.build_commands;
if (commands && commands.length) {
for (var i in commands) {
var command = commands[i];
console.log('Executing command:', command)
shell.exec(command);
}
}
cb();
}
], function (err, results) {
console.log('All automated deployment tasks for', project.name, 'project have been completed!');
callback();
});
});
}