Skip to content

Instantly share code, notes, and snippets.

@MikeTodd
Forked from cowboy/github_post_recieve.php
Last active May 19, 2016 10:15
Show Gist options
  • Save MikeTodd/5862043 to your computer and use it in GitHub Desktop.
Save MikeTodd/5862043 to your computer and use it in GitHub Desktop.
<?php
// Use in the "Post-Receive URLs" section of your Bitbucket repo.
if ( $_POST['payload'] ) {
$data = json_decode($_POST['payload']);
$commits = $data->{"commits"};
// This code checks to see if commits were made to a specific branch, and if so, performs a git reset/pull
foreach ($commits as $commit_obj) {
$branch = $commit_obj->{"branch"};
if ($branch == 'my-branch') {
$output = shell_exec('cd /path/to/my/httpdocs && git reset --hard HEAD && git pull');
echo "Output: <pre>$output</pre>";
// TODO: Check for error statuses, respond appropriately
}
}
}
?>
@MikeTodd
Copy link
Author

This script will check for updates to a given branch, and if so, perform a Git reset and pull. This is good for use if, for example, you are working on a development branch for a website and need your dev server to automatically pull every time a commit is made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment