19

I am using Gitlab on one server and would like to push my git repository on commit of the master branch to another webserver. So when I push a new version of the website the production server gets updated. I know this should be possible with hooks inside gitlab but I am unable to find how exactly. Tried the following guide http://danielmiessler.com/study/git/#website but it is not written for use with gitlab so Im missing parts.

What do I need to do on the production webserver and what do I set the hook URL to then?

tftd
  • 1,480
  • 7
  • 24
  • 38
tvb
  • 341
  • 1
  • 2
  • 8
  • you can use the regular git hooks, if you have root access to the git server, see post-commit hook here: http://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks – Doka Nov 09 '14 at 13:27
  • You can try [Custom Git hooks](https://github.com/gitlabhq/gitlabhq/blob/667c0a909bde1cf71f21d8ec9768e98b1c489030/doc/hooks/custom_hooks.md). – monsta Dec 24 '14 at 10:58

3 Answers3

9

gitlab already uses the post-receive hook internally. you could fiddle around with that script and call your hook as well, but from the docs it looks like the "official" way would be to use "web-hooks", i.e. let gitlab call your webserver on post-receive and your webserver then pulls the repository. I haven't tried this myself, but since no one answered so far I thought I'd point you in that direction:

to enable web-hooks go into your project's main page and select hooks from the top right, below the main menu. ( http://yourgitlab.example.net/yourproject/hooks ). there is an example&docs linked from that page ( http://yourgitlab.example.net/help/web_hooks ).

edit://

I tried it this morning. Here is a php script example. It assumes the you have already cloned the repo and the webserver has all the necessary permissions/ssh keys set up.

<?php
$mirrordir='/srv/http/gitlabhooktest/gitmirror';
$gitdir=$mirrordir."/.git";

$json= file_get_contents('php://input');
#error_log($json);
$jsarr=json_decode($json,true);
#error_log(print_r($jsarr,true));
$branch=$jsarr["ref"];
if($branch=='refs/heads/master'){
 $cmd="git --work-tree=$mirrordir --git-dir=$gitdir pull";
 #error_log($cmd);
 exec($cmd);
} 
Gryphius
  • 2,710
  • 1
  • 18
  • 19
  • yes I know where to find the hooks page. I just don't know how the file should look like gitlab should post to. – tvb Jan 06 '13 at 21:51
  • 1
    see edited answer, I added an example script. may not be perfect but at least a quick test seems to work – Gryphius Jan 07 '13 at 08:04
5

Custom hooks were recently added (since as Gryphius said the regular hooks are used internally): https://github.com/gitlabhq/gitlabhq/blob/667c0a909bde1cf71f21d8ec9768e98b1c489030/doc/hooks/custom_hooks.md

You just create a custom_hooks directory in you bare Git repo, and put the hooks in it, and then GitLab makes sure they get run.

4

Gitlab doesn't have a post-receive hook since the developers replaced gitolite with gitlab-shell.

Therefore you can:

sudo -u git bash
touch /home/git/repositories/<repository name>.git/hooks/post-receive
vim /home/git/repositories/<repository name>.git/hooks/post-receive

Make sure the git user has all the permissions needed to run the commands in this file

Nicolay77
  • 151
  • 2