碼雲配置WebHook自動更新


配置項目提交到git的時候自動同步服務器代碼

一、在服務器項目跟目錄新建文件hook.php 代碼如下:

<?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文件中 } ?>

 

 

二、配置碼雲webhook:

 

 

三、確認服務器安裝了git,在web目錄下clone 項目:git clone https://gitee.com/xxxx/xxxx.git;

四、把git的目錄權限改為web用戶權限: chown -r www:www

五、手動更新代碼:cd /home/www/honli && git pull;

六、git pull 沖突或錯誤時,用命令git stash && git pull解決沖突或錯誤

 

 

 

 

寶塔使用WebHook

在軟件管理中下載寶塔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配置如上(待完善)

 

 

 

 

 

 

 

轉載:https://www.cnblogs.com/mayi168/p/9070505.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM