Created
September 2, 2019 02:21
-
-
Save zhaofeng-shu33/948a3732f71578e9806b28e6fb948a0e to your computer and use it in GitHub Desktop.
git webhook pull code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function exitJson($err, $msg , $result='') | |
{ | |
echo json_encode(array('err'=>$err, 'msg'=>$msg , 'result'=>$result)); | |
exit(); | |
} | |
$postdata = file_get_contents("php://input"); | |
$jsondata = json_decode($postdata); | |
if($jsondata->ref == 'refs/heads/master'){ // only deploy on master branch | |
$return_val; | |
$output = array(); | |
$ok_str = 'ok'; | |
$current_directory = dirname(__FILE__); | |
if(strpos($jsondata->compare, basename($current_directory))>0){ | |
$cmd = 'cd ' . $current_directory . ' && git pull origin master'; | |
} | |
else{ | |
$cmd = 'git pull origin master'; | |
} | |
$cmd .= ' 2>&1'; | |
exec($cmd, $output, $return_val); | |
if($return_val != 0){ | |
// record error log | |
$err_msg = join("\n", $output); | |
exitJson(1, $output); | |
} | |
else{ | |
exitJson(0, $ok_str); | |
} | |
} | |
else{ | |
exitJson(1,'invalid request'); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment