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


鈎子(hooks)—webhook

http://fighter.blog.51cto.com/1318618/1670667

https://www.lovelucy.info/auto-deploy-website-by-webhooks-of-github-and-gitlab.html

什么是webhook?


wehook

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.

webhook是個在特定情況下觸發的一種api. 越來越多在web上的操作被描述為事件.

 

那個 Payload URL 上填上需要部署到的服務器的網址,比方說 http://dev.lovelucy.info/incoming。然后之后每次有 push 事件 GitHub 都會主動往這個地址發送一個 POST 請求,當然你也可以選擇任何事件都發個 POST 通知你。GitHub 還有個 Secret 的設定,就是一個字符串,如果加上的話就在 POST 請求的 HTTP 頭中會帶一個 Hash 值做驗證密文,證明這個 POST 真是來自 GitHub,不然任何人都往那個地址 POST 忽悠你你都不知道誰是誰對吧……

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.

事件是webhook的核心,當倉庫發生特定action會觸發webhook,

 

gitlab中解釋: Web 鈎子用於在項目發生相關事件時通知外部服務器。

 

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

 

 

1、在客戶端上面配置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>  


 

2、在服務端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  




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

[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");  


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

http://192.168.14.128: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] =>
測試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
)

 

注意事項:

1、配置完成后。調用接口的時候沒有自動更新到測試環境。可以使用apache的運行用戶測試命令是否可以執行成功

#su - webuser

#cd /path/project

#git pull

 

2、如果apache的用戶無法執行命令或者無法更新git代碼請檢查一下apache用戶的shell。

參考資料:

http://blog.ycnets.com/2013/10/19/automatic-update-version-with-gitlab-web-hook/#disqus_thread


免責聲明!

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



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