多个 git post-接收挂钩
Multiple git post-receive hooks
我正在使用 Gitlab。 Gitlab 正在创建以下 link 以通过所有存储库分发相同的挂钩
hooks -> /opt/gitlab/embedded/service/gitlab-shell/hooks
在这个目录中已经有一个 post-receive
钩子来在 Gitlab 中正确处理提交,它是用 ruby 编写的。我想添加一个用 bash 编写的附加钩子。这可能吗?
此致
Gitlab 支持 $GIT_DIR/custom_hooks
directory.
中的项目挂钩
支持 pre-receive
、post-receive
和 update
挂钩。
来自以上网页:
Normally, git hooks are placed in the repository or project's hooks
directory. GitLab creates a symlink from each project's hooks
directory to the gitlab-shell hooks directory for ease of maintenance
between gitlab-shell upgrades. As such, custom hooks are implemented a
little differently. Behavior is exactly the same once the hook is
created, though. Follow these steps to set up a custom hook.
- Pick a project that needs a custom git hook.
- 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
.
- 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.
- 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
.
That's it! Assuming the hook code is properly implemented the hook
will fire as appropriate.
运行多个相同类型的挂钩
现在可以像任何其他 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 [=10=])
hook_name=$(basename [=10=])
hook_dir="$script_dir/$hook_name.d"
if [[ -d $hook_dir ]]; then
pee $hook_dir/* $*
fi
exit 0
我正在使用 Gitlab。 Gitlab 正在创建以下 link 以通过所有存储库分发相同的挂钩
hooks -> /opt/gitlab/embedded/service/gitlab-shell/hooks
在这个目录中已经有一个 post-receive
钩子来在 Gitlab 中正确处理提交,它是用 ruby 编写的。我想添加一个用 bash 编写的附加钩子。这可能吗?
此致
Gitlab 支持 $GIT_DIR/custom_hooks
directory.
支持 pre-receive
、post-receive
和 update
挂钩。
来自以上网页:
Normally, git hooks are placed in the repository or project's hooks directory. GitLab creates a symlink from each project's hooks directory to the gitlab-shell hooks directory for ease of maintenance between gitlab-shell upgrades. As such, custom hooks are implemented a little differently. Behavior is exactly the same once the hook is created, though. Follow these steps to set up a custom hook.
- Pick a project that needs a custom git hook.
- 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
.- 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.
- 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
.That's it! Assuming the hook code is properly implemented the hook will fire as appropriate.
运行多个相同类型的挂钩
现在可以像任何其他 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 [=10=])
hook_name=$(basename [=10=])
hook_dir="$script_dir/$hook_name.d"
if [[ -d $hook_dir ]]; then
pee $hook_dir/* $*
fi
exit 0