git 如何向 gitlab 添加钩子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24154384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:11:13  来源:igfitidea点击:

How can I add hooks to gitlab?

gitcontinuous-integrationgitlab

提问by Anonymous

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.

我在 CentOs 上设置了一个新的 Gitlab,/opt/gitlab-6.9.2-0/apps/gitlab/并在持续交付组下创建了一个新的存储库。完整路径是/opt/gitlab-6.9.2-0/apps/gitlab/gitlab-satellites/continuous-delivery/cd-test . 此路径下只有一个文件 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-updateand updatefiles under .git/hooks/' each file creates a new file usingecho "text" >> file_name`
  2. chmod them to 775.
  1. 在echo "text" >> file_name`下创建post-updateupdate文件.git/hooks/' each file creates a new file using
  2. 将它们修改为 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

更新 1

I added post-receiveand post-updateto repositoriespath as VonC suggested

我按照VonC 的建议添加了post-receivepost-updaterepositories路径

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

这两个文件都包含一个脚本,该脚本将新行添加到现有文件"post-receive-2" >> /var/log/hooks_test.log. 然后将更改从我的本地机器推送到服务器。但它仍然没有附加文本。

Update 2

更新 2

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

post-receive 中的脚本是错误的,它没有回声。在我添加 echo (echo "post-receive-2" >> /var/log/hooks_test.log然后它按预期工作!

采纳答案by VonC

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

那是因为那些卫星存储库不是您要推送到的那个,所以它们的钩子不会在您想到时触发(即,当有人推送到 GitLab 服务器时不会触发)。

PR 6185introduced the archicture overview documentation

PR 6185介绍了架构概览文档

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

/home/git/gitlab-satellites- 从 Web UI 签出合并请求和文件编辑的存储库。这可以被视为一个临时文件目录。

卫星存储库由 Web 界面用于编辑存储库和 wiki,它也是一个 git 存储库。

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

您应该在裸 repos 中添加您的钩子~git/repositories

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

或者(更新 Q4 2014,来自GitLab 7.5+ Nov 2014),您可以使用自定义钩子(而不是webhooks),如下面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_hooksdirectory, create a file with a name matching the hook type.
    For a pre-receivehook the file name should be pre-receivewith no extension.
  • Make the hook file executable and make sure it's owned by git.

必须在 GitLab 服务器的文件系统上配置自定义 git 钩子。
只有 GitLab 服务器管理员才能完成这些任务。如果您没有文件系统访问权限,请探索 webhooks 作为一个选项。

  • 在 GitLab 服务器上,导航到项目的存储库目录。
    对于手动安装,路径通常是/home/git/repositories/<group>/<project>.git.
    对于 Omnibus 安装,路径通常是/var/opt/gitlab/git-data/repositories/<group>/<project>.git.
  • 在此位置创建一个名为custom_hooks.
  • 在新custom_hooks目录中,创建一个名称与挂钩类型匹配的文件。
    对于pre-receive钩子,文件名应该pre-receive没有扩展名。
  • 使钩子文件可执行并确保它由git.

回答by Doka

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

从 gitlab-shell 版本 2.2.0(需要 GitLab 7.5+)开始,GitLab 管理员可以向任何 GitLab 项目添加自定义 git hook。所以你必须升级,并按照这里的说明进行操作:https: //docs.gitlab.com/ce/administration/custom_hooks.html

回答by Peter V. M?rch

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

有关执行此操作的官方方法,请参阅自定义 Git Hooks - GitLab 文档

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.

总结:从 gitlab-shell 版本 2.2.0(需要 GitLab 7.5+)开始,GitLab 管理员可以向任何 GitLab 项目添加自定义 git hook。

  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>.gitFor 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_hooksdirectory, create a file with a name matching the hook type. For a pre-receive hook the file name should be pre-receivewith 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.
  1. 选择一个需要自定义 git hook 的项目。
  2. 在 GitLab 服务器上,导航到项目的存储库目录。对于从源代码安装,路径通常/home/git/repositories/<group>/<project>.git为 对于 Omnibus 安装,路径通常为/var/opt/gitlab/git-data/repositories/<group>/<project>.git
  3. 在此位置创建一个名为custom_hooks.
  4. 在新custom_hooks目录中,创建一个名称与挂钩类型匹配的文件。对于预接收挂钩,文件名应该pre-receive没有扩展名。
  5. 使钩子文件可执行并确保它归 git 所有。
  6. 编写代码使git钩子功能如预期。钩子可以是任何语言。确保顶部的“shebang”正确反映语言类型。例如,如果脚本在 Ruby 中,shebang 可能是#!/usr/bin/env ruby.

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

但是请参阅上面的官方文档链接。上面是今天的快照......