Close

Setup code deployment using Github.

# ssh-keygen -t ed25519 -C "youremail@email.com"
# cat /home/username/.ssh/id_ed25519.pub
# git init
# git branch -m master
or
# git config --global init.defaultBranch master
# git remote add "origin" git@github.com:usename/yourgit.git
# git pull --set-upstream origin master
# git add public_html/hook.php
# chmod 600 public_html/hook.php
# git pull
or
# git pull "origin" master

webhook.php

<?php
$secret = 'yoursecretcode';
list($hashAlgo, $signature) = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE']);
$payload = $HTTP_RAW_POST_DATA ?: file_get_contents('php://input');

$compute = hash_hmac($hashAlgo, $payload, $secret);
$valid = hash_equals($compute, $signature);

if ($valid === false) {
	exit('Incorrect data');
}else{
    // array of commands
    $commands = array(
        'echo $PWD',
        'whoami',
        //'git pull',
      	'git --work-tree=$PWD pull',
        'git status',
        'git submodule sync',
        'git submodule update',
        'git submodule status',
    );

    // exec commands
    $output = '';
    foreach($commands AS $command){
        $tmp = shell_exec($command);
        
        $output .= "<span style=\"color: #6BE234;\">\$</span><span style=\"color: #729FCF;\">{$command}\n</span><br />";
        $output .= htmlentities(trim($tmp)) . "\n<br /><br />";
    }
}
?>

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>GIT DEPLOYMENT SCRIPT</title>
</head>
<body style="background-color: #000000; color: #FFFFFF; font-weight: bold; padding: 0 10px;">
<div style="width:700px">
    <div style="float:left;width:350px;">
    <p style="color:white;">Git Deployment Script</p>
    <?php echo $output; ?>
    </div>
</div>
</body>
</html>

webhook.php with sha256 validation

<?php
define("SECRET", "yoursecretcode");

$body = file_get_contents("php://input");
// $decodedBody = json_decode(urldecode($body));

function verifySignature($body){
  $headers = getallheaders();
  return hash_equals('sha256='.hash_hmac('sha256', $body, SECRET), isset($headers['X-Hub-Signature-256']) ? $headers['X-Hub-Signature-256'] : ''); 
}

if (verifySignature($body) !== false) {

    // array of commands
    $commands = array(
        'echo $PWD',
        'whoami',
        //'git pull',
        //'git --work-tree=$PWD pull',
        'git --work-tree=/home/username/public_html pull',
        'git status',
        //'git submodule sync',
        //'git submodule update',
        'git submodule status',
    );

    // exec commands
    $output = '';
    foreach($commands AS $command){
        $tmp = shell_exec($command);
        
        $output .= "<span style=\"color: #6BE234;\">\$</span><span style=\"color: #729FCF;\">{$command}\n</span><br />";
        $output .= htmlentities(trim($tmp)) . "\n<br /><br />";
    }
} else {
  // unverified
  http_response_code(404);
  echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>';
}

?>