我正在使用Gitlab。 Gitlab正在创建以下链接,以通过所有存储库分发相同的钩子
hooks -> /opt/gitlab/embedded/service/gitlab-shell/hooks
在这个目录中,已经有一个post-receive
钩子来在Gitlab中正确处理提交,这是用ruby编写的。我想添加一个用bash编写的附加钩子。这可能吗?
祝你好运
答案 0 :(得分:3)
Gitlab支持$GIT_DIR/custom_hooks
directory中的项目挂钩。
这支持pre-receive
,post-receive
和update
个钩子。
从上面的网页:
通常,git钩子放在存储库或项目的钩子中 目录。 GitLab从每个项目的钩子创建一个符号链接 目录到gitlab-shell hooks目录以便于维护 gitlab-shell升级之间。因此,自定义钩子被实现了 一点点不同。一旦挂钩,行为就完全一样了 虽然创造了。请按照以下步骤设置自定义挂钩。
- 选择需要自定义git hook的项目。
- 在GitLab服务器上,导航到项目的存储库目录。对于从源安装,通常是路径
/home/git/repositories/<group>/<project>.git
。对于Omnibus安装 路径通常是/var/opt/gitlab/git-data/repositories/<group>/<project>.git
。- 在此位置创建一个名为custom_hooks的新目录。
- 在新的custom_hooks目录中,创建一个名称与钩子类型匹配的文件。对于预接收挂钩,文件名应该是
pre-receive
没有扩展名。- 使钩子文件可执行,并确保它由git拥有。
- 编写代码以使git钩子函数按预期方式运行。钩子可以是任何语言。确保&#39; shebang&#39;在顶部正确 反映了语言类型。例如,如果脚本在Ruby中 shebang可能会
醇>#!/usr/bin/env ruby
。那就是它!假设钩子代码正确实现钩子 会酌情开火。
运行相同类型的多个挂钩
现在可以像任何其他git存储库一样完成:编写委托脚本以将操作转发到您想要触发的所有钩子实现。例如:
#!/bin/bash
# Allow to run multiple hooks
# For each hook type (post-receive, post-update, ...) create a type.d subdirectory, e.g. post-receive.d/
# Then put all hook scripts into that directory and make them executable.
# Requires the "pee" utility from moreutils package: http://joeyh.name/code/moreutils/ or use "apt install moreutils"
# pee duplicates stdinput to all scripts
script_dir=$(dirname $0)
hook_name=$(basename $0)
hook_dir="$script_dir/$hook_name.d"
if [[ -d $hook_dir ]]; then
pee $hook_dir/* $*
fi
exit 0