Skip to content

Commit f499504

Browse files
committed
You must push to the relevant git branch before deploying to production or staging
1 parent 364b46a commit f499504

File tree

2 files changed

+58
-15
lines changed

2 files changed

+58
-15
lines changed

deploy.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ var CloudfilesMirror = require("cloudfiles-mirror");
33
var exec = require('child_process').exec;
44
var async = require("async");
55
var colors = require('colors');
6+
var request = require('request');
7+
8+
var auth = 'ac2aa55ec8adecc56501fc32cc22ec38';
69
var apiKey, env;
710

811
function parseArgs(){
@@ -25,8 +28,15 @@ function getContainer(){
2528
}
2629
}
2730

28-
function checkout(cb){
29-
run('git checkout ' + env, cb);
31+
function getServers(){
32+
if (env === 'staging'){
33+
return ['50.56.185.86'];
34+
}else if (env === 'production'){
35+
return ['50.56.172.114', '198.101.231.69'];
36+
}else{
37+
console.error('unknown environment: ' + env + '. Should be staging or production');
38+
process.exit(1);
39+
}
3040
}
3141

3242
function build(cb){
@@ -53,12 +63,8 @@ function cdnSync(cb){
5363
}, cb);
5464
}
5565

56-
function push(cb){
57-
run('git push origin HEAD', cb);
58-
}
59-
6066
function run(cmd, cb){
61-
console.log('Running command: ' + cmd);
67+
console.error('Running command: ' + cmd);
6268
exec(cmd, function(err, stdout, stderr){
6369
if (err){
6470
console.error(stdout.blue);
@@ -71,8 +77,29 @@ function run(cmd, cb){
7177
});
7278
}
7379

80+
function deploy(cb){
81+
var servers = getServers();
82+
async.forEach(servers, function(server, done){
83+
console.error('Triggering deploy on ' + server);
84+
request({method: 'POST', url: 'http://' + server + '/deploy', form: {auth: auth}}, function(err, res, body){
85+
if (err){
86+
console.error(('ERROR: ' + err).red);
87+
return cb('Triggering deploy on ' + server + ' ERROR');
88+
}
89+
if (res.statusCode != 200){
90+
console.error(('ERROR: status code was ' + res.statusCode).red);
91+
console.error(('ERROR: ' + body).red);
92+
return cb('Triggering deploy on ' + server + ' ERROR');
93+
}
94+
console.error(body);
95+
console.error(('Triggering deploy on ' + server + ' OK').green);
96+
cb();
97+
});
98+
});
99+
}
100+
74101
parseArgs();
75-
async.series([checkout, build, cdnSync, push], function(err){
102+
async.series([build, cdnSync, deploy], function(err){
76103
if (err){
77104
console.error(('ERROR: ' + err).red);
78105
process.exit(1);

src/DGM/Provider/BaseControllerProvider.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,34 @@ public function connect(Application $app)
2727
$committedBranch = (isset($data['ref'])) ? $data['ref'] : $request->get('ref');
2828
$request->request->replace(is_array($data) ? $data : array());
2929

30-
# Only update this deployment if the commit was on the current branch
3130
$branch = trim($app['config']['branch']);
32-
$app['monolog']->addInfo("Build app on $branch");
31+
if ($branch !== "master"){
32+
$app['monolog']->addInfo("Rejected github hook as not on master branch");
33+
$exec = "Commit was not on master and will be ignored";
34+
$response = 200;
35+
} else{
36+
$app['monolog']->addInfo("Build app on $branch");
37+
$dir = realpath(__DIR__ . '/../../../');
38+
$exec = shell_exec("cd $dir && git pull && git submodule update --init && composer install 2>&1 >> logs/build_log.txt");
39+
$response = $exec == null ? 500 : 200;
40+
}
3341

34-
if ($committedBranch === "refs/heads/$branch") {
42+
return new Response($exec, $response);
43+
});
44+
45+
$controllers->post('/deploy', function(Request $request) use ($app) {
46+
$auth = $request->get('auth');
47+
48+
$branch = trim($app['config']['branch']);
49+
if ($auth !== 'ac2aa55ec8adecc56501fc32cc22ec38'){
50+
$app['monolog']->addInfo("Rejected deploy with bad auth: {$auth}");
51+
$exec = "Auth error";
52+
$response = 500;
53+
}else{
54+
$app['monolog']->addInfo("Build app on $branch");
3555
$dir = realpath(__DIR__ . '/../../../');
36-
// @TODO refactor epic one-liner?
3756
$exec = shell_exec("cd $dir && git pull && git submodule update --init && composer install 2>&1 >> logs/build_log.txt");
3857
$response = $exec == null ? 500 : 200;
39-
} else {
40-
$exec = "Commit was not on $branch";
41-
$response = 200;
4258
}
4359

4460
return new Response($exec, $response);

0 commit comments

Comments
 (0)