Skip to content

Commit

Permalink
You must push to the relevant git branch before deploying to producti…
Browse files Browse the repository at this point in the history
…on or staging
  • Loading branch information
bxjx committed Oct 10, 2012
1 parent 364b46a commit f499504
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
43 changes: 35 additions & 8 deletions deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ var CloudfilesMirror = require("cloudfiles-mirror");
var exec = require('child_process').exec;
var async = require("async");
var colors = require('colors');
var request = require('request');

var auth = 'ac2aa55ec8adecc56501fc32cc22ec38';
var apiKey, env;

function parseArgs(){
Expand All @@ -25,8 +28,15 @@ function getContainer(){
}
}

function checkout(cb){
run('git checkout ' + env, cb);
function getServers(){
if (env === 'staging'){
return ['50.56.185.86'];
}else if (env === 'production'){
return ['50.56.172.114', '198.101.231.69'];
}else{
console.error('unknown environment: ' + env + '. Should be staging or production');
process.exit(1);
}
}

function build(cb){
Expand All @@ -53,12 +63,8 @@ function cdnSync(cb){
}, cb);
}

function push(cb){
run('git push origin HEAD', cb);
}

function run(cmd, cb){
console.log('Running command: ' + cmd);
console.error('Running command: ' + cmd);
exec(cmd, function(err, stdout, stderr){
if (err){
console.error(stdout.blue);
Expand All @@ -71,8 +77,29 @@ function run(cmd, cb){
});
}

function deploy(cb){
var servers = getServers();
async.forEach(servers, function(server, done){
console.error('Triggering deploy on ' + server);
request({method: 'POST', url: 'http://' + server + '/deploy', form: {auth: auth}}, function(err, res, body){
if (err){
console.error(('ERROR: ' + err).red);
return cb('Triggering deploy on ' + server + ' ERROR');
}
if (res.statusCode != 200){
console.error(('ERROR: status code was ' + res.statusCode).red);
console.error(('ERROR: ' + body).red);
return cb('Triggering deploy on ' + server + ' ERROR');
}
console.error(body);
console.error(('Triggering deploy on ' + server + ' OK').green);
cb();
});
});
}

parseArgs();
async.series([checkout, build, cdnSync, push], function(err){
async.series([build, cdnSync, deploy], function(err){
if (err){
console.error(('ERROR: ' + err).red);
process.exit(1);
Expand Down
30 changes: 23 additions & 7 deletions src/DGM/Provider/BaseControllerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,34 @@ public function connect(Application $app)
$committedBranch = (isset($data['ref'])) ? $data['ref'] : $request->get('ref');
$request->request->replace(is_array($data) ? $data : array());

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

if ($committedBranch === "refs/heads/$branch") {
return new Response($exec, $response);
});

$controllers->post('/deploy', function(Request $request) use ($app) {
$auth = $request->get('auth');

$branch = trim($app['config']['branch']);
if ($auth !== 'ac2aa55ec8adecc56501fc32cc22ec38'){
$app['monolog']->addInfo("Rejected deploy with bad auth: {$auth}");
$exec = "Auth error";
$response = 500;
}else{
$app['monolog']->addInfo("Build app on $branch");
$dir = realpath(__DIR__ . '/../../../');
// @TODO refactor epic one-liner?
$exec = shell_exec("cd $dir && git pull && git submodule update --init && composer install 2>&1 >> logs/build_log.txt");
$response = $exec == null ? 500 : 200;
} else {
$exec = "Commit was not on $branch";
$response = 200;
}

return new Response($exec, $response);
Expand Down

0 comments on commit f499504

Please sign in to comment.