SVN1.7.14上配置post_commit hooks,實現自動發郵件功能

操作系統是centos7. SVN1.7.16 的post-commit.tmpl中提供的mailer.cf文件需要安裝python,很麻煩。所以仍舊使用sendmail 發送郵件

1. 使用sendmail,安裝sendmail服務
#yum install sendmail


2. 修改/etc/mail/sendmail.mc
修改DAEMON_OPTIONS(`Port=smtp,Addr=192.168.99.35, Name=MTA')dnl 爲本機ip

3. 修改/etc/mail.rc 增加內容

           set [email protected] smtp=smtp.126.com smtp-auth-user=xxx smtp-auth-password=123456 smtp-auth=login

4. 增加svn_email_commit.sh發送郵件腳本,內容如下:

#!/bin/bash
REPOS=$1
REV=$2
SENDTO=$3
[email protected]
 
LIMITDIFF=200
CHANGELOG=`svnlook log -r $REV $REPOS`
AUTHOR=`svnlook author -r $REV $REPOS`
CHANGED=`svnlook changed -r $REV $REPOS`
DIFF=`svnlook diff -r $REV $REPOS | head --lines=$LIMITDIFF`
DATE=`date`
TMPFILE=/tmp/svn$REV-$RANDOM.message
 
SUBJECT="SVNCommit ($AUTHOR) $REPOS [$REV]"
echo "-------------------- SVN Commit Notification --------------------
Repository: $REPOS
Revision:   $REV
Author:     $AUTHOR
Date:       $DATE
     
-----------------------------------------------------------------
Log Message:
-----------------------------------------------------------------
$CHANGELOG
 
-----------------------------------------------------------------
Changes:
-----------------------------------------------------------------
$CHANGED
 
-----------------------------------------------------------------
Diff: (only first $LIMITDIFF lines shown)
-----------------------------------------------------------------
$DIFF
" > $TMPFILE
# Send email
#cat $TMPFILE | mail -a "From: $SENDFROM" -s "$SUBJECT" "$SENDTO"
cat $TMPFILE | mail -a "$TMPFILE" -s "$SUBJECT" "$SENDTO"
 
# Cleanup
rm $TMPFILE


5. 修改post-commit腳本

#!/bin/sh
export LC_CTYPE=zh_CN.UTF-8

export PATH=$PATH:/usr/local/svn/bin:/opt/svnrepo/upublish/hooks:
REPOS="$1"
REV="$2"

SENDTO="[email protected]"
# Send it to these people, calling the script we created above
/opt/svnrepo/upublish/hooks/svn_email_commit.sh "$REPOS" "$REV" "$SENDTO"


6. 自動sendmail服務

#systemctl enable sendmail.service
#systemctl start sendmail.service

注意,如果sendmail服務啓動很慢,很可能是因爲解析本機的域名慢,需要修改/etc/hosts文件。
添加: 192.168.99.35  svnhost. svnhost
(主機名後面一定要加“."啊)


7. 可以測試

mail -s "test" [email protected]



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章