ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Gitlab per-recive预处理配置

2021-03-17 19:34:12  阅读:243  来源: 互联网

标签:git hooks recive Gitlab per gitlab print commit subject


背景:通过git提交规约对开发者的提交进行限制,为了后期对开发者提交动作进行分析与相关数据的报表展示。

 

Gitlab提供的钩子

          1.1 单一仓库

              通过此模式,我们可以将自定义的预处理代码部署到指定的仓库。

 

          1.2 激活方式

                源码安装的配置目录,通常如下:

             /home/git/repositories/<group>/<project>.git

 

                官方源安装的配置目录,通常如下:

             /var/opt/gitlab/git-data/repositories/<group>/<project>.git

 

                在项目目录下创建custom_hooks目录:

                   mkdir -pv /var/opt/gitlab/git-data/repositories/<group>/<project>.git/custom_hooks

 

                 新建一个名为"pre-receive"的开发文件,这个是官方规定。接下来可以进行自定义编码。

               

          2.1 全局

              通过此模式,我们可以将自定义的预处理代码应用到整个仓库。

 

      2.2 激活方式:

              通过编辑/etc/gitlab/gitlab.rb配置文件,增加gitlab_shell['custom_hooks_dir']参数来配置自定义配置文件存放位置,如下是我的配置:

              gitlab_shell['custom_hooks_dir'] = "/opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks_dir"

 

              刷新Gitlab配置(重启库操作,生产环境需要评估操作时间)

          gitlab-ctl reconfigure

          gitlab-ctl restart

 

        配置更新成功后,需要在自定义钩子的目录下创建其中一个文件夹pre-receive.dpost-receive.dupdate.d  这个是官方的规定:

                mkdir -pv /opt/gitlab/embedded/service/gitlab-shell/hooks/custom_hooks_dir/pre-receive.d

 

              新建一个名为"pre-receive"的开发文件,这个是官方规定。接下来可以进行自定义编码。

             

二、实现DEMO

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import re
import subprocess
import json

match_pattern=['feat', 'fix', 'docs', 'style', 'refactor', 'perf', 'test', 'build', 'ci', 'chore', 'revert', 'merge']
regex = re.compile("(" + "|".join(match_pattern) + ")")
line = sys.stdin.read()
(base, commit, ref) = line.strip().split()
new_branch_push = re.match(r'[^1-9A-Za-z]+', base)
branch_deleted = re.match(r'[^1-9A-Za-z]+', commit)
contains_commit_msg = False
if not new_branch_push:
    revs = base + "..." + commit
    #proc = subprocess.Popen(['git', 'rev-list','--oneline','--first-parent', revs], stdout=subprocess.PIPE)
    proc = subprocess.Popen(['git', 'log', '--first-parent', revs, '--pretty=format:{"author":"%cn","subject":"%s"}'], stdout=subprocess.PIPE)
    lines = proc.stdout.readlines()
    if lines:
        for line in lines:
            rev = json.loads(str(line))
            print(rev)
            try:
                subject_type, subject_message = rev['subject'].split(':', 1)
            except:
                contains_commit_msg = False
                break
            subject_message = subject_message.strip()
            print subject_message
            match = regex.search(subject_type.lower())
            if match:
                contains_commit_msg = True
if contains_commit_msg or new_branch_push or branch_deleted:
    print "Commit success"
    exit(0)
else:
    print('\033[1;31;40m')     #下一目标输出背景为黑色,颜色红色高亮显示
    print('*' * 50)
    print('\033[7;31m不合规的提交格式!')  #字体颜色红色反白处理
    print('\033[7;31m参考示例: git commit -m "feat 666: New FEAT"')  #字体颜色红色反白处理
    print('\033[7;31m更多请参考:https://git.com.cn/docs/SA\033[1;31;40m') 
    print('*' * 50)
    print('\033[0m')
    exit(1)
View Code

 

参考文档:

官方说明(请参考自己使用的版本说明书): https://docs.gitlab.com/ee/administration/server_hooks.html

为你的 GitLab 增加提交信息检测 _: https://mritd.com/2018/05/11/add-commit-message-style-check-to-your-gitlab/

标签:git,hooks,recive,Gitlab,per,gitlab,print,commit,subject
来源: https://www.cnblogs.com/milanin9/p/14551291.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有