Skip to content

Instantly share code, notes, and snippets.

@keithgreer
Forked from nichtich/README.md
Last active December 23, 2021 10:24
Show Gist options
  • Save keithgreer/6c3478d1771ee6cc620afe1338007203 to your computer and use it in GitHub Desktop.
Save keithgreer/6c3478d1771ee6cc620afe1338007203 to your computer and use it in GitHub Desktop.
How to automatically deploy from GitHub - https://keithgreer.dev/git-automatically-deploy-website-github
<?php
// Forked from https://gist.github.com/1809044
// Available from https://keithgreer.uk/git-automatically-deploy-website-github
// Check if client is allowed
$allowed_ips = array(
'207.97.227.', '50.57.128.', '108.171.174.', '50.57.231.', '204.232.175.', '192.30.252.', // GitHub
'123.123.123.123' // Your own IP address
);
$allowed = false;
$headers = apache_request_headers();
if (@$headers["X-Forwarded-For"]):
$ips = explode(",",$headers["X-Forwarded-For"]);
$ip = $ips[0];
else:
$ip = $_SERVER['REMOTE_ADDR'];
endif;
foreach ($allowed_ips as $allow):
if (stripos($ip, $allow) !== false):
$allowed = true;
break;
endif;
endforeach;
// If not allowed
if (!$allowed):
header('HTTP/1.1 403 Forbidden');
exit;
endif;
flush();
// If they are allowed
$commands = array(
'echo $PWD',
'whoami',
'git pull',
'git status',
'git submodule sync',
'git submodule update',
'git submodule status',
'test -e /usr/share/update-notifier/notify-reboot-required && echo "system restart required"',
);
$output = "\n";
// Add new deploy to log file
$log = "####### ".date('Y-m-d H:i:s'). " #######\n";
// Loop through commands
foreach($commands AS $command):
$tmp = shell_exec("$command 2>&1");
$output .= "\$ {$command}\n";
$log .= "\$ $command\n".trim($tmp)."\n";
endforeach;
$log .= "\n";
// Save output to log
file_put_contents ('deploy.log',$log,FILE_APPEND);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment