通常項目中咱們都會用到Git或者SVN等工具進行版本控制,在本地開發完後,再使用ssh鏈接服務器進行git pull拉取倉庫代碼,這裏給你們分享下 如何讓服務端自動拉取代碼php
官方歸納是這麼說的:git
Webhooks allow you to build or set up GitHub Apps which subscribe to certain events on GitHub.com. When one of those events is triggered, we'll send a HTTP POST payload to the webhook's configured URL. Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production server. You're only limited by your imagination.github
Webhooks can be installed on an organizationor a specific repository. Once installed, the webhook will be triggered each time one or more subscribed events occurs.web
翻譯了一下大概的意思就是:shell
初始化本地倉庫並鏈接遠程倉庫json
echo "# webhooks" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/ITHcc/webhooks.git
git push -u origin master
複製代碼
服務器git clone遠程倉庫數組
git clone 你的倉庫地址
服務器
添加webhooks.php腳本文件,用來接收github通知dom
<?php
//建議在這裏驗證簽名
exec("git pull");
複製代碼
配置Webhooks通知ssh
在倉庫的Settings中選擇Webhooks添加配置
測試可否通知成功
綠色的是請求成功的,紅色的是失敗的
修改php.ini 中的disable_functions配置,刪除其中的exec函數
disable_functions
配置是服務器禁用的危險函數
將項目文件要與php-fpm爲同一用戶組不然沒權限 chown -r www:www webhooks
打開在webhooks中配置的通知地址所對應的腳本文件,我這裏配置的是 domain.com/webhooks.ph…
因此對應的腳本文件就是在我host主機根目錄的webhooks.php文件,在文件中添加校驗代碼
<?php
//webhooks頁面配置的祕鑰
$key = "123456";
//接收Github發送給咱們的數據
$contents = file_get_contents("php://input");
//將json轉爲數組
$data = json_decode($contents);
//獲取請求通中的sha1簽名
$signature = $_SERVER['HTTP_X_HUB_SIGNATURE'];
list($algo,$hash) = explode("=",$signature);
$newHash = hash_hmac($algo,$contents,$key);
//驗證本身生產的sha1與Github發給咱們的sha1是否一致
if($hash==$newHash){
//執行git pull
exec("git pull");
}else{
echo "簽名錯誤";
}
複製代碼