1. 修改 gitlab 配置 vi /etc/gitlab/gitlab.rb ,增加 custom_hooks_dir 路径:

默认路径: /opt/gitlab/embedded/service/gitlab-shell/hooks/

2. 执行 sudo gitlab-ctl reconfigure

3. 创建 hook 文件

自定义脚本目录要符合 <custom_hooks_dir>/<hook_name.d>/*的规范。具体来说就是:

  • 在自定的 custom_hooks_dir目录下可创建三个文件夹对应三类 server hook name
    • pre-receive.d
    • update.d
    • post-receive.d
  • 在每个文件夹下可创建任意文件,在对应的 hook 时期,gitlab 就会主动调用
  • 文件名以 ~结尾的文件会被忽略
  • 如果想看这部分的实现细节可以看 <gitlab-shell>/lib/gitlab_custom_hook.rb文件


4.环境变量

Environment variable Description
GL_ID GitLab identifier of user that initiated the push. For example, user-2234
GL_PROJECT_PATH (GitLab 13.2 and later) GitLab project path
GL_PROTOCOL (GitLab 13.2 and later) Protocol used with push
GL_REPOSITORY project-<id> where id is the ID of the project
GL_USERNAME GitLab username of the user that initiated the push

Pre-receive and post-receive server hooks can also access the following Git environment variables.

Environment variable Description
GIT_ALTERNATE_OBJECT_DIRECTORIES Alternate object directories in the quarantine environment. See Git receive-packdocumentation.
GIT_OBJECT_DIRECTORY GitLab project path in the quarantine environment. See Git receive-pack documentation.
GIT_PUSH_OPTION_COUNT Number of push options. See Git pre-receivedocumentation.
GIT_PUSH_OPTION_<i> Value of push options where i is from 0 to GIT_PUSH_OPTION_COUNT - 1. See Git pre-receivedocumentation.

 

git config credential.helper store

标签: none

添加新评论