怎樣把git倉庫的代碼部署到服務(wù)器上,而不含.git文件夾?文末有驚喜~~~
一、環(huán)境
lamp: centos7.2+Apache/2.4.6+PHP5.6
二、原理
將代碼push到github后,通過在github項(xiàng)目中Settings下的Webhooks配置一個(gè)URL,此URL對應(yīng)的是自己服務(wù)器上的一個(gè)php文件,在此文件中通過shell_exec對服務(wù)器發(fā)送shell命令(pull操作),github的webhooks能夠檢測客戶端的push操作,觸發(fā)對URL的http請求,故而實(shí)現(xiàn)了服務(wù)器上的pull操作,實(shí)現(xiàn)代碼部署到服務(wù)器。
三、操作步驟
3.1 配置github下webhooks

編輯切換為居中
3.2 服務(wù)器的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 ? ? ?強(qiáng)制恢復(fù)版本到前一個(gè)穩(wěn)定版
/usr/bin/git clean -f ? ? ? 清理提交的更改
/usr/bin/git pull 2>&1 ? ? ?拉取最新的版本到本地
注意:項(xiàng)目目錄要為www權(quán)限,否則shell命令不成功,第一次要手動(dòng)執(zhí)行g(shù)it clone,之后webhooks才能生效
這里有一份?JetBrains 的激活方法,有需要的來取了!
鏈接:https://pan.baidu.com/s/1jxvnKgTsTbkVmjcPGeTm0g
提取碼:2cg8