温馨提示×

CentOS中GitLab的邮件通知设置

小樊
40
2025-10-06 23:55:27
栏目: 智能运维

1. 安装并配置Postfix邮件服务器
Postfix是GitLab默认使用的邮件传输代理(MTA),需先安装并配置才能让GitLab发送邮件。

  • 安装Postfix:运行sudo yum install postfix命令安装。安装过程中选择“Internet Site”类型,并设置系统邮件名称(如gitlab.example.com)。
  • 配置Postfix:编辑/etc/postfix/main.cf文件,修改以下关键参数(替换为你的实际信息):
    myhostname = gitlab.example.com  # GitLab实例主机名
    mydomain = example.com           # 域名
    myorigin = $mydomain             # 发件域名
    inet_interfaces = all            # 监听所有网络接口
    inet_protocols = ipv4            # 仅使用IPv4
    mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain  # 接收邮件的目标域
    relayhost =                      # 不转发邮件到其他SMTP服务器
    smtpd_tls_security_level = may   # 可选TLS加密(根据SMTP服务器要求调整)
    
  • 启动Postfix:运行sudo systemctl start postfix启动服务,并设置开机自启sudo systemctl enable postfix

2. 配置GitLab邮件通知参数
编辑GitLab主配置文件/etc/gitlab/gitlab.rb,找到或添加以下SMTP设置(替换为你的SMTP服务器信息):

gitlab_rails['smtp_enable'] = true                # 启用SMTP
gitlab_rails['smtp_address'] = "smtp.example.com" # SMTP服务器地址(如smtp.gmail.com)
gitlab_rails['smtp_port'] = 587                   # SMTP端口(常用587或465)
gitlab_rails['smtp_user_name'] = "your-email@example.com"  # SMTP用户名(如个人邮箱)
gitlab_rails['smtp_password'] = "your-email-password"      # SMTP密码(或应用专用密码)
gitlab_rails['smtp_domain'] = "example.com"       # 发件域名(与Postfix的mydomain一致)
gitlab_rails['smtp_authentication'] = "login"     # 认证方式(常用login/plain)
gitlab_rails['smtp_enable_starttls_auto'] = true  # 启用StartTLS加密(推荐)
gitlab_rails['smtp_tls'] = false                  # 不使用SSL(若SMTP服务器要求SSL则设为true)
gitlab_rails['smtp_openssl_verify_mode'] = 'peer' # 证书验证模式(peer严格验证,self_signed信任自签名,none忽略)

注意:若使用Gmail等第三方SMTP服务,需开启“允许不够安全的应用”或创建应用专用密码;若SMTP服务器使用自签名证书,将openssl_verify_mode设为none

3. 重新配置并重启GitLab
保存gitlab.rb文件后,运行以下命令使配置生效:

sudo gitlab-ctl reconfigure  # 应用GitLab配置变更
sudo gitlab-ctl restart      # 重启GitLab服务

4. 测试邮件通知功能

  • 方法1:通过GitLab Web界面测试
    登录GitLab,进入“Admin Area”(管理员区域)→“Settings”(设置)→“General”(常规)→“Email Server”(邮件服务器),向下滚动找到“Test email configuration”(测试邮件配置),输入测试邮箱地址(如你的个人邮箱),点击“Send test email”(发送测试邮件)。若配置正确,应能收到包含测试内容的邮件。
  • 方法2:通过命令行测试
    运行sudo gitlab-rake gitlab:email:test命令,若输出“Email sent successfully”(邮件发送成功),则表示配置无误。

常见问题排查

  • 无法发送邮件:检查Postfix服务是否运行(sudo systemctl status postfix),查看GitLab和Postfix日志(/var/log/gitlab/gitlab-rails/production.log/var/log/maillog)定位错误。
  • SSL证书问题:若使用自签名证书,将smtp_openssl_verify_mode设为none;若使用公共SMTP服务(如Gmail),确保openssl_verify_modepeer并正确配置证书。
  • SMTP认证失败:确认SMTP用户名、密码正确,若使用第三方服务(如Gmail),检查是否开启了“两步验证”或生成了应用专用密码。

0