配置項目提交到git的時候自動同步服務器代碼php
1、在服務器項目跟目錄新建文件hook.php 代碼以下:html
<?php $json = file_get_contents("php://input"); $data = json_decode($json,true); if (isset($data['ref']) && $data['total_commits_count']>0) { $res = PHP_EOL."pull start ---------------------------------------------".PHP_EOL; $res .= shell_exec("cd /home/www/honli && git pull https://654589023%40qq.com:passwod@gitee.com/xxxx/xxxx.git 2<&1 "); $res_log = '------------------------------------------------------------'.PHP_EOL; $res_log .= $data['user_name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $data['repository']['name'] . '項目的'
. $data['ref'] . '分支push了' . $data['total_commits_count'] . '個commit:'.$data['commits']['message']; $res_log .= $res.PHP_EOL; $res_log .= "pull end -----------------------------------------------------".PHP_EOL; file_put_contents("/home/wwwlogs/webhook/".date('Y-m-d',time()).".txt", $res_log, FILE_APPEND);//寫入日誌到log文件中 } ?>
2、配置碼雲webhook:git
3、確認服務器安裝了git,在web目錄下clone 項目:git clone https://gitee.com/xxxx/xxxx.git;web
4、把git的目錄權限改成web用戶權限: chown -r www:wwwshell
5、手動更新代碼:cd /home/www/honli && git pull;json
6、git pull 衝突或錯誤時,用命令git stash && git pull解決衝突或錯誤bash
在軟件管理中下載寶塔WebHook添加以下腳本服務器
#!/bin/bash echo "" #輸出當前時間 date --date='0 days ago' "+%Y-%m-%d %H:%M:%S" echo "Start" #git項目路徑 gitPath="/data/www/wwwroot/httpurl/" #git 網址 gitHttp="https://gitee.com/giturl" echo "Web站點路徑:$gitPath" #判斷項目路徑是否存在 if [ -d "$gitPath" ]; then cd $gitPath #判斷是否存在git目錄 if [ ! -d ".git" ]; then echo "在該目錄下克隆 git" git clone $gitHttp gittemp mv gittemp/.git . rm -rf gittemp fi #拉取最新的項目文件 git reset --hard origin/master git pull #設置目錄權限 chown -R www:www $gitPath echo "End" exit else echo "該項目路徑不存在" echo "End" exit fi
Git配置如上(待完善)url