Skip to content

Instantly share code, notes, and snippets.

@allex
Last active August 29, 2015 14:20
Show Gist options
  • Save allex/433fa22c0af8be914c44 to your computer and use it in GitHub Desktop.
Save allex/433fa22c0af8be914c44 to your computer and use it in GitHub Desktop.
<?php
/**
* Simple git hooks handler for web automate deployment.
*
* @author Allex Wang ([email protected])
* Last Modified: Wed Dec 24, 2014 03:49PM
* GistID: 433fa22c0af8be914c44
* GistURL: https://gist.github.com/433fa22c0af8be914c44
*/
$CUR_DIR = dirname(__FILE__);
$TMP_DIR = $CUR_DIR . "/cache";
$LOG_FILE = "/tmp/githook.log";
function exec_cmd($cmd) {
$output = system($cmd);
if (!empty($output)) {
return implode(" ", $output);
}
return NULL;
}
function get_request_body() {
$body = @file_get_contents('php://input');
if (!empty($body)) {
return json_decode($body, true);
}
return NULL;
}
$action=$_GET['act'];
if (empty($action)) {
exit('403: Access forbidden');
}
$req_body = get_request_body();
if (empty($req_body)) {
exit('407: Invalid request');
}
switch ($action) {
case "update":
$repos_name = @$_GET['n'];
if (empty($repos_name)) {
$repos_name = $req_body['repository']['name'];
}
file_put_contents("$TMP_DIR/$repos_name.json", json_encode($req_body));
$repos_branch = $req_body['ref'];
$commit = $req_body['after'];
$cmd = "sh $CUR_DIR/.bin/git-deploy.sh $repos_name $repos_branch $commit &>> ${LOG_FILE}&";
echo exec_cmd($cmd);
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment