How to add Server-side Custom Hooks in Gitlab?

落花浮王杯 提交于 2020-05-17 08:52:05

问题


What I am trying to do is that i want to add my hooks to repo in server, so that whoever has cloned the repo, passes through this before pushing to Gitlab server. So far what i have done is, created pre-receive file in custom_hooks folder in /.git/custom_hooks and added some script to it. Below is my pre-receive file.

#!/bin/bash

zero_commit="0000000000000000000000000000000000000000"
excludeExisting="--not --all"

while read oldrev newrev refname; do
  # echo "payload"
  echo $refname $oldrev $newrev

  # branch or tag get deleted
  if [ "$newrev" = "$zero_commit" ]; then
    continue
  fi

  # Check for new branch or tag
  if [ "$oldrev" = "$zero_commit" ]; then
    span=`git rev-list $newrev $excludeExisting`
  else
    span=`git rev-list $oldrev..$newrev $excludeExisting`
  fi

  for COMMIT in $span;
  do
    for FILE  in `git log -1 --name-only --pretty=format:'' $COMMIT`;
    do
        echo "rejecting all pushes"
        exit 1
    done
  done
done
exit 0

Then I cloned the repo in my local Windows machine and tried pushing it. But it didnt create the intended effect. It still got pushed to server.

I'm new to Gitlab and Git Hooks. I don't know whether my pre-receive file is wrong or where I am going wrong. Please let me how to add hooks to server so that it validates/works for whoever cloned my repo. Please help. Thanks in advance.


回答1:


See https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks which starts with:

Like many other Version Control Systems, Git has a way to fire off custom scripts when certain important actions occur. There are two groups of these hooks: client-side and server-side. Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits. You can use these hooks for all sorts of reasons.

See also https://docs.gitlab.com/ee/administration/server_hooks.html



来源:https://stackoverflow.com/questions/61653972/how-to-add-server-side-custom-hooks-in-gitlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!