Custom post-receive hook with gitlab

2.6k views Asked by At

I am trying to set up a custom post-receive hook using gitlab

I am following their documentation here

I have an executable post-receive file in the custom_hooks folder with the following code:

 !/bin/bash
 echo "post-receive firing | wall
 git pull

If I run this file with:

 ./post-receive

It works fine. It does not appear to fire at all when a push is made to the gitlab repo.

This is the first time I have ever tried web hooks of any kind so I am a little lost.

a) I have the above script on my remote webserver, inside the .git/custom_hooks folder, is this correct?

b) Do I need to do anything in the repo?

c) In the documentation, it specifies I should set permissions on the file so it is owned by the 'git' user. I have no git user on the webserver... all other git related files are owned by my user or root.

So assuming I am doing something completely wrong here, can someone please put me out of my misery and suggest a sane way to auto push to a web server whenever I push to gitlab from a local machine. I feel like I have spent some time looking for a way to do this and solutions seem to be complete overkill (cronjob running every few seconds...) or like the above, do not seem to work. Or at least I seem incapable of making them work.

Surely this is a straightforward task...

2

There are 2 answers

2
VonC On BEST ANSWER

on the file so it is owned by the 'git' user. I have no git user on the webserver... all other git related files are owned by my user or root.

That is because oyur webserver is a client of the GitLab server.
The documentation you reference is for configuring a GitLab server (adding a post-receive hook to a GitLab repo). Hence the 'git' user, which is the default one on a GitLab server.

But if you are pushing to gitlab.com, you wouldn't add directly a hook.

You would need to declare a web hook, which would then send back a JSON message that you can listen to in order to trigger another action.

If you are the only one to push, you could simply push to multiple repos. No hooks involved there, client or server.
But that would be a local setting valid only for your local cloned repo.

0
sachin On

Please follow following steps

  1. Go to your git sever
  2. Find hooks using following command find / -xdev 2>/dev/null -name "hooks"
  3. Select and go to your respective project hook folder.
  4. open post-receive file in edit mode (http://www.wikihow.com/Create-and-Edit-Text-File-in-Linux-by-Using-Terminal)
  5. put your logic here. if want to execute url u can do it in following way

require 'net/http' require 'uri' uri = URI.parse("YOUR_URL") response = Net::HTTP.get_response(uri)

Hope, this will help you