Last active
February 1, 2016 08:02
-
-
Save vallieres/c85cad27fc1f8ecf6833 to your computer and use it in GitHub Desktop.
BitBucket Webhook for Hipster Pixel, full details available at https://hipsterpixel.co/2016/01/27/my-blog-publishing-workflow-on-ios-part-1-tutorial
This file contains 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
if( $key != 'aRandomKeyThatMakesItPrivate' ) { | |
return '404'; | |
} | |
// The dir where the Git repo will live on your server | |
$repo_dir = '/home/username/webapps/website_git/'; | |
// Full path to git binary is required if git is not in your PHP user's path. Otherwise just use 'git'. | |
$git_bin_path = '/usr/bin/git'; | |
$update = false; | |
// 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($debug) print("Commit branch = $branch\n"); | |
if ($branch === 'production' || isset($commit->branches) && in_array('production', $commit->branches)) { | |
$update = true; | |
break; | |
} | |
} | |
} | |
if ($update) { | |
// Do a git checkout to the web root | |
exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' pull'); | |
// Log the deployment | |
$commit_hash = shell_exec('cd ' . $repo_dir . ' && ' . $git_bin_path . ' rev-parse --short HEAD'); | |
file_put_contents('deploy.log', date('m/d/Y h:i:s a') . " Deployed branch: " . $branch . " Commit: " . $commit_hash . "\n", FILE_APPEND); | |
} | |
return; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment