Site icon Soul & Shell Blog

GitLab 設定 Email 透過 Gmail SMTP 發送信件 (筆記)

GitLab 是一個非常好用的版本控制系統,雖然 GitLab 在專案管理與工作流程功能上,比起開源的 Redmine 或閉源的 Jira 相對薄弱一些,但是我特別喜歡 GitLab 的 UI/UX 與專案透明的設計。自從用了 GitLab CI/CD Pipeline,就越來越少使用 Jenkins,GitLab CI/CD 比起 Jenkins Build Script 優美多了。

GitLab 設定 Gmail SMTP 發信教學

今天要介紹如何設定 GitLab 透過 Gmail SMTP 發信 (參考官方文件),開始設定以前要先開啟準備發信 Google Account 的「低安全性應用程式存取權」,不然 SMTP 認證會失敗,請登入「應用程式存取權」並開啟,如下:

然後在 GitLab 機器上編輯 /etc/gitlab/gitlab.rb 這一支檔案,如下:

external_url 'http://your-doamin'

gitlab_rails['time_zone'] = 'Asia/Taipei'

gitlab_rails['gitlab_email_enabled'] = true

gitlab_rails['gitlab_email_from'] = 'your_email'

gitlab_rails['gitlab_email_display_name'] = 'GitLab'

gitlab_rails['gitlab_shell_ssh_port'] = 22

gitlab_rails['smtp_enable'] = true

gitlab_rails['smtp_address'] = 'smtp.gmail.com'

gitlab_rails['smtp_port'] = 587

gitlab_rails['smtp_user_name'] = 'your_email'

gitlab_rails['smtp_password'] = 'your_email_password'

gitlab_rails['smtp_domain'] = 'smtp.gmail.com'

gitlab_rails['smtp_authentication'] = 'login'

gitlab_rails['smtp_enable_starttls_auto'] = true

gitlab_rails['smtp_tls'] = false

gitlab_rails['smtp_openssl_verify_mode'] = 'peer'

如果是透過 Docker Compose 部署 GitLab 也可以在 yml 加入資訊,docker-compose.yml 範例如下:

version: '3.7'

services:

gitlab:

image: 'gitlab/gitlab-ce:12.9.2-ce.0'

restart: always

hostname: 'gitlab.imp.justup.co'

container_name: gitlab

environment:

GITLAB_OMNIBUS_CONFIG: |

external_url 'http://your-doamin'

gitlab_rails['time_zone'] = 'Asia/Taipei'

gitlab_rails['gitlab_email_enabled'] = true

gitlab_rails['gitlab_email_from'] = 'your_email'

gitlab_rails['gitlab_email_display_name'] = 'GitLab'

gitlab_rails['gitlab_shell_ssh_port'] = 22

gitlab_rails['smtp_enable'] = true

gitlab_rails['smtp_address'] = 'smtp.gmail.com'

gitlab_rails['smtp_port'] = 587

gitlab_rails['smtp_user_name'] = 'your_email'

gitlab_rails['smtp_password'] = 'your_email_password'

gitlab_rails['smtp_domain'] = 'smtp.gmail.com'

gitlab_rails['smtp_authentication'] = 'login'

gitlab_rails['smtp_enable_starttls_auto'] = true

gitlab_rails['smtp_tls'] = false

gitlab_rails['smtp_openssl_verify_mode'] = 'peer'

ports:

- 80:80

volumes:

- '../volumes/gitlab/config:/etc/gitlab'

- '../volumes/gitlab/logs:/var/log/gitlab'

- '../volumes/gitlab/data:/var/opt/gitlab'

修改好設定檔以後透過 gitlab-ctl reconfigure 重新載入 RoR 設定,接著可以透過 gitlab-rails console 進入 Console 進行除錯,並輸入以下命令來進行發信測試:

Notify.test_email('your_email', 'test', 'test').deliver_now

發信成功的畫面如下:

大概是這樣,Good Lock...

Exit mobile version