Skip to content

Instantly share code, notes, and snippets.

@shalakolee
Created April 25, 2017 23:16
Show Gist options
  • Save shalakolee/15a51ce96af8cbd73f81186fbbd96f2c to your computer and use it in GitHub Desktop.
Save shalakolee/15a51ce96af8cbd73f81186fbbd96f2c to your computer and use it in GitHub Desktop.
Git WebHook (windows/linux)
<?php
/**
*
* coded by:
* ▄▄▄ █ █░▓█████ ██████ ▒█████ ███▄ ▄███▓▓█████ ██ ▄█▀ ▒█████
*▒████▄ ▓█░ █ ░█░▓█ ▀ ▒██ ▒ ▒██▒ ██▒▓██▒▀█▀ ██▒▓█ ▀ ██▄█▒ ▒██▒ ██▒
*▒██ ▀█▄ ▒█░ █ ░█ ▒███ ░ ▓██▄ ▒██░ ██▒▓██ ▓██░▒███ ▓███▄░ ▒██░ ██▒
*░██▄▄▄▄██ ░█░ █ ░█ ▒▓█ ▄ ▒ ██▒▒██ ██░▒██ ▒██ ▒▓█ ▄ ▓██ █▄ ▒██ ██░
* ▓█ ▓██▒░░██▒██▓ ░▒████▒▒██████▒▒░ ████▓▒░▒██▒ ░██▒░▒████▒▒██▒ █▄░ ████▓▒░
* ▒▒ ▓▒█░░ ▓░▒ ▒ ░░ ▒░ ░▒ ▒▓▒ ▒ ░░ ▒░▒░▒░ ░ ▒░ ░ ░░░ ▒░ ░▒ ▒▒ ▓▒░ ▒░▒░▒░
*
*
* PHP Webhook, Handling Linux and Windows
* NOTE:
* - If you are using this on windows with IIS, you will need to have your .ssh folder and keys in the base directory
* - of where you have git installed. You will also need to set correct permissions on this script for IUSR
*
* - This script needs execute/read/write permissions
*
**/
/* Find the User this is running under for permissions testing */
print_r(exec('whoami'));
/* relative location of log files */
$GIT_LOG = "logs/gitlog.log";
$HUB_LOG = "logs/payloads.log";
/* set this to true if you are on windows and IIS */
$WINDOWS = false;
/* add a timestamp to the log */
$prepend = "(".date("m.d.y @ G:i:s").") ";
if($payload = file_get_contents('php://input')) {
try {
/* json decode the payload */
$payload = json_decode($payload);
} catch(Exception $ex) {
/* write the error to the log and exit */
file_put_contents($HUB_LOG, $prepend."error: " . $ex . "\n", FILE_APPEND);
exit(0);
}
/**
* here you can do different things based on the branch.
* I have left two different branches here so you can see whats going on.
*/
if($payload->ref == "refs/heads/development"){ /* development branch */
if($WINDOWS){
$result .= shell_exec('cmd /c "cd c:\inetpub\wwwroot\yoursite\ &git fetch origin &git pull origin development" ');
}else{
$result = `cd /var/www/yoursite/development/public_html && git fetch origin && git merge origin/development`;
}
}elseif($payload->ref == "refs/heads/production"){ /* production branch */
if($WINDOWS){
$result .= shell_exec('cmd /c "cd c:\inetpub\wwwroot\yoursite\ &git fetch origin &git pull origin production" ');
}else{
$result = `cd /var/www/yoursite/production/public_html && git fetch origin && git merge origin/production`;
}
}else{
echo "Wrong Branch";
exit(0);
}
/* log the results */
file_put_contents($GIT_LOG, $prepend.$result."\n", FILE_APPEND);
/* log the payload that came through */
file_put_contents($HUB_LOG, $prepend.print_r($payload, TRUE), FILE_APPEND);
/* debugging */
echo $result;
} else {
/* log the invalid request */
file_put_contents($HUB_LOG, $prepend."invalid request\n", FILE_APPEND);
/* debugging */
echo "failed request";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment