【问题标题】:Multiple git post-receive hooks多个 git post-receive 钩子
【发布时间】:2015-11-13 10:36:37
【问题描述】:
我正在使用 Gitlab。 Gitlab 正在创建以下链接以通过所有存储库分发相同的钩子
hooks -> /opt/gitlab/embedded/service/gitlab-shell/hooks
在这个目录中,已经有一个post-receive 挂钩,可以在用 ruby 编写的 Gitlab 中正确处理提交。我想添加一个用 bash 编写的附加钩子。这可能吗?
最好的问候
【问题讨论】:
标签:
git
gitlab
git-post-receive
【解决方案1】:
Gitlab 支持 $GIT_DIR/custom_hooks directory 中的项目挂钩。
pre-receive、post-receive 和 update 挂钩支持此功能。
来自上述网页:
通常,git hooks 放在仓库或者项目的 hooks 中
目录。 GitLab 从每个项目的钩子中创建一个符号链接
目录到 gitlab-shell hooks 目录以便于维护
在 gitlab-shell 升级之间。因此,自定义挂钩被实现为
有点不同。一旦钩子出现,行为是完全相同的
创造了,虽然。按照以下步骤设置自定义挂钩。
- 选择一个需要自定义 git 挂钩的项目。
- 在 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 挂钩按预期运行。 Hooks 可以是任何语言。确保顶部的“shebang”正确
反映语言类型。例如,如果脚本是 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