文章目录
- 前言
- 解决方案
- 遇到的错误
- 域名解析问题
- 解决办法
- 邮箱认证问题
- 解决方案
- 端口配置互斥
- 解决方案
- 运行环境
- docker安装
- 总结
前言
这个问题网上解决的帖子很多,根本原因就是配置错误,修改好配置就可以了,我也没有其他的更加新奇的解决办法,之所以再总结一遍只是为了自己统一查找方便,同时把一些差异点记录一下,因为版本和运行环境的不同,可能跟网上的解决方案有一点点差异,先写答案再聊不同吧。
解决方案
-
开启企业微信邮箱(QQ邮箱、163邮箱亦可)的POP3/SMTP服务,获取授权码
-
修改gitlab的配置文件
/etc/gitlab/gitlab.rb
如下
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "gitlab@gameup.com"
gitlab_rails['smtp_password'] = "授权码"
gitlab_rails['smtp_domain'] = "exmail.qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = false
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'gitlab@gameup.com'
gitlab_rails['gitlab_email_display_name'] = 'Gitlab'
-
重新加载配置生效
gitlab-ctl reconfigure
,等待执行完成 -
利用gitlab测试配置是否成功
- 执行 gitlab-rails console进入控制台,我的机器性能较差,多等一会看到命令提示符
- 输入后面面的命令
Notify.test_email('收件人邮箱', '邮件标题', '正文').deliver_now
发送一封测试邮件 - 若成功过则收到测试邮件,失败则会在控制台显示错误消息,根据错误信息检查和修改配置即可
遇到的错误
我修改配置的旅程也不是一帆风顺的,期间也遇到的了几个错误,都是在 Notify.test_email
测试之后发现有问题,逐步修改的
域名解析问题
Notify.test_email('shz@gamegu.com', 'Test Email', 'This is a test email from GitLab').deliver_now
--------------------------------------------------------------------------------
Ruby: ruby 3.2.5 (2024-07-26 revision 31d0f1a2e7) [x86_64-linux]
GitLab: 17.5.1 (e8dca573167) FOSS
GitLab Shell: 14.39.0
PostgreSQL: 14.11
------------------------------------------------------------[ booted in 61.07s ]
Loading production environment (Rails 7.0.8.4)
Delivered mail 67248a5c847e9_6f32fe445660@gitlab.gamegu.com.mail (20023.7ms)
/opt/gitlab/embedded/lib/ruby/3.2.0/socket.rb:231:in getaddrinfo': getaddrinfo: Temporary failure in name resolution (SocketError)
from /opt/gitlab/embedded/lib/ruby/3.2.0/socket.rb:231:in foreach'
from /opt/gitlab/embedded/lib/ruby/3.2.0/socket.rb:635:in tcp'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:643:in tcp_socket'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:656:in do_start'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:611:in start'
from /opt/gitlab/embedded/service/gitlab-rails/config/initializers/mail_starttls_patch.rb:53:in start_smtp_session'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/network/delivery_methods/smtp.rb:100:in deliver!'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/message.rb:2145:in do_delivery'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/message.rb:253:in block in deliver'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/actionmailer-7.0.8.4/lib/action_mailer/base.rb:588:in block in deliver_mail'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/notifications.rb:206:in block in instrument'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/notifications/instrumenter.rb:24:in instrument'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/notifications.rb:206:in instrument'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/actionmailer-7.0.8.4/lib/action_mailer/base.rb:586:in deliver_mail'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/message.rb:253:in deliver'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/actionmailer-7.0.8.4/lib/action_mailer/message_delivery.rb:119:in block in deliver_now'
... 15 levels...
irb(main):002:0>
解决办法
修改 /etc/resolv.conf
来指定自己服务器的DNS,通用的可以使用 nameserver 8.8.8.8
邮箱认证问题
/opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:1076:in check_response': 501 mail from address must be same as authorization user (Net::SMTPSyntaxError)
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:1044:in getok'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:945:in mailfrom'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:767:in send_message'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/network/delivery_methods/smtp_connection.rb:53:in deliver!'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/network/delivery_methods/smtp.rb:101:in block in deliver!'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/net-smtp-0.3.3/lib/net/smtp.rb:612:in start'
from /opt/gitlab/embedded/service/gitlab-rails/config/initializers/mail_starttls_patch.rb:53:in start_smtp_session'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/network/delivery_methods/smtp.rb:100:in deliver!'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/message.rb:2145:in do_delivery'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/message.rb:253:in block in deliver'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/actionmailer-7.0.8.4/lib/action_mailer/base.rb:588:in block in deliver_mail'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/notifications.rb:206:in block in instrument'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/notifications/instrumenter.rb:24:in instrument'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/notifications.rb:206:in instrument'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/actionmailer-7.0.8.4/lib/action_mailer/base.rb:586:in deliver_mail'
from /opt/gitlab/embedded/lib/ruby/gems/3.2.0/gems/mail-2.8.1/lib/mail/message.rb:253:in deliver'
... 16 levels...
解决方案
smtp
配置中应该填写正确的邮箱和授权码,同时 /etc/gitlab/gitlab.rb
中设置 gitlab_rails['gitlab_email_from']
与 gitlab_rails['smtp_user_name']
一致
端口配置互斥
gitlab_rails['smtp_tls'] and gitlab_rails['smtp_enable_starttls_auto'] are mutually exclusive. Set one of them to false. SMTP providers usually use port 465 for TLS and port 587 for STARTTLS.
解决方案
这个错误表明在 GitLab 的配置文件中同时启用了 smtp_tls
和 smtp_enable_starttls_auto
选项,但它们是互斥的,只能选择其中一个设置为 true,如果使用 TLS(通常是端口 465)仅将 smtp_tls
选项设置为 true,如果使用 STARTTLS(通常是端口 587)只能将 smtp_enable_starttls_auto
设置为true。
关于gitlab的参数,有一个external_url
表示gitlab的访问域名,可以配置为 external_url 'http://gitlab.gameup.com'
,而这个域名也可以在定义容器时通过 --hostname
指定
运行环境
我的Gitlab不是在服务器上直接安装的,而是通过Docker安装的gitlab镜像,服务器是CentOS7系统,gitlab组件相关版本如下:
Components
GitLab v17.5.1
GitLab Shell 14.39.0
GitLab Workhorse v17.5.1
GitLab API v4
GitLab KAS 17.5.1
Ruby 3.2.5p208
Rails 7.0.8.4
PostgreSQL (main) 14.11
PostgreSQL (ci) 14.11
Redis 7.0.15
正因为我是通过Dokcer安装的,所以上面的很多命令需要到容器中执行,也就是得先执行 docker exec -it gitlab /bin/bash
而通过Docker安装gitlab的命令也列举一下:
docker run --detach \
--hostname gitlab.gameup.com \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume /export/docker/gitlab/config:/etc/gitlab \
--volume /export/docker/gitlab/logs:/var/log/gitlab \
--volume /export/docker/gitlab/data:/var/opt/gitlab \
registry.cn-hangzhou.aliyuncs.com/z5z/gitlab-ce:latest
不要问我这个镜像名字为什么这样古怪,那么因为DockerHub镜像在当前的网络环境下实在难以下载,不得不借助Github的Actions和阿里云的镜像完成中转,可以利用这个开源项目 进行中转
docker安装
上面说了镜像难装,实际上现在连Docker的安装也需要费点劲,我曾经在AWS上安装过一些Docker镜像,那丝滑的程度真的跟教科书写的一模一样,但是在我目前所处的网络注定要一波三折了,CentOS7的系统可以按照下面的步骤操作了。
# 备份源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# yum使用阿里云
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sudo yum makecache
# 安装必要的一些系统工具
sudo yum install -y wget yum-utils device-mapper-persistent-data lvm2
# 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 修改源
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# 开启Docker服务
sudo systemctl start docker
总结
- gitlab的配置文件为
/etc/gitlab/gitlab.rb
, 发邮件需要配置smtp
和email
参数 smtp_tls
和smtp_enable_starttls_auto
选项是互斥的,只能选择其中一个设置为 true- 进入到docker镜像内部的命令为
docker exec -it gitlab /bin/bash
他时若遂凌云志,敢笑黄巢不丈夫。伟人可以被人们敬仰,也可以被用来作为登天的阶梯,借他人之成就,抒发自己之情怀~