简体   繁体   English

如何在从 13.1 开始的服务器端 Gitlab 全局挂钩中获取组和存储库名称

[英]How to get group and repository name in server-side Gitlab global hooks starting with 13.1

My organization uses Gitlab CE and server-side git hooks.我的组织使用 Gitlab CE 和服务器端 git 挂钩。 Specifically we use global pre-recieve hooks to validate commit messages (the one placed in /opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.d ).具体来说,我们使用全局预接收挂钩来验证提交消息(放置在/opt/gitlab/embedded/service/gitlab-shell/hooks/pre-receive.d中的那个)。 Validation rules are different for different Gitlab groups, so the script parses the full path to the repository and based on the group applies different rules.不同的 Gitlab 组的验证规则不同,因此脚本会解析存储库的完整路径,并根据组应用不同的规则。

We used to get the full path to the repository, including group, using PWD environmental variable in the past.我们过去使用PWD环境变量来获取存储库的完整路径,包括组。 But it looks like starting with Gitlab 13.1 this variable is no longer available.但看起来从 Gitlab 13.1开始,这个变量不再可用。 Also since new repositories are created in @hashed folder this approach anyway needs to be adjusted.此外,由于在 @hashed 文件夹中创建了新的存储库,因此无论如何都需要调整这种方法。

We've tried to use git config --get gitlab.fullpath in the pre-recieve hook and so far it works fine for all repositories except wiki repositories (like <project_name>.wiki.git ).我们尝试在 pre-recieve 挂钩中使用git config --get gitlab.fullpath ,到目前为止它适用于除 wiki 存储库之外的所有存储库(如<project_name>.wiki.git Also it's not a documented approach, so it might break again in the future.此外,这不是一个记录在案的方法,因此将来可能会再次中断。 Is there any official way of getting the group name in pre-recieve hook?是否有任何官方方法可以在预接收挂钩中获取组名?

UPD: it looks like there is a workaround using gitlab-rails, but it's far from optimal: UPD:看起来有一个使用 gitlab-rails 的解决方法,但它远非最佳:

  1. GIT_OBJECT_DIRECTORY environment variable contains the path like /var/opt/gitlab/git-data/repositories/@hashed/dd/8e/dd8e8c8c9dae8978f122d7bcf3d0d49f6a0e86b9fc35528f55e78f7408927bb1.git/objects/incoming-16WBe9 which is a directory inside the repo directory GIT_OBJECT_DIRECTORY环境变量包含类似/var/opt/gitlab/git-data/repositories/@hashed/dd/8e/dd8e8c8c9dae8978f122d7bcf3d0d49f6a0e86b9fc35528f55e78f7408927bb1.git/objects/incoming-16WBe9目录的路径
  2. Extract the repo hash from the path above and use the following command to lookup the full path: gitlab-rails runner "puts ProjectRepository.find_by(disk_path: '@hashed/dd/8e/dd8e8c8c9dae8978f122d7bcf3d0d49f6a0e86b9fc35528f55e78f7408927bb1').project.full_path" Extract the repo hash from the path above and use the following command to lookup the full path: gitlab-rails runner "puts ProjectRepository.find_by(disk_path: '@hashed/dd/8e/dd8e8c8c9dae8978f122d7bcf3d0d49f6a0e86b9fc35528f55e78f7408927bb1').project.full_path"

But the issue with this approach is that GIT_OBJECT_DIRECTORY might get changed in future and that gitlab_rails command takes ~30 seconds to execute on our installation, which is unacceptably long但是这种方法的问题是GIT_OBJECT_DIRECTORY将来可能会发生变化,并且gitlab_rails命令在我们的安装上执行大约需要 30 秒,这是不可接受的长

UPD2: there is GL_REPOSITORY environment variable with the project ID , that can be used to find out the full path: https://stackoverflow.com/a/57399790/13881221 UPD2:项目IDGL_REPOSITORY环境变量,可用于查找完整路径: https://stackoverflow.com/a/57399790/13881221

UPD3 2020-07-07: there is GL_PROJECT_PATH environment variable included starting with Gitlab 13.1.3 that resolves the issue https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2313 UPD3 2020-07-07:包含从 Gitlab 13.1.3开始的GL_PROJECT_PATH环境变量,可解决问题https://gitlab.com/gitlab-org/gitaly/-/merge_requests/2313

Actually GL_PROJECT_PATH has this format:实际上 GL_PROJECT_PATH 有这种格式:
group/subgroup/repo_name
You can customize update hook on git server and from there, use group or subgroup.您可以在 git 服务器上自定义更新挂钩,并从那里使用组或子组。
For example, you can use regex to get group name like this:例如,您可以使用正则表达式来获取组名,如下所示:
cut -d'/' -f1 <<< $GL_PROJECT_PATH
more information about git hooks: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks有关 git 挂钩的更多信息: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM