如何在Gitlab中添加服务器端自定义挂钩? [英] How to add Server-side Custom Hooks in Gitlab?

查看:200
本文介绍了如何在Gitlab中添加服务器端自定义挂钩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是,我想在服务器中添加我的钩子到仓库中,以便克隆仓库的人在推送到Gitlab服务器之前都要经过此过程. 到目前为止,我所做的是,在/.git/custom_hooks 的custom_hooks文件夹中创建了 pre-receive 文件,并向其中添加了一些脚本. 以下是我的预接收文件.

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

然后,我在本地Windows计算机中克隆了该存储库,并尝试将其推送.但是它并没有产生预期的效果.它仍然被推送到服务器.

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.

我是Gitlab和Git Hooks的新手.我不知道我的 pre-receive 文件是错误的还是出问题的地方.请让我如何向服务器添加挂钩,以便它为克隆我的存储库的任何人验证/工作. 请帮忙. 预先感谢.

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.

推荐答案

请参见与许多其他版本控制系统一样,Git有一种在发生某些重要操作时触发自定义脚本的方法.这些挂钩有两组:客户端和服务器端.客户端挂钩由诸如提交和合并之类的操作触发,而服务器挂钩由网络操作(如接收推送的提交)运行.您可以出于各种原因使用这些挂钩.

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.

另请参见 https://docs.gitlab.com/ee/administration/server_hooks .html

这篇关于如何在Gitlab中添加服务器端自定义挂钩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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