Last active
December 22, 2021 23:39
-
-
Save pascalchevrel/463fc7b1f0382f187820 to your computer and use it in GitHub Desktop.
Example script to use with github webhooks to update a site when pushing on master
This file contains hidden or 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 | |
/* 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