配置git push后自动发送邮件来进行邮件方式的代码评审

前言

任何对原有系统的修改,必须先进行备份,然后再慎重修改。不然可能会解决了1个问题,然后带来2个新的问题。慎之!慎之! 本文介绍的过程,是对git hook的应用。如果是想面向所有的git库,请参考第4个链接中的全局hook配置进行调整。

1. 安装msmtp

1.下载msmtp源码: https://marlam.de/msmtp/download/
2.编译msmtp:
cd msmtp-1.8.2
./configure –prefix=/usr/local/msmtp
make
make install
3.配置msmtp:
cd /usr/local/msmtp
mkdir etc
vim msmtprc
#set default values for all following accounts.
defaults
logfile /usr/local/msmtp/msmtp.log
#the SMTP server of provider
account public
#SMTP邮件服务器地址
host <邮件服务器地址>
port 25
#发送邮件的邮箱
from <发送邮件的邮箱>
auth login
#登陆邮件服务器的账号
user <发送邮件的用户>
#密码
password <邮箱密码>
account default: <同发送邮件的账号>

4.注意:
① 必须在root以及使用msmtp的用户的home目录下创建这个配置文件$HOME/.msmtprc,内容如上即可,否则测试的时候,会提示用户找不到;
② 这个文件的权限必须是600,因为里面存在明文密码信息: chmod 600 $HOME/.msmtprc
③ 对于普通用户,要能够访问/usr/local/msmtp/msmtp.log, 注意更改权限;
5.测试MSMTP功能:
bin/msmtp freflying1119@google.com
Just a test for msmtp.
【Ctrl +d】

2. 安装配置MUTT

yum -y install mutt
Vim /etc/Muttrc
在文件末尾添加如下配置:
set from="<发送邮件的邮箱>"
set sendmail="/usr/local/msmtp/bin/msmtp"
set use_from=yes
set realname=“Git-Server”
set editor=“vim”
保存退出后便可以测试一下msmtp+mutt的配置是否成功:
echo “hello world” | mutt -s “test again” freflying1119@google.com

3. 配置Gitlab的邮件提醒

1.在gitlab server上定位配置文件路径:
find / -name production.rb
2.修改production.rb
cd /opt/gitlab/embedded/service/gitlab-rails/config/environments/
Vi production.rb
在# config.action_mailer.delivery_method = :sendmail下加入
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {

:address => “<邮件服务器地址>”,

:port => 25,

:domain => ‘<公司邮件后缀名>’,

:user_name => ‘<发送邮件的邮箱账号>’,

:password => ‘<发送邮件的邮箱密码>’,

:authentication => :plain,

:enable_starttls_auto => true

}

3.编辑config/gitlab.yml(/opt/gitlab/embedded/service/gitlab-rails/config)
vi config/gitlab.yml
对应修改一下配置
email_enabled:
email_from: <发送邮件的邮箱账号>
email_display_name: Gitlab Server
protocol: http

4. 配置要发送通知的git库

在这里我开始犯了一个错误,我直接使用post-receive-email hook脚本替换了原来Gitlab的post-receive hook, 导致Gitlab出现2个问题:一是当开发人员提交代码后,在Gitlab 的GUI界面上看不到,一段时间内碰到这个问题时,只能通过运行命令 “gitlab-rake cache:clear RAILS_ENV=production”来解决;二是导致所有push操作后的自动触发任务不能运行,比如导致开发配置的.gitlab-ci.yaml的自动化脚本不再运行。 在事情发生一段时间后,终于找到了解决方案。

1.进入到repositories中:
cd <代码库>.git
2. 在git库下创建一个custom_hooks目录:
cp /usr/share/git-core/contrib/hooks/post-receive-email custom_hooks/post-receive
修改目录用户和权限:
chown -R git:git custom_hooks
chmod 755 custom_hooks/post-receive
3.修改项目的config文件,新增如下内容:
[hooks]
mailinglist ="<相关邮件通知的人员的地址,多个用户地址用逗号分隔>"
sendmail = /usr/local/msmtp/bin/msmtp
envelopesender = <前面配置的邮箱地址>
emailprefix = "Git Push Notice: "
emailfrom = “Git Server”
showrev = “git show -C %s;echo”
注意:mailinglist用逗号分隔,如果用空格的话,只给最后一个人发邮件。
4.编辑项目的Description名称
vim description
alliance-coss-admin
5.修改hooks/post-receive脚本的send_mail函数:替换sendmail为msmtp;
修改脚本

6.因为post-receive 脚本是由git调用的,所以必须在git用户的home目录下,创建.msmtprc配置文件,并设置权限为600, 参见第一章内容; 同时也要让git能访问msmtp的log文件;
chmod 777 /usr/local/msmtp/msmtp.log

5. 测试邮件发送功能

1.下载代码
git clone <代码>.git
2.添加一个文件HelloWorld.java
git add HelloWorld.java
git commit -m “add HelloWorld.java”
git push origin dev1
3.检查是否收到邮件即可

参考文档

  1. https://blog.csdn.net/Rebel_Yangke/article/details/52470764
  2. https://www.cnblogs.com/hanxianlong/p/3463494.html
  3. https://blog.csdn.net/csr_hema/article/details/8060702?utm_medium=distribute.pc_relevant.none-task-blog-title-1&spm=1001.2101.3001.4242
  4. https://docs.gitlab.com/ee/administration/server_hooks.html
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值