Last active
August 29, 2015 14:14
-
-
Save clehner/7566094ce0bec7a7cb80 to your computer and use it in GitHub Desktop.
GitHub Web Hook for updating a repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header('Content-Type: text/plain'); | |
$secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
$branch = 'master'; | |
$hub_signature = @$_SERVER['HTTP_X_HUB_SIGNATURE']; | |
if (!$hub_signature) { | |
http_response_code(403); | |
exit('missing signature'); | |
} | |
list($algo, $hash) = explode('=', $hub_signature, 2); | |
try { | |
$payload = file_get_contents('php://input'); | |
$data = json_decode($payload); | |
} catch (Exception $e) { | |
http_response_code(400); | |
exit('invalid payload'); | |
} | |
if ($hash !== hash_hmac($algo, $payload, $secret)) { | |
http_response_code(403); | |
exit('invalid secret'); | |
} | |
switch (@$_SERVER['HTTP_X_GITHUB_EVENT']) { | |
case 'ping': | |
echo $data->zen; | |
break; | |
case 'push': | |
if ($data->ref !== "refs/heads/$branch") { | |
exit("branch not $branch, nothing done"); | |
} | |
$clone_url = escapeshellarg($data->repository->clone_url); | |
$sha = escapeshellarg($data->after); | |
passthru("git fetch $clone_url $branch; git reset --hard $sha", $ret); | |
if ($ret != 0) { | |
http_response_code(500); | |
} | |
break; | |
default: | |
http_response_code(204); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment