gitlab鈎子搭建


目標:在本地開發機上push代碼到GitLab倉庫時,通過鈎子同步到測試服務器

准備工作
GitLab 服務器一台
測試服務器一台
本地開發服務器一台

1.在gitlab上新建一個項目,名稱test
2.在本地機上開發克隆gitlab項目
3.在本地機上配置gitlab事件觸發回調地址 (http://domain.com/Hook/hook.php)

a.建立Hooks項目 建議目錄:Hooks |- item1 |- hook.php |- logs |- sh |- git_dev_release.sh |- item2

4.設置gitlab項目回調地址並進行測試

注意:需要注意的本地用戶和Nginx請求用戶都需要把公鑰扔給測試服務器上

hook.php 代碼

// 接收gitlab回調時推過來的流信息
$requestBody = file_get_contents("php://input"); // 數據類型轉換
$content = json_decode($requestBody, true); // 將流信息寫入日志
file_put_contents("/home/xingfupeng/hooks/test/logs/test.log",var_export($content, true) . PHP_EOL,FILE_APPEND); // 定義執行的命令
$devSh="/bin/bash /home/xingfupeng/hooks/test/sh/git_dev_release.sh"; if($content['ref'] == "refs/heads/dev" && $content['total_commits_count'] > 0) { // 當 dev 分支提交的時候且提交的文件數量大於0時才進行同步操作
  system($devSh) ; }

 

git_dev_release.sh 腳本代碼

LOG=/home/xingfupeng/hooks/test/logs/git_dev_release_test.log echo "`date +%Y-%m-%d-%H:%M:%S` Begin update test code...$1..." >>$LOG cd /home/xingfupeng/test git checkout dev >>$LOG git pull origin dev >>$LOG echo "=============Start to update the test program!!!=============">> $LOG # 同步命令操作,將本地的/home/xingfupeng/test/文件夾下的所有文件同步到測試www@192.168.20.229:/app/izhuanbei_test目錄下 rsync -az --delete --exclude=.svn --exclude=.git --exclude=.gitignore --exclude=logs --exclude=cache --exclude=conf --exclude=pdf --exclude=upload --exclude=pc --exclude=wechat /home/xingfupeng/test/ www@192.168.20.229:/app/izhuanbei_test echo "`date +"%Y-%m-%d %H:%M:%S"` SSH remote server 192.168.20.229_Server to modify config file!!!============" >> $LOG

 


免責聲明!

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



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