GitLab自定义全局服务器钩子

服务器钩子

全局服务器钩子

/opt/gitlab/embedded/service/gitaly-ruby/git-hooks 这个目录下的钩子脚本,如果调整,则会覆盖 gitlab 本身的钩子:比如调整了此目录下的钩子程序,会覆盖 gitlab 的受保护分支不得push的钩子规则。

自定义全局钩子

  • 修改/etc/gitlab/gitlab.rb 中的 gitlab_shell['custom_hooks_dir'] = "/var/opt/gitlab/gitaly/custom_hooks" 并取消注释;
  • gitlab-ctl reconfigure
  • gitlab-ctl restart
  • 新增钩子目录:mkdir -p /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/ /var/opt/gitlab/gitaly/custom_hooks/post-receive.d/ /var/opt/gitlab/gitaly/custom_hooks/update.d/
  • 创建钩子程序:
cat > /var/opt/gitlab/gitaly/custom_hooks/pre-receive.d/deny_commit_files << 'EOF'
#!/bin/bash
# 这是一段拒绝 .gitlab-ci.yml 文件提交的钩子
APPROVAL_USERS='zhuchun'
NOT_ALLOWED_FILE='.gitlab-ci.yml'

echo $GL_USERNAME

while read -r oldVersion newVersion branch; do
  revss=$(git diff --name-only "$oldVersion" "$newVersion")
  for rev in $revss
  do 
    if [[ $rev =~ $NOT_ALLOWED_FILE ]] && [[ $GL_USERNAME != $APPROVAL_USERS ]];then
      echo "Your commit contains files that are not allowed to be modified, please confirm."
      echo $NOT_ALLOWED_FILE
      echo "$GITALY_BIN_DIR/gitaly-hooks" "$(basename $0)" "$@"
      exit 1
    fi
  done
done

EOF

仓库服务器钩子

系统钩子

文件钩子