在技术团队讨论中,咱们决定从svn 迁移到 git ,因而使用了gitlab,代码自动部署使用了webhookphp
服务器环境必须先安装git 环境,webhook 依赖php运行环境,同时须要使用shell_exec 和 exec 等函数。使用前先开启php部分可执行函数。html
$ which php /usr/local/php/bin/php $ sudo vi /usr/local/php/etc/php.ini ;disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,fsocket,popen
由于gitlab 的 webhooks 的使用者都是web 服务器在用,我使用的是nginx ,因此必须的知道nginx和php-fpm的全部者是谁linux
$ ps aux | grep php $ ps aux | grep php-fpm
个人环境下是nginx
nginx
sudo -Hu nginx ssh-keygen -t ras #一路回车下去就能够了
而后把公钥添加到gitlab 上去,不要问我怎么添加的,若是使用过github,就应该知道这个是怎么回事git
git clone git@git.xxxx.com:test/test.git /data/www/git_test/
<?php //git webhook 自动部署脚本 //项目存放物理路径 $path = "/data/www/git_test/test/"; $requestBody = file_get_contents("php://input"); if (empty($requestBody)) { die('send fail'); } $content = json_decode($requestBody, true); //如果主分支且提交数大于0 if ($content['ref']=='refs/heads/master' && $content['total_commits_count']>0) { $res = shell_exec("cd {$path} && git pull 2>&1");//以nginx用户运行 $res_log = '-------------------------'.PHP_EOL; $res_log .= $content['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . $content['total_commits_count'] . '个commit:' . PHP_EOL; $res_log .= $res.PHP_EOL; file_put_contents("git-webhook.txt", $res_log, FILE_APPEND);//追加写入 }
修改这个文件的全部者为 nginxgithub
chown -R nginx:nginx webhook.php
参考文章
1.http://www.piaoyi.org/linux/G...
2.https://docs.gitlab.com/ce/us...
3.https://m.aoh.cc/149.htmlweb