在php.ini文件中,把disable_fuction配置中解除shell_exec函数禁用
error_reporting(1); set_time_limit(0); // 部署目录 $target = '/www/www.baidu.com'; // 部署密钥 $token = '123456789'; // 部署分支 $branch = 'develop'; // token判断 $request_token = isset($_SERVER['HTTP_X_GITLAB_TOKEN']) ? $_SERVER['HTTP_X_GITLAB_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) > /dev/null 2>&1 &"; echo shell_exec($cmd);
以上代码适配的gitlab
如果适配gitee等其他git代码托管平台,修改HTTP_X_GITLAB_TOKEN即可