鈎子(hooks)
hooks是在特定事件發生之前或之后執行特定腳本代碼功能(從概念上類比,就與監聽事件、觸發器之類的東西類似)。
Git hooks就是那些在Git執行特定事件(如commit、push、receive等)后觸發運行的腳本。
gitlab的web hooks跟git hook類型。也是當項目發生提交代碼、提交tag等動作會自動去調用url,這個url可以是更新代碼,或者其他操作。
配置目的
由於系統屬於后台接口系統,開發提交完git倉庫后要實時的部署到測試環境,這時候就需要用到gitlab的web hooks自動更新部署了。
- 客戶端:要自動更新的測試服務器IP:192.168.1.2
- 服務端:gitlab服務器IP:192.168.1.1
- Gitlab Version 7.13.0.pre
- Gitlab-Shell Version:2.6.3
步驟如下:
- 在客戶端上配置Apache配置文件,為web hooks添加一個訪問接口
#vim /usr/local/apache/conf/httpd.conf listen 81 <VirtualHost *:81> ServerAdmin localhost DocumentRoot "/www/gitlab_web" <Directory "/www/gitlab_web"> Options -Indexes +FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> RewriteEngine on </VirtualHost>
2.在服務端gitlab上面為客戶端添加gitlab新賬號,然后將生產的公鑰添加到gitl的賬號里面
(profile setting→SSH Keys →add ssh key) #su - webuser #ssh-keygen -t rsa 進入項目目錄 #cd /path/project 初始化git倉庫 #git clone git@192.168.1.1:test/test_api.git
3.在客戶端上面添加接口文件
#vim /www/gitlab_web/index.php <?php //作為接口傳輸的時候認證的密鑰 $valid_token = 'd49dfa762268687eb2ca59498ce852'; //調用接口被允許的ip地址 $valid_ip = array('192.168.1.1','10.17.10.175','112.112.112.112'); $client_token = $_GET['token']; $client_ip = $_SERVER['REMOTE_ADDR']; $fs = fopen('./auto_hook.log', 'a'); fwrite($fs, 'Request on ['.date("Y-m-d H:i:s").'] from ['.$client_ip.']'.PHP_EOL); if ($client_token !== $valid_token) { echo "error 10001"; fwrite($fs, "Invalid token [{$client_token}]".PHP_EOL); exit(0); } if ( ! in_array($client_ip, $valid_ip)) { echo "error 10002"; fwrite($fs, "Invalid ip [{$client_ip}]".PHP_EOL); exit(0); } $json = file_get_contents('php://input'); $data = json_decode($json, true); fwrite($fs, 'Data: '.print_r($data, true).PHP_EOL); fwrite($fs, '======================================================================='.PHP_EOL); $fs and fclose($fs); //這里也可以執行自定義的腳本文件update.sh,腳本內容可以自己定義。 //exec("/bin/sh /root/updategit.sh"); exec("cd /path/project;/usr/bin/git pull");
4.訪問接口,測試接口是否成功。
http://192.168.1.2:81/?token=d49dfa7622681425fbcbdd687eb2ca59498ce852
5.查看客戶端日志
#cat /www/gitlab_web/auto_hook.log ======================================================================= Request on [2015-07-03 14:05:02] from [112.122.112.112] Data: =======================================================================
6.在服務端gitlab服務器上面添加web hooks
admin area->projects->test/edit->WEB Hooks->add WEB Hooks
7.提交修改代碼到gitlab倉庫,然后查看日志、查看測試環境是否更新
#cat /www/gitlab_web/auto_hook.log Request on [2015-07-03 14:13:37] from [12.123.12.3] Data: Array ( [object_kind] => push [before] => e5988b5dce7a038 [after] => d8ce92ac4ab4ba046dd [ref] => refs/heads/master [checkout_sha] => d8ceefd5c4ab4ba046dd [message] => [user_id] => 7 [user_name] => test [user_email] => test@qq.com [project_id] => 3 [repository] => Array ( [name] => test_api [url] => git@192.168.1.1:test/test.api [description] => test.com product code [homepage] => http://xx./test_api [git_http_url] => http://xx./test_api [git_ssh_url] => git@112.23.23.1:test.git [visibility_level] => 10 ) [commits] => Array ( [0] => Array ( [id] => d8cec4ab4ba046dd [message] => 測試gitlab的web hook接口。 [timestamp] => 2015-07-03T14:13:51+08:00 [url] => http://xxxx/test_api/commit/d8ce95c4ab4ba046dd [author] => Array ( [name] => test [email] => test@qq.com ) ) ) [total_commits_count] => 1 )
PS:注意事項:
1:配置完成后。調用接口的時候沒有自動更新到測試環境。可以使用apache的運行用戶測試命令是否可以執行成功
#su - webuser #cd /path/project #git pull
2:如果Apache的用戶無法執行命令或者更新git代碼,請檢查一下Apache的用戶的shell以及PHP是否支持exec調用shell命令。
原文地址:https://mp.weixin.qq.com/s?__biz=MzIxNDE4NDg0Ng==&mid=405872601&idx=2&sn=1b7392ef541e044e8e60bd0695cf430a&scene=24&srcid=0407Gq48cxRCWvJmt1qV5Klb#rd