鉤子(hooks)—webhookphp
http://fighter.blog.51cto.com/1318618/1670667
html
https://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html
node
wehookpython
A webhook is an API concept that's growing(激發) in popularity. As more and more of what we do on the web can be described by events, webhooks are becoming even more applicable. They're incredibly useful and a resource-light way to implement event reactions.react
webhook是個在特定狀況下觸發的一種api. 愈來愈多在web上的操做被描述爲事件.git
那個 Payload URL 上填上須要部署到的服務器的網址,比方說 http://dev.lovelucy.info/incoming。而後以後每次有 push 事件 GitHub 都會主動往這個地址發送一個 POST 請求,固然你也能夠選擇任何事件都發個 POST 通知你。GitHub 還有個 Secret 的設定,就是一個字符串,若是加上的話就在 POST 請求的 HTTP 頭中會帶一個 Hash 值作驗證密文,證實這個 POST 真是來自 GitHub,否則任何人都往那個地址 POST 忽悠你你都不知道誰是誰對吧……
github
Events are at the core of webhooks. These webhooks fire whenever a certain action is taken on the repository, which your server's payload URL intercepts and acts upon.web
事件是webhook的核心,當倉庫發生特定action會觸發webhook,shell
gitlab中解釋: Web 鉤子用於在項目發生相關事件時通知外部服務器。apache
Git是在特定事件發生以前或以後執行特定腳本代碼功能(從概念上類比,就與監聽事件、觸發器之類的東西相似)。
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添加一個接口訪問
二、在服務端gitlab上面爲客戶端添加gitlab新帳號,而後將生成好的公鑰添加到gitlab好的帳號裏面(profile setting-->SSH Keys -->add ssh key)
三、在客戶端上面添加接口文件
四、訪問接口,測試接口是否成功
http://192.168.14.128: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