天天看点

自动化发布-GitLab WEB Hooks 配置

钩子(hooks)

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添加一个接口访问

#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

2、在服务端gitlab上面为客户端添加gitlab新账号,然后将生成好的公钥添加到gitlab好的账号里面(profile setting-->SSH  Keys -->add ssh key)

#su - webuser

#ssh-keygen -t rsa

进入项目目录

#cd /path/project

初始化git仓库 

#git clone [email protected]:test/test_api.git

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

#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);

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

4、访问接口,测试接口是否成功

http://192.168.1.2: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

<a href="http://s3.51cto.com/wyfs02/M00/6F/47/wKiom1WWX6qhXEhdAAM3gTdDuvU940.jpg" target="_blank"></a>

7、提交修改代码到gitlab仓库,然后查看日志、查看测试环境是否更新

Request on [2015-07-03 14:13:37] from [12.123.12.3]

Data: Array

(

    [object_kind] =&gt; push

    [before] =&gt; e5988b5dce7a038

    [after] =&gt; d8ce92ac4ab4ba046dd

    [ref] =&gt; refs/heads/master

    [checkout_sha] =&gt; d8ceefd5c4ab4ba046dd

    [message] =&gt; 

    [user_id] =&gt; 7

    [user_name] =&gt; test

    [user_email] =&gt; [email protected]

    [project_id] =&gt; 3

    [repository] =&gt; Array

        (

            [name] =&gt; test_api

            [url] =&gt; [email protected]:test/test.api

            [description] =&gt; test.com product code

            [homepage] =&gt; http://xx./test_api

            [git_http_url] =&gt; http://xx./test_api 

            [git_ssh_url] =&gt; [email protected]:test.git

            [visibility_level] =&gt; 10

        )

    [commits] =&gt; Array

            [0] =&gt; Array

                (

                    [id] =&gt; d8cec4ab4ba046dd

                    [message] =&gt; 测试gitlab的web hook接口。

                    [timestamp] =&gt; 2015-07-03T14:13:51+08:00

                    [url] =&gt; http://xxxx/test_api/commit/d8ce95c4ab4ba046dd

                    [author] =&gt; Array

                        (

                            [name] =&gt; test

                            [email] =&gt; [email protected]

                        )

                )

    [total_commits_count] =&gt; 1

)

注意事项:

1、配置完成后。调用接口的时候没有自动更新到测试环境。可以使用apache的运行用户测试命令是否可以执行成功

#git pull

2、如果apache的用户无法执行命令或者无法更新git代码请检查一下apache用户的shell。

参考资料:

<a href="http://blog.ycnets.com/2013/10/19/automatic-update-version-with-gitlab-web-hook/#disqus_thread" target="_blank">http://blog.ycnets.com/2013/10/19/automatic-update-version-with-gitlab-web-hook/#disqus_thread</a>

本文转自 张玉坡 51CTO博客,原文链接:http://blog.51cto.com/fighter/1670667