Automated publishing-gitlab WEB Hooks configuration

Source: Internet
Author: User
Tags git hooks

Hooks (hooks)
Git is the ability to execute specific scripting code before or after a particular event (conceptually analogous to something like listening to events, triggers, and so on).
Git hooks is a script that triggers a run when git executes a specific event, such as commit, push, receive, and so on.
Gitlab's web hooks is similar to git hooks. It is also when the project commit code, submit tag, etc. will automatically call the URL, this URL can be updated code. or other operations.

Purpose of configuration:

Because the system belongs to the background interface system, the development of the GIT warehouse after the implementation of real-time deployment to the test environment, it is necessary to use the Gitlab Web hooks Automatic Update deployment.

Client: The test server to be updated automatically ip:192.168.1.2

Service side: Gitlab Server ip:192.168.1.1

Gitlab Version:7.13.0.pre

Gitlab-shell version:2.6.3



1, configure the Apache configuration file on the client, add an interface access for the 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, on the server side Gitlab add gitlab new account for the client, and then add the generated good public key to Gitlab good account (profile Setting-->ssh Keys-->add SSH key)

#su-webuser

#ssh-keygen-t RSA

Go to project directory
#cd/path/project

Initializing the Git repository
#git Clone [Email protected]:test/test_api.git


3. Add the interface file on the client

#vim/www/gitlab_web/index.php

<?php
The authenticated key as the interface transfer
$valid _token = ' d49dfa762268687eb2ca59498ce852 ';
The IP address that the calling interface is allowed to
$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);
Exit (0);
}
$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);
It is also possible to execute a custom script file update.sh, where the script content can be defined by itself.
EXEC ("/bin/sh/root/updategit.sh");
EXEC ("cd/path/project;/usr/bin/git pull");



4, Access interface, test the interface is successful

http://192.168.1.2:81/?token=d49dfa7622681425fbcbdd687eb2ca59498ce852


5. View Client Logs

#cat/www/gitlab_web/auto_hook.log

=======================================================================
Request on [2015-07-03 14:05:02] from [112.122.112.112]
Data:
=======================================================================


6. Add Web hooks on server Gitlab servers

Admin Area->projects->test/edit->web hooks->add WEB Hooks

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/47/wKiom1WWX6qhXEhdAAM3gTdDuvU940.jpg "title=" 1.png " alt= "Wkiom1wwx6qhxehdaam3gtdduvu940.jpg"/>


7, submit the change code to the Gitlab warehouse, and then view the log, check whether the test environment is updated

#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] = [email protected]
[PROJECT_ID] = 3
[Repository] = = Array
(
[Name] = Test_api
[url] = [email protected]:test/test.api
[Description] = test.com Product Code
[Homepage] = Http://xx./test_api
[Git_http_url] = Http://xx./test_api
[Git_ssh_url] = [email protected]:test.git
[Visibility_level] = 10
)

[commits] = = Array
(
[0] = = Array
(
[id] = D8CEC4AB4BA046DD
[Message] = Test Gitlab's web hook interface.

[Timestamp] = 2015-07-03t14:13:51+08:00
[url] = HTTP://XXXX/TEST_API/COMMIT/D8CE95C4AB4BA046DD
[author] = = Array
(
[name] = = Test
[email] = [email protected]
)

)

)

[Total_commits_count] = 1
)


Precautions:

1, after the configuration is complete. The interface is not automatically updated to the test environment when it is called. You can use Apache to run a user test to see if the command can execute successfully

#su-webuser

#cd/path/project

#git Pull


2. If the Apache user cannot execute the command or cannot update the GIT code, check the Apache user's shell.



Resources:

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


This article is from the "Zhang Yupo" blog, make sure to keep this source http://fighter.blog.51cto.com/1318618/1670667

Automated publishing-gitlab WEB Hooks configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.