问题描述
我正在尝试在 Github 中设置一个预接收钩子,我们将使用它来避免不必要的提交。我正在寻找一些需要维护的规则
需要自定义提交消息。
每个提交都应在开始示例中包含一个现有的 JIRA JIRA-XXX | Commit message
也对任何拉取请求强制执行此操作。
需要分支名称格式。
示例:abc_devName_some_br_name_jira_XXX
这里有人能帮我吗,怎么做?
解决方法
Git webhooks 和 git hooks 是不同的选项。我们可以在:
- 客户端 Git 钩子
- 在开发者机器级别工作
- 服务器端 Git 钩子
- 在 git 服务器机器级别工作
- 网络钩子
- 在 git push 的情况下,在事件发生后将事件通知给外部服务(jenkins、travis、circleci 等)
你的标题问题说“webhooks”,但你的问题细节说“pre-receive hook”,所以首先要澄清的是:据我所知,有不同的选项可以单独使用。
我将向您展示一些实现目标的选项:
分支保护
用于保护 master 分支的意外推送。
这些规则是在仓库创建时刻创建的,通常输入参数只是分支名称和角色组。提交消息在这些规则中不可用。
Github 示例
Bitbucket 示例
网络钩子
简单的方法,但发生在 git push 事件之后。步骤:
- 在您的 git 平台(在您的情况下为 github)的设置部分中配置指向您的 c.i 服务器的 webhook
- 在您的 c.i 服务器中创建一个作业/任务来接收事件详细信息(一个包含大量信息的大 json,例如:存储库名称、分支名称、作者邮件、提交 ID、提交消息)
- 在这项工作/任务中,将您的逻辑用于评估提交消息是否遵循您的准则。
- 如果提交消息不符合您的准则,您可以:
- 通过邮件通知
- 使用一些特权帐户,还原提交。我认为这是一些危险的操作,因为开发人员的工作可能会被删除。
- 搜索与提交相关的拉取请求并拒绝它,或者为拉取请求事件创建另一个 webhook 并执行相同操作。
Github 服务器钩子
pre-receive 挂钩功能仅适用于 GitHub Enterprise
基本上这是一个 shell 脚本,它将在您的 github 企业的服务器中运行。
我认为类似于 git 服务器端的钩子。我发现此链接包含注册、更新和删除挂钩的步骤:
现在,如何创建脚本:
我发现的问题是 git message 字符串不是一个可以使用的变量。 在 webhooks 中是!!
如果要访问提交消息,则需要对此进行测试
主要思想是拥有提交ID,您可以访问git服务器中的某些文件,其中包含消息:
tree cfda3bf37b78aef7f4daf
parent 085bb3306e7e7
author Scott Chacon <[email protected]> 1205815931 -0700
committer Scott Chacon <[email protected]> 1240030591 -0700
I'm the message sent by the developer
如果您实现了这一点,您可以添加一条消息,当您的开发人员使用无效的 git 消息执行 git push 时显示:
> Counting objects: 7,done.
> Delta compression using up to 4 threads.
> Compressing objects: 100% (3/3),done.
> Writing objects: 100% (7/7),700 bytes | 0 bytes/s,done.
> Total 7 (delta 0),reused 7 (delta 0)
> remote: error: rejecting all pushes
> To [email protected]:test.git
> ! [remote rejected] main -> main (pre-receive hook declined)
> error: commit message does not meet the rule xyz
服务器端 Git 钩子
类似于github企业功能。我认为在云端或本地机器上安装 git 服务器并不是一种常见的做法,因为它有现成的服务(github、bitbucket、gitlab 等),但它是一种选择。
您可以找到更多详情here
与在 github 企业服务器中一样,您将没有准备好用作变量的提交消息。检查这个:
客户端 Git 钩子
相同的想法,但在开发人员机器级别。更多详情here
基本上在你的开发者机器的任何仓库的 .git 文件夹中,你都可以放置这些脚本。
由于这个脚本是shell脚本,你会遇到同样的问题:git message不是一个可以使用的变量
参考资料
- https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
- https://githooks.com/
- git pre receive hook to check commit message
- https://support.gitkraken.com/working-with-repositories/githooksexample/
- https://git-scm.com/book/en/v2/Customizing-Git-An-Example-Git-Enforced-Policy#Server-Side-Hook
- https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
- https://docs.github.com/en/[email protected]/admin/policies/enforcing-policy-with-pre-receive-hooks/managing-pre-receive-hooks-on-the-github-enterprise-server-appliance
- https://docs.github.com/en/[email protected]/admin/policies/enforcing-policy-with-pre-receive-hooks/creating-a-pre-receive-hook-script
- https://github.community/t/using-pre-receive-hooks-in-github-enterprise/13506
- https://docs.github.com/en/github/administering-a-repository/defining-the-mergeability-of-pull-requests/managing-a-branch-protection-rule
- Link to the issue number on GitHub within a commit message
- Is it possible to reject a commit on Github if the commit isn't formatted correctly?
- Git hook to reject commits where files contain a specific string
- https://medium.com/@f3igao/get-started-with-git-hooks-5a489725c639
- Example of a pre-receive Git hook
- https://github.blog/2013-01-22-closing-issues-via-commit-messages/