多个git post-receive钩子 [英] Multiple git post-receive hooks

查看:241
本文介绍了多个git post-receive钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gitlab. Gitlab正在创建以下链接,以在所有存储库中分发相同的钩子

i'm using Gitlab. Gitlab is creating following link to distribute the same hooks through all repositories

hooks -> /opt/gitlab/embedded/service/gitlab-shell/hooks

在此目录中,已经有一个post-receive钩子可以在Gitlab中正确处理提交,该提交以ruby编写.我想添加一个用bash编写的附加钩子.这可能吗?

Within this directory there's already a post-receive hook to process the commits properly within Gitlab which is written in ruby. I'd like to add an additional hook written in bash. Is this possible?

最诚挚的问候

推荐答案

Gitlab在

Gitlab supports project hooks in a $GIT_DIR/custom_hooks directory.

pre-receivepost-receiveupdate挂钩支持此功能.

This is supported for the pre-receive, post-receive and update hooks.

从以上网页:

通常,git钩子放置在存储库或项目的钩子中 目录. manbetx客户端打不开从每个项目的钩子创建一个符号链接 目录到gitlab-shell hooks目录,以便于维护 gitlab-shell升级之间.因此,实现了自定义钩子 有点不同.一旦钩子出现,行为是完全相同的 创建,虽然.请按照以下步骤设置自定义钩子.

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.

  1. 选择一个需要自定义git钩子的项目.
  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 hook功能符合预期.挂钩可以使用任何语言.确保顶部的"shebang"正确 反映语言类型.例如,如果脚本在Ruby中, shebang可能是#!/usr/bin/env ruby.
  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.

就是这样!假设挂钩代码已正确实现,则挂钩 将视情况触发.

That's it! Assuming the hook code is properly implemented the hook will fire as appropriate.

运行相同类型的多个钩子

现在可以像其他任何git存储库一样完成此操作:编写一个委托脚本以将操作转发到要触发的所有hook实现.例如:

This now can be done like any other git repository: Write a delegation script to forward operation to all the hook implementations which you want to be triggered. For example:

#!/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

这篇关于多个git post-receive钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆