16

I setup a new Gitlab on CentOs on /opt/gitlab-6.9.2-0/apps/gitlab/ and created a new repository under continuous-delivery group. The full path is /opt/gitlab-6.9.2-0/apps/gitlab/gitlab-satellites/continuous-delivery/cd-test . There is only one file under this path which is README.txt.

What I try to achieve is to create a new file when somebody pushes changes to the server. Below are what I have done on the server:

  1. Create post-update and update files under .git/hooks/' each file creates a new file usingecho "text" >> file_name`
  2. chmod them to 775.

When I push changes from my local to the server, there is no file being created. So, I would like to know what I have to do to fix this problem.

Update 1

I added post-receive and post-update to repositories path as VonC suggested

[root@git-cd hooks]# pwd
/opt/gitlab-6.9.2-0/apps/gitlab/repositories/continuous-delivery/cd-test.git/hooks
[root@git-cd hooks]# ll
total 48
-rwxrwxr-x. 1 git git  452 Jun 10 06:01 applypatch-msg.sample
-rwxrwxr-x. 1 git git  896 Jun 10 06:01 commit-msg.sample
-rwxrwxr-x. 1 git git   44 Jun 11 00:37 post-receive
-rwxrwxr-x. 1 git git   41 Jun 11 00:38 post-update
-rwxrwxr-x. 1 git git  189 Jun 10 06:01 post-update.sample
-rwxrwxr-x. 1 git git  398 Jun 10 06:01 pre-applypatch.sample
-rwxrwxr-x. 1 git git 1642 Jun 10 06:01 pre-commit.sample
-rwxrwxr-x. 1 git git 1281 Jun 10 06:01 prepare-commit-msg.sample
-rwxrwxr-x. 1 git git 1352 Jun 10 06:01 pre-push.sample
-rwxrwxr-x. 1 git git 4972 Jun 10 06:01 pre-rebase.sample
lrwxrwxrwx. 1 git git   57 Jun 10 06:01 update -> /opt/gitlab-6.9.2-0/apps/gitlab/gitlab-shell/hooks/update
-rwxrwxr-x. 1 git git 3611 Jun 10 06:01 update.sample

Both file contains a script that adds a new line to an existing file, "post-receive-2" >> /var/log/hooks_test.log. then pushed changes from my local machine to the server. But it still doesn't append the text.

Update 2

Script in post-receive was wrong, it didn't have echo. After I added echo (echo "post-receive-2" >> /var/log/hooks_test.log then it works as expected!

Community
  • 1
  • 1
Anonymous
  • 8,708
  • 20
  • 80
  • 127

3 Answers3

12

That would be because those satellite repos aren't the one you would push to, so their hook aren't trigger when you would think (ie, not when someone is pushing to the GitLab server).

PR 6185 introduced the archicture overview documentation

/home/git/gitlab-satellites - checked out repositories for merge requests and file editing from web UI. This can be treated as a temporary files directory.

The satellite repository is used by the web interface for editing repositories and the wiki which is also a git repository.

You should add your hook in the bare repos ~git/repositories.

Or (update Q4 2014, from GitLab 7.5+ Nov 2014), you can use custom hooks (instead of webhooks), as mentioned below by Doka.

Custom git hooks must be configured on the filesystem of the GitLab server.
Only GitLab server administrators will be able to complete these tasks. Please explore webhooks as an option if you do not have filesystem access.

  • On the GitLab server, navigate to the project's repository directory.
    For a manual install the path is usually /home/git/repositories/<group>/<project>.git.
    For Omnibus installs the path is usually /var/opt/gitlab/git-data/repositories/<group>/<project>.git.
  • Create a new directory in this location called custom_hooks.
  • Inside the new custom_hooks directory, create a file with a name matching the hook type.
    For a pre-receive hook the file name should be pre-receive with no extension.
  • Make the hook file executable and make sure it's owned by git.
Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • I just tried adding files to bare-repository but it still doesn't work. I added more info on the question. – Anonymous Jun 11 '14 at 07:02
  • @Anonymous you don't add files to a bare repo: it is a *bare* repo (.git only, no files at all). see http://stackoverflow.com/a/24115534/6309. Your hook should create files in a separate path. But that hook must be declared in the bare repo in order to be triggered by a push. – VonC Jun 11 '14 at 07:06
  • Oops, I found the problem. The command I wrote on post-receive was wrong. – Anonymous Jun 11 '14 at 07:07
2

As of gitlab-shell version 2.2.0 (which requires GitLab 7.5+), GitLab administrators can add custom git hooks to any GitLab project. So you have to upgrade, and follow the instructions from here: https://docs.gitlab.com/ce/administration/custom_hooks.html

Cavva79
  • 331
  • 3
  • 15
Doka
  • 205
  • 1
  • 7
  • Interesting. I had forgotten about that previous answer that could benefit from that new feature. I have edited it accordingly for more visibility. +1 – VonC Dec 29 '14 at 16:11
2

See Custom Git Hooks - GitLab Documentation for the official way to do it.

Summary: As of gitlab-shell version 2.2.0 (which requires GitLab 7.5+), GitLab administrators can add custom git hooks to any GitLab project.

  1. Pick a project that needs a custom git hook.
  2. On the GitLab server, navigate to the project's repository directory. For an installation from source the path is usually /home/git/repositories/<group>/<project>.git For Omnibus installs the path is usually /var/opt/gitlab/git-data/repositories/<group>/<project>.git
  3. Create a new directory in this location called custom_hooks.
  4. Inside the new custom_hooks directory, create a file with a name matching the hook type. For a pre-receive hook the file name should be pre-receive with no extension.
  5. Make the hook file executable and make sure it's owned by git.
  6. Write the code to make the git hook function as expected. Hooks can be in any language. Ensure the 'shebang' at the top properly reflects the language type. For example, if the script is in Ruby the shebang will probably be #!/usr/bin/env ruby.

But do see the link above for the official docs. The above is a snapshot as it was today...

Cavva79
  • 331
  • 3
  • 15
Peter V. Mørch
  • 9,433
  • 5
  • 46
  • 65