Last active
August 29, 2015 14:02
-
-
Save ryross/55f2d5f568833af27242 to your computer and use it in GitHub Desktop.
Simple Git deply ( from http://f6design.com/journal/2013/11/19/automated-git-deployments-from-bitbucket/ )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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