經過php 執行git pull 自動部署

安裝git,生成公鑰,加公鑰 都有教程,須要注意的是如下幾個方面php

php以www用戶執行,須要給到權限才能執行git pullhtml

用合適的工具幹合適的活,php去操做git pull只是在測試環境圖方便搞的,若是正式環境,應該考慮用其餘方式,例如系統定時任務設置觸發條件等。git

一、vim /etc/sudoers  shell

root下加一行json

www localhost=(ALL) NOPASSWD:/usr/bin/git (安裝git後目錄用which git查看)vim

二、容許php執行exec shell_execssh

三、改目錄權限工具

四、.git改權限可寫,我直接改爲777了測試

五、.git/config https地址改爲ssh的spa

<?
header("Content-type: text/html; charset=utf-8");
$js = json_decode($_REQUEST["hook"]); // json轉換
if ($js->password != "password") {
    die("ERROR!"); // 判斷密碼
}
$fp = fopen("./log.txt", 'a');
$lastcommit = $js->push_data->commits[count($js->push_data->commits) - 1]; // 獲取最後的commit
if (strstr($lastcommit->message, "release")) // 這裏意爲:若是最後的commit包含"release"則進行自動發佈。
{
    exec("cd ./"); // 進入目錄
    exec("git pull origin master"); // 進行git拉取,前提是使用了ssh
    fwrite($fp, "※" . date('Y-m-d H:i:s') . "\t" . $lastcommit->message . "\t" . $lastcommit->author->name . "\n"); // 進行記錄
} else {
    fwrite($fp, date('Y-m-d H:i:s') . "\t" . $lastcommit->message . "\t" . $lastcommit->author->name . "\n");
}
相關文章
相關標籤/搜索