Skip to content

Instantly share code, notes, and snippets.

@ryross
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryross/55f2d5f568833af27242 to your computer and use it in GitHub Desktop.
Save ryross/55f2d5f568833af27242 to your computer and use it in GitHub Desktop.
<?php
$repo_dir = '/var/www/';
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'.
$git_bin_path = 'git';
$branch = '';
//$update = false;
$update = true;
/*
// Parse data from Bitbucket hook payload
$payload = json_decode($_POST['payload']);
if (empty($payload->commits)) {
// When merging and pushing to bitbucket, the commits array will be empty.
// In this case there is no way to know what branch was pushed to, so we will do an update.
$update = true;
} else {
foreach ($payload->commits as $commit) {
$branch = $commit->branch;
if ($branch === 'production' || isset($commit->branches) && in_array('production', $commit->branches)) {
$update = true;
break;
}
}
}
*/
function addlog($log)
{
file_put_contents('deploy.txt', $log, FILE_APPEND);
}
function run($command)
{
$output = '';
exec($command, $output);
addlog("[" . date('m/d/Y h:i:s a') . "] " . "$command\n");
if (count($output)) {
addlog("\t" . implode("\n\t", $output) ."\n");
}
}
if ($update) {
// Do a git checkout to the web root
run('cd ' . $repo_dir . ' && ' . $git_bin_path . ' fetch');
run('cd ' . $repo_dir . ' ' . $git_bin_path . ' checkout -f');
run('cd ' . $repo_dir . ' && composer install');
run('cd ' . $repo_dir . ' && php artisan migrate');
// Log the deployment
$commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD');
addlog("[" . date('m/d/Y h:i:s a') . "] " . "Deployed branch: " . $branch . " Commit: " . $commit_hash);
addlog("========================================\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment