git码云配置web hook钩子实现自动部署

<?php

error_reporting(1);
set_time_limit(0);
// 部署目录
$target = '/usr/local/nginx/html/webapi';
// 部署密钥
$token = '123456';
// 部署分支
$branch = 'master';

// token判断
//$request_token = isset($_SERVER['HTTP_X_GITEE_TOKEN']) ? $_SERVER['HTTP_X_GITLEE_TOKEN'] : '';
//if ($token !== $request_token) {
//    die;
//}

// 分支判断
$arr = file_get_contents('php://input', 'r');
$arr = json_decode($arr, true);
if (JSON_ERROR_NONE !== json_last_error()) {
    die;
}
$ref = isset($arr['ref']) ? $arr['ref'] : die;
$ref = explode('/', $ref);
$ref = array_pop($ref);
if ($branch != $ref) {
    die;
}

$cmd = "(cd $target && git pull && composer install -vvv) 2>&1 ";
echo shell_exec($cmd);
    $res_log = "---------------------------------------------------------------".PHP_EOL;
    $res_log .= PHP_EOL."pull start ---------------------------------------------".PHP_EOL;
    $res_log .= '------------------------------------------------------------'.PHP_EOL;
    $res_log .= '-----------当前时区:' . date_default_timezone_get();
    $res_log .= $arr['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $arr['repository']['name'] . '项目的' . $arr['ref'] . '分支push了' . $arr['total_commits_count'] . '个commit:';
    $res_log .= '------------------------------------------------------------'.PHP_EOL;
    $res_log .= "pull end -----------------------------------------------------".PHP_EOL;
    file_put_contents("/usr/local/nginx/html/webhook/".date('Y-m-d',time()).".txt", $res_log, FILE_APPEND);//写入日志到log文件中
 

你可能感兴趣的:(版本控制器,PHP)