How can I add hooks to gitlab?

2019-01-22 21:47发布

问题:

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!

回答1:

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.


回答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



回答3:

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...