Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example script to use with github webhooks to update a site when pushing on master
<?php
/* Webhook to update a repo for each push on GitHub. */
date_default_timezone_set('Europe/Paris');
$header_match = 'HTTP_X_HUB_SIGNATURE';
$secret_key = 'my_secret_key_in_github_webhook';
$branch = 'master';
function logHookResult($message , $success = false) {
$log_headers = "$message\n";
if (! $success) {
foreach ($_SERVER as $header => $value) {
$log_headers .= "$header: $value \n";
}
}
file_put_contents(__DIR__ . '/github_log.txt', $log_headers);
}
if (isset($_SERVER[$header_match])) {
$validation = hash_hmac(
'sha1',
file_get_contents("php://input"),
$secret_key
);
if ($validation == explode('=', $_SERVER[$header_match])[1]) {
exec("git checkout $branch ; git pull origin $branch");
logHookResult('Last update: ' . date('d-m-Y H:i:s'), true);
} else {
logHookResult('Invalid github secret');
}
} else {
logHookResult("{$header_match} header missing, define a secret key for your project in GitHub");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment