退订git提交通知

A method to send email notification to a list of email addresses by the remote git server after every push from the client will be introduced in this post. An example notification email after a commit is shown in the figure below. The subject contains a prefix, the repository name, the branch name and the last commit message. The body of the email contains a summary of the changes and the commit log.

在这篇文章中将介绍一种在客户端每次推送之后通过远程git服务器将电子邮件通知发送到电子邮件地址列表的方法。 下图显示了提交后的示例通知电子邮件。 该主题包含一个前缀,存储库名称,分支名称和最后的提交消息。 电子邮件的正文包含更改摘要和提交日志。

(How does it work)

After the client pushing to the git server, several actions are triggered. These actions are done by hooks. Hooks are scripts placed in the GIT_DIR/hooks directory. Here, GIT_DIR is the directory of the git repository on the git server. One of the trigger action is post-receive,  so we can put a executable script with name post-receive under GIT_DIR/hooks. For every push by clients, post-receive will be executed to send email notification.

客户端推送到git服务器后,将触发几个操作。 这些动作是通过钩子完成的。 挂钩是放置在GIT_DIR / hooks目录中的脚本。 在这里,GIT_DIR是git服务器上git仓库的目录。 触发动作之一是后接收,因此我们可以将名称为后接收的可执行脚本放在GIT_DIR / hooks下。 对于客户的每次推送,将执行后接收以发送电子邮件通知。

Now lets go to the detail of how to set up the email notification.

现在,让我们详细了解如何设置电子邮件通知。




gitlab 取消邮箱认证_java


(Install mailx)

The script uses mailx to send emails out. You need to install mailx on your git server.

该脚本使用mailx发送电子邮件。 您需要在git服务器上安装mailx 。

On Fedora 12+/RHEL6+/CentOS6+, the mailx package works just fine.

在Fedora 12 + / RHEL6 + / CentOS6 +上, mailx软件包可以正常工作。

On Debian/Ubuntu/Linux Mint, please install the heirloom-mailx package instead of using the default bsd-mailx package. The mailx from bsd-mailx does not support all the options used such as -r in the script.

在Debian / Ubuntu / Linux Mint上,请安装heirloom-mailx软件包,而不要使用默认的bsd-mailx软件包。 来自bsd-mailx不支持脚本中使用的所有选项,例如-r 。

(Get the post-receive script)

My script that sends email like the example described above can be downloaded from: git post-receive script.

我的发送电子邮件的脚本(如上述示例)可以从git post-receive script下载。

Or view the source code: git post-receive script

或查看源代码: git post-receive脚本

This script uses mailx to send email by default which uses the local message transfer agent service (like sendmail) to send email. You need to enable the message transfer agent to send out emails on your git server. You can also choose to use smtp to send out email, just find the line

默认情况下,此脚本使用mailx发送电子邮件,该脚本使用本地邮件传输代理服务(例如sendmail )发送电子邮件。 您需要启用邮件传输代理以在git服务器上发送电子邮件。 您也可以选择使用smtp发送电子邮件,只需找到以下行

# cat $email_tmp_file | mailx -S smtp="smtp://smtp.cse.ust.hk" -s "$emailsubject" -r $senderemail $recipients

near to the bottom of the script, uncomment it out, comment out the next line which calls mailx, and change the smtp server to yours. The script will use smtp server to send email. If you like to use Gmail’s smtp, please refer to .

在脚本底部附近,将其取消注释,注释掉调用mailx的下一行,并将smtp服务器更改为您的服务器。 该脚本将使用smtp服务器发送电子邮件。 如果您想使用Gmail的smtp,请参阅 。

This script is changed from Andy Parkins’s one which is in git’s distribution. On fedora 12, the original script can be found under /usr/share/git-core/contrib/hooks.

该脚本与git发行版中的Andy Parkins的脚本有所不同。 在fedora 12上,原始脚本位于/ usr / share / git-core / contrib / hooks下。

(Put the post-receive script into hooks directory)

After getting the post-receive script, you need to copy it to or make a soft link (which I do since there usually many repositories) in the GIT_DIR/hooks/ directory on the git server.

获取接收后脚本后,您需要将其复制到git服务器上的GIT_DIR / hooks /目录中或在其中创建一个软链接(由于通常有很多存储库,因此我会这样做)。

Please note that this script should be executable. You may need to add executable mod bits to post-receive by

请注意,该脚本应该是可执行的。 您可能需要添加可执行的mod位以通过以下方式接收

chmod a+x hooks/post-receive

when you are in GIT_DIR.

当您在GIT_DIR中时。

If you use soft links, setting the original post-receive file once is sufficient.

如果使用软链接,则只需设置一次原始的接收后文件即可。

(Change the repository description)

The repository name in the email subject is the first line of $GIT_DIR/description. Change the first line of the description file to you project’s name.

电子邮件主题中的存储库名称是$ GIT_DIR / description的第一行。 将描述文件的第一行更改为项目的名称。

(Change the config file)

The sender’s email address, the mail list and the subject prefix is defined in GIT_DIR/config file. I like to edit this config file by hand which is as easy as using git config command.

发件人的电子邮件地址,邮件列表和主题前缀在GIT_DIR / config文件中定义。 我喜欢手动编辑此配置文件,就像使用git config命令一样容易。

Add these line to the config file:

将这些行添加到配置文件中:

[hooks]
   mailinglist = "email1@example.com email2@example.com"

The email addressed in mailing list is separated by space.

邮件列表中发送的电子邮件由空格分隔。

The default subject prefix is [GIT]. You can change it to any string you like by adding hooks.emailprefix in the config file.

默认的主题前缀为[GIT]。 您可以通过在配置文件中添加hooks.emailprefix将其更改为所需的任何字符串。

Now the email notification mechanism has been set up. You can change the script if more functions are needed. If you think your new version is great and would like to share it, send me a pull request.

现在,已经建立了电子邮件通知机制。 如果需要更多功能,则可以更改脚本。 如果您认为自己的新版本很棒,并希望与他人分享,请向我发送请求。

翻译自: https://www.systutorials.com/setting-up-git-commit-email-notification/

退订git提交通知