怎样把git仓库的代码部署到服务器上,而不含.git文件夹?文末有惊喜~~~
一、环境
lamp: centos7.2+Apache/2.4.6+PHP5.6
二、原理
将代码push到github后,通过在github项目中Settings下的Webhooks配置一个URL,此URL对应的是自己服务器上的一个php文件,在此文件中通过shell_exec对服务器发送shell命令(pull操作),github的webhooks能够检测客户端的push操作,触发对URL的http请求,故而实现了服务器上的pull操作,实现代码部署到服务器。
三、操作步骤
3.1 配置github下webhooks

编辑切换为居中
3.2 服务器的php文件
<?php // GitHub Webhook Secret. // Keep it the same with the 'Secret' field on your Webhooks / Manage webhook page of your respostory. $secret = "1234"; // Path to your respostory on your server. // e.g. "/var/www/respostory" $path = "/webdata/basic"; // Headers deliveried from GitHub $signature = $_SERVER['HTTP_X_HUB_SIGNATURE']; if ($signature) { $hash = "sha1=".hash_hmac('sha1', file_get_contents("php://input"), $secret); if (strcmp($signature, $hash) == 0) { echo shell_exec("cd {$path} && /usr/bin/git reset --hard origin/master && /usr/bin/git clean -f && /usr/bin/git pull 2>&1") ; exit(); } } http_response_code(404); ?>
四、命令说明
/usr/bin/git reset --hard origin/master 强制恢复版本到前一个稳定版
/usr/bin/git clean -f 清理提交的更改
/usr/bin/git pull 2>&1 拉取最新的版本到本地
注意:项目目录要为www权限,否则shell命令不成功,第一次要手动执行git clone,之后webhooks才能生效
这里有一份 JetBrains 的激活方法,有需要的来取了!
链接:https://pan.baidu.com/s/1jxvnKgTsTbkVmjcPGeTm0g
提取码:2cg8