配置用戶提交評論、添加issue等的郵件通知:
Gitlab郵件提醒方便跟蹤項目進度,在這里介紹兩種方式,一種是用系統的sendmail發送郵件,另一種是GMAIL的stmp來發送郵件
第一種 用系統的sendmail發送郵件
cd /home/gitlab/gitlab/
vi config/environments/production.rb
將這行 # config.action_mailer.delivery_method = :sendmail
改為 config.action_mailer.delivery_method = :sendmail
保存config/environments/production.rb
編輯config/gitlab.yml
vi config/gitlab.yml
對應修改一下配置
web:
host: gitlab123.com
port: 80
https: false
email:
from: notify@gitlab123.com
protocol: http
host: gitlab123.com
git_host:
host: gitlab123.com
編輯/etc/hosts
加入你的ip對應gitlab123.com
10.0.0.71 gitlab123.com
第二種 GMAIL的stmp來發送郵件
cd /home/gitlab/gitlab/
vi config/environments/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 => "smtp.gmail.com",
:port => 587,
:domain => 'gmail.com',
:user_name => 'account@gmail.com',
:password => 'password',
:authentication => :plain,
:enable_starttls_auto => true
}
#配置好你的郵箱和密碼
編輯config/gitlab.yml
vi config/gitlab.yml
對應修改一下配置
email:
from: account@gmail.com
protocol: http
配置源碼push到版本庫時的通知:
0、先安裝msmtp,參考http://www.cnblogs.com/hanxianlong/p/3474429.html
1、進入到repositories中,如/opt/gitlab-6.3.0-0/apps/gitlab/repositories/imagesys5/test.git
cd /opt/gitlab-6.3.0-0/apps/gitlab/repositories/imagesys5/test.git
2、到項目的hooks目錄中,新建一個名為post-receive的hook軟鏈接,指向到/usr/share/doc/git-1.7.1/contrib/hooks/post-receive-email
ln -s /usr/share/doc/git-1.7.1/contrib/hooks/post-receive-email hooks/post-receive
3、編輯項目的config文件
vim config
新增如下配置節:
[hooks]
mailinglist = "YOURMAIL1@MAILSERVER.COM,YOURMAIL2@MAILSERVER.COM"
emailprefix="[GIT]"
4、編輯項目的Description名稱
vim description
修改內容為項目名稱
5、修改/usr/share/doc/git-1.7.1/contrib/hooks/post-receive-email文件的send_mail函數(大約640行和642行),將/usr/sbin/sendmail替換為/usr/bin/msmtp
send_mail()
638 {
639 if [ -n "$envelopesender" ]; then
640 #/usr/sbin/sendmail -t -f "$envelopesender"
641 /usr/bin/msmtp -t -f "$envelopesender"
642 else
643 #/usr/sbin/sendmail -t
644 /usr/bin/msmtp -t
645 fi
646 }
##############################
Introduction
為Git repository 添加hook,每次commit 時發送一封diff email
Procedure
-
拷貝Git-core自帶的post-receive-email Shell 腳本
mv YOUR_REPO_PATH/hooks/post-receive YOUR_REPO_PATH/hooks/post-receive.back cp PATH/TO/GIT-CORE/contrib/hooks/post-receive-email YOUR_REPO_PATH/hooks/post-receive chmod a+x YOUR_REPO_PATH/hooks/post-receive
-
設置 Git repository 的config
vi YOUR_REPO_PATH/config
[hooks] mailinglist = “Receive1@email.address,Receive2@email.address” emailprefix = “[git]” announcelist = envelopesender = “SenderSender@email.address” sendmail = /usr/sbin/sendmail showrev=
其中 mailinglist
為收件人,emailprefix
為郵件前綴,announce list
為repo創建tag時的匯總信息郵件收件人列表, evelopesender
為發件郵箱地址,sendmail
為發件程序地址,showrev
為Log打印格式。
然后推送一個提交試試。It's work!
More useful
Email Content formatted
發送的是純文本的郵件,對於Diff信息很難辨識。我們可以通過修改post-receive
腳本自定義郵件樣式。 [Making Git show post-receive emails as an HTML colour formatted diff]提供了這個腳本,我就直接拿來用。
但是標題我們要求是"[ProjectName][CommitAuthor][Action][Branch]CommitLog"
,所以還需要自己定制。 首先是獲取ProjectName,因為我們是使用GitLab作為Git repository server,它集成 gitelitolite 了,自己負責創建項目,一些MetaData並沒有保存在Repo中,而是在數據庫中。比如ProjectName,它沒有提供YOUR_REPO_PATH/description
這個文件,所以直接使用文章中的post-receive
會出現項目名稱為空的情況。
我就將ProjectName
設置為Repo目錄名稱:
projectdesc=$(basename $(pwd))
projectdesc=${projectdesc%.git*}
然后我們再修改郵件標題:
Subject: ${emailprefix}[$projectdesc][$(git log --pretty=format:"%cn" -1 $describe)][${change_type}d][$short_refname]$(git log --pretty=format:"%s" -1 $describe)
其中$(git log --pretty=format:"%s" -1 $describe
為獲取提交日志,$describe
為Commit reversion。
好了自此,郵件內容格式問題解決了。
Integrate with Gitlab
並不是所有的Repo都需要郵件綁定,不過我們可以將這些配置放到YOUR_REPO_PATH/config
中作為開關。
此外Gitlab Dashboard的提交動態同樣需要綁定post-receive
hook,所以需要將這兩個腳本整合在一起。
我們直接將Gitlab工作的那一行邏輯復制到我們的腳本中。
這里發生一個事故,我直接粘貼rpush
那一行,導致其中變量引用錯誤,Gitlab 的默認版本不是$refname
,導致提交了部分臟數據到Gitlab中,最后通過刪除那部分的events
數據解決。耗時半小時。
# --- Main loop
# Allow dual mode: run from the command line just like the update hook, or
# if no arguments are given then run as a hook script
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
# Output to the terminal in command line mode - if someone wanted to
# resend an email; they could redirect the output to sendmail
# themselves
PAGER= generate_email $2 $3 $1
else
while read oldrev new rev refname
do
repo_path=`pwd`
env -i redis-cli rpush "resque:gitlab:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$repo_path\",\"$oldrev\",\"$newrev\",\"$refname\",\"$GL_USER\"]}" > /dev/null 2>&1
if [ -n "$recipients" ]; then # validate repo email notify information
generate_email $oldrev $newrev $refname | send_mail
fi
done
fi
然后將這個腳本替換Gitlab默認的post-receive . 以后的項目只需要添加YOUR_REPO_PATH/config
的hooks
郵件信息就可以觸發。
##############################################
http://huangx.in/3/for-git-add-a-submit-message-notification