djmonta
2/27/2015 - 4:14 PM

Github Webhook Deploy Script

Github Webhook Deploy Script

<?php
$logfile = dirname(__FILE__).'/hook.log';
$secret = 's.e.c.r.e.t.'; // Webhook Secret
 
$headers = getallheaders();
$hubSignature = $headers['X-Hub-Signature'];
 
// Split signature into algorithm and hash
list($algo, $hash) = explode('=', $hubSignature, 2);
 
// Get payload
$payload = file_get_contents('php://input');
 
// Calculate hash based on payload and the secret
$payloadHash = hash_hmac($algo, $payload, $secret);
 
// Check if hashes are equivalent
if ($hash !== $payloadHash) {
    // Kill the script or do something else here.
    file_put_contents($logfile, date("[Y-m-d H:i:s]")." invalid access: ".$_SERVER['REMOTE_ADDR']."\n", FILE_APPEND|LOCK_EX);
} else {
	// Your code here.
	$data = json_decode($payload);
	if ($data->ref === 'refs/heads/master') {
    `cd /path/to/the/repository1; git pull origin master; cd /path/to/the/repository1; git pull origin master`;
    file_put_contents($logfile, date("[Y-m-d H:i:s]")." ".$_SERVER['REMOTE_ADDR']." git pulled: ".$data->head_commit->message."\n", FILE_APPEND|LOCK_EX);
	}
}