Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.