鉤子(hooks)
Git是在特定事件發生以前或以後執行特定腳本代碼功能(從概念上類比,就與監聽事件、觸發器之類的東西相似)。
Git Hooks就是那些在Git執行特定事件(如commit、push、receive等)後觸發運行的腳本。
gitlab的web hooks跟git hook相似。也是當項目發生提交代碼、提交tag等動做會自動去調用url,這個url能夠是更新代碼。或者其餘操做。
配置目的:php
因爲系統屬於後臺接口系統,開發提交完git倉庫後要實時的部署到測試環境,這時候就須要用到gitlab的web hooks自動更新部署了。
客戶端:要自動更新的測試服務器IP:192.168.1.2git
服務端:Gitlab服務器IP:192.168.1.1web
Gitlab Version: 7.13.0.preshell
GitLab-Shell Version: 2.6.3apache
一、在客戶端上面配置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
二、在服務端gitlab上面爲客戶端添加gitlab新帳號,而後將生成好的公鑰添加到gitlab好的帳號裏面(profile setting-->SSH Keys -->add ssh key)json
#su - webuservim
#ssh-keygen -t rsaapi
進入項目目錄
#cd /path/project服務器
初始化git倉庫
#git clone git@192.168.1.1:test/test_api.git
三、在客戶端上面添加接口文件ssh
#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");
四、訪問接口,測試接口是否成功
http://192.168.1.2:81/?token=d49dfa7622681425fbcbdd687eb2ca59498ce852
五、查看客戶端日誌
#cat /www/gitlab_web/auto_hook.log
=======================================================================
Request on [2015-07-03 14:05:02] from [112.122.112.112]
Data:
=======================================================================
六、在服務端gitlab服務器上面添加web hooks
admin area->projects->test/edit->WEB Hooks->add WEB Hooks
七、提交修改代碼到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
)
注意事項:
一、配置完成後。調用接口的時候沒有自動更新到測試環境。可使用apache的運行用戶測試命令是否能夠執行成功
#su - webuser
#cd /path/project
#git pull
二、若是apache的用戶沒法執行命令或者沒法更新git代碼請檢查一下apache用戶的shell。
參考資料:
http://blog.ycnets.com/2013/10/19/automatic-update-version-with-gitlab-web-hook/#disqus_thread