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

相关文章
相关标签/搜索