鉤子(hooks)—webhook-使用鉤子自動觸發部署

鉤子(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

什麼是webhook?


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

what is events?

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添加一個接口訪問

[python] view plain copy
print ?
  1. #vim /usr/local/apache/conf/httpd.conf  
  2. listen 81  
  3. <VirtualHost *:81>  
  4.           ServerAdmin localhost  
  5.           DocumentRoot "/www/gitlab_web"  
  6.        <Directory "/www/gitlab_web">  
  7.             Options -Indexes +FollowSymLinks  
  8.             AllowOverride None  
  9.             Order allow,deny  
  10.             Allow from all  
  11.           </Directory>  
  12.        RewriteEngine on  
  13. </VirtualHost>  


 

二、在服務端gitlab上面爲客戶端添加gitlab新帳號,而後將生成好的公鑰添加到gitlab好的帳號裏面(profile setting-->SSH  Keys -->add ssh key)

[python] view plain copy
print ?
  1. #su - webuser  
  2. #ssh-keygen -t rsa  
  3. 進入項目目錄  
  4. #cd /path/project  
  5. 初始化git倉庫   
  6. #git clone git@192.168.1.1:test/test_api.git  




三、在客戶端上面添加接口文件

[python] view plain copy
print ?
  1. [root@node1 gitlab_web]# pwd  
  2. /www/gitlab_web  
  3. [root@node1 gitlab_web]# cat index.php   
  4. <?php  
  5. //做爲接口傳輸的時候認證的密鑰  
  6. $valid_token = 'd49dfa762268687eb2ca59498ce852';  
  7. //調用接口被容許的ip地址  
  8. $valid_ip = array('192.168.14.2','192.168.14.1','192.168.14.128');  
  9. $client_token = $_GET['token'];  
  10. $client_ip = $_SERVER['REMOTE_ADDR'];  
  11. $fs = fopen('./auto_hook.log''a');  
  12. fwrite($fs, 'Request on ['.date("Y-m-d H:i:s").'] from ['.$client_ip.']'.PHP_EOL);  
  13. if ($client_token !== $valid_token)  
  14. {  
  15.     echo "error 10001";  
  16.     fwrite($fs, "Invalid token [{$client_token}]".PHP_EOL);  
  17.     exit(0);  
  18. }  
  19. if ( ! in_array($client_ip, $valid_ip))  
  20. {  
  21.     echo "error 10002";  
  22.     fwrite($fs, "Invalid ip [{$client_ip}]".PHP_EOL);  
  23.     exit(0);  
  24. }  
  25. $json = file_get_contents('php://input');  
  26. $data = json_decode($json, true);  
  27. fwrite($fs, 'Data: '.print_r($data, true).PHP_EOL);  
  28. fwrite($fs, '======================================================================='.PHP_EOL);  
  29. $fs and fclose($fs);  
  30. //這裏也能夠執行自定義的腳本文件update.sh,腳本內容能夠本身定義。  
  31. //exec("/bin/sh /root/updategit.sh");  
  32. exec("/bin/echo $valid_ip >>/tmp/webhook.txt");  


四、訪問接口,測試接口是否成功

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] =>
測試gitlabweb 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

相關文章
相關標籤/搜索