Skip to content

Instantly share code, notes, and snippets.

@jellehak
Last active November 11, 2019 16:06
Show Gist options
  • Save jellehak/5be83e24eae383bb9cae974ca620ef49 to your computer and use it in GitHub Desktop.
Save jellehak/5be83e24eae383bb9cae974ca620ef49 to your computer and use it in GitHub Desktop.
<?php
// Script to handle github webhook
// https://developer.github.com/webhooks/
// =========================
// CONFIG
// =========================
// Set in Github
$secret = ""; // (optional but recommended) this makes this endpoint only availible to github e.g. veryverysecret
$remote = "[email protected]:yourorg/reponame.git";
$branch = "master";
$path = "..";
// =========================
// Code
// =========================
// Check secret
if (isset($secret)) {
// https://developer.github.com/webhooks/securing/
$body = file_get_contents('php://input');
$signature = str_replace("sha1=","", $_SERVER['HTTP_X_HUB_SIGNATURE']);
$hash = hash_hmac('sha1', $body, $secret);
if ($signature != $hash) {
exit ("Not for you");
}
}
if($_GET['action'] == 'pull') {
echo "Fetching...\n";
function execPrint($command) {
$result = array();
exec($command, $output, $result);
print_r($output);
}
$cmd = "cd $path && git pull $remote $branch";
echo "Running: $cmd\n";
echo "\n";
// Print the exec output inside of a pre element
print(execPrint($cmd));
} else {
exit("Nothing");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment