Skip to content

Instantly share code, notes, and snippets.

@benjibee
Created February 2, 2015 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benjibee/856789cfa5e6d7a3aa7f to your computer and use it in GitHub Desktop.
Save benjibee/856789cfa5e6d7a3aa7f to your computer and use it in GitHub Desktop.
BitBucket Webhook
<?php
/*
* ButBucket Webhook `git pull` automation v0.1
* @author Benji Bilheimer
*
* If a directory matching the repository name is found
* in BASE_PATH, CD into that directory and run a git pull.
*
*/
define('BASE_PATH', '/htdocs/');
// something like: cd BASE_PATH/name && git pull origin master
$command = '';
if ( isset( $_POST['payload'] ) && !empty( $_POST['payload'] ) ) {
$data = json_decode( $_POST['payload'], true );
$path = BASE_PATH . $data['repository']['name'];
if ( is_readable( $path ) ) {
$command = 'cd ' . $path;
$command .= ' && git pull origin ';
// this should return the current branch
$command .= "\"`git branch | grep -E '^\* ' | sed 's/^\* //g'`\"";
shell_exec($command);
} else {
error_log('Webhook Error: path to repo not readable or doesn\'t exist: ' . $path);
}
} else {
error_log('Webhook Error: POST request missing appropriate key or key is empty.');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment