Skip to content

Instantly share code, notes, and snippets.

@cflems
Created August 17, 2017 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cflems/0499e70c92069b32bcfd35ecf0448b4f to your computer and use it in GitHub Desktop.
Save cflems/0499e70c92069b32bcfd35ecf0448b4f to your computer and use it in GitHub Desktop.
PHP Github Webhook Template
<?php
function sh ($cmd, $cwd) {
$descriptorspec = array(
1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
2 => array('pipe', 'w') // stderr
);
$resource = proc_open($cmd, $descriptorspec, $pipes, $cwd);
if (is_resource($resource)) {
$output = stream_get_contents($pipes[2]);
$output .= PHP_EOL;
$output .= stream_get_contents($pipes[1]);
$output .= PHP_EOL;
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($resource);
return $output;
}
}
$secret = '{AHEM}';
$sig = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE']);
$payload = file_get_contents('php://input');
if ($sig[1] == hash_hmac($sig[0], $payload, $secret) && $_SERVER['HTTP_X_GITHUB_EVENT'] == 'push') {
$load = json_decode($payload, true);
if ($load['ref'] != 'refs/heads/master') {
file_put_contents('gh-hook.log', '[SKIP] '.$_SERVER['REMOTE_ADDR'].' on '.$load['ref'].PHP_EOL, FILE_APPEND);
die;
}
sh('git pull -q origin master', '/home/theexoni/public_html');
file_put_contents('gh-hook.log', '[OK] Attempting to pull, initiated by: '.$_SERVER['REMOTE_ADDR'].PHP_EOL, FILE_APPEND);
} else {
file_put_contents('gh-hook.log', '[NOK] Signature failure: '.$_SERVER['REMOTE_ADDR'].PHP_EOL, FILE_APPEND);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment