Skip to content

Instantly share code, notes, and snippets.

@inxilpro
Created October 20, 2016 18:12
Show Gist options
  • Select an option

  • Save inxilpro/7488b87ce7a901e0682ae54f972d8d82 to your computer and use it in GitHub Desktop.

Select an option

Save inxilpro/7488b87ce7a901e0682ae54f972d8d82 to your computer and use it in GitHub Desktop.
<?php
define('GITHUB_SECRET', '');
set_time_limit(120);
header('Content-Type', 'text/plain');
// Ensure signature exists
if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
http_response_code(403);
die('Missing signature');
}
// Ensure that signature is formed as expected
$signature = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE']);
if (2 !== count($signature)) {
http_response_code(400);
die('Malformed signature');
}
// Parse signature
$algos = hash_algos();
list($algo, $hash) = $signature;
if (!in_array($algo, $algos)) {
http_response_code(400);
die("Unknown hashing algo: $algo");
}
// Load payload
$payload = file_get_contents('php://input');
// Check signature
$expectedHash = hash_hmac($algo, $payload, GITHUB_SECRET);
if ($expectedHash !== $hash) {
http_response_code(403);
die('Invalid signature');
}
// Parse data
$data = json_decode($payload, true);
// Check whether this is a master push
if ('refs/heads/master' !== $data['ref']) {
die("Skipping (not master branch): {$data['ref']}");
}
// Configure commands
$commands = [
'example/laravel-app' => [
'cd /var/www/',
'pwd',
'git pull',
'composer install',
'php artisan optimize',
'php artisan config:cache',
'php artisan route:cache',
]
];
// Run
$repo = $data['repository']['full_name'];
if (isset($commands[$repo])) {
$command = implode(' && ', $commands[$repo]);
exec($command, $output, $returnCode);
if (0 === $returnCode) {
echo "[OK] $command\n";
} else {
echo "[ERROR $returnCode] $command\n";
http_response_code(500);
}
echo " - ";
echo implode("\n - ", $output);
echo "\n";
echo "Done";
} else {
http_response_code(400);
die("Repository not configured: $repo\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment