配置前提:
1台开发电脑,我本地是windows10,(主要用来提交开发的网站代码)
1台git服务器,已安装好git环境。本案例所用服务器ip为192.168.32.131,centos7系统,
n台web服务器(下文都以web服务器统称)。本案例用了3台服务器,ip分别为192.168.32.128,192.168.32.129,192.168.32.130,centos7系统
达到目的:
在开发电脑上开发好网站,向git服务器push更新,如果提交的描述带有发布release_new_version(当然也可以自己配置)的字眼,就把代码更新发布到多台web服务器。
配置步骤:
1,看系统原来有没有rsync,如果有,先删除,以防以前版本太旧,配置不成功(web服务器上执行)。
$ yum remove rsync (注意不要在git所在服务器执行此操作,有可能会删除git,因为git依赖了rsync)
2,安装同步工具rsync(git服务器和web服务器上都要执行此步)
$ wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
$ tar -zxvf rsync-3.1.2.tar.gz
$ cd rsync-3.1.2
$ ./configure --prefix=/usr/local/rsync
$ make
$ make install
$ ln -s/usr/local/rsync/bin/rsync /usr/local/bin/rsync
3,编辑rsync启动所需要的配置文件(web服务器执行)
$ mkdir/usr/local/rsync/etc /usr/local/rsync/var
$ mkdir /usr/local/rsync/var/run /usr/local/rsync/var/log
$ cd /usr/local/rsync/etc
$ vim rsyncd.conf 内容如下
#global settings
pid file = /usr/local/rsync/var/run/rsyncd.pid
port = 873
lock file = /usr/local/rsync/var/run/rsyncd.lock
log file = /usr/local/rsync/var/log/rsync.log
gid = root
uid = root
#
##module settings
[www] #写模块,到时要在git所在服务器的同步配置中指定此模块名www
path = /home/wwwroot/advanced/
use chroot = no
max connections = 15
read only = no
list = no
ignore errors = yes
timeout = 120
secrets file = /usr/local/rsync/etc/rsyncd.secrets
hosts allow = 192.168.32.131 #限制ip,只有git仓库所在服务器才有同步权限
$ vim rsyncd.secrets 内容如下
rsync:123456
4,编辑服务脚本(web服务器上执行)
$ vim/etc/init.d/rsync 内容如下
#!/bin/bash
#this script for start|stop rsync daemon service
#date:2017/11/3
status1=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v'grep')
pidfile="/usr/local/rsync/var/run/rsyncd.pid"
start_rsync="/usr/local/rsync/bin/rsync --daemon--config=/usr/local/rsync/etc/rsyncd.conf"
function rsyncstart() {
if [ "${status1}X" =="X" ];then
rm -f $pidfile
${start_rsync}
status2=$(ps -ef | egrep"rsync --daemon.*rsyncd.conf" | grep -v 'grep')
if ["${status2}X" != "X"];then
echo "rsync servicestart.......OK"
fi
else
echo "rsync service isrunning !"
fi
}
function rsyncstop() {
if [ "${status1}X" !="X" ];then
kill -9 $(cat $pidfile)
status2=$(ps -ef | egrep"rsync --daemon.*rsyncd.conf" | grep -v 'grep')
if [ "${statusw2}X" =="X" ];then
echo "rsync servicestop.......OK"
fi
else
echo "rsync service is notrunning !"
fi
}
function rsyncstatus() {
if [ "${status1}X" !="X" ];then
echo "rsync service isrunning !"
else
echo "rsync service is notrunning !"
fi
}
function rsyncrestart() {
if [ "${status1}X" =="X" ];then
echo "rsync serviceis not running..."
rsyncstart
else
rsyncstop
rsyncstart
fi
}
case $1 in
"start")
rsyncstart
;;
"stop")
rsyncstop
;;
"status")
rsyncstatus
;;
"restart")
rsyncrestart
;;
*)
echo
echo"Usage: $0start|stop|restart|status"
echo
esac
5,启动rsync(web服务器上执行)
$ service rsync start
6,编辑同步密码文件(git服务器上执行),假设文件放在/home/git目录下,可以自己指定。
$ vim /home/git/rsync.passwd 内容如下(内容就是我们在以上步骤web服务器中配置的rsyncd.secrets密码部分的内容,一定要对应)
123456
7,进入网站代码所在git仓库钩子目录(如/home/git/repositories/advanced.git/hooks)(git服务器上执行)
$ cd /home/git/repositories/advanced.git/hooks
$ ls
$ cp post-update.sample post-update
$ vim post-update 内容如下
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
#此参数自己定,到时像发布时,commit–m这个参数
RELEASE_MSG="release_new_version";
LEN=`git log -1 | grep $RELEASE_MSG | wc -L`;
if [[ $LEN -gt 0 ]]; then
echo "发布代码到所有服务器";
GIT_REPO=$HOME/repositories/advanced.git
TMP_GIT_CLONE=$HOME/tmp/advanced
mkdir -p $TMP_GIT_CLONE
#从本机clone仓库到临时目录
git clone $GIT_REPO $TMP_GIT_CLONE
#进入到临时目录,并执行其中的deploy.sh
cd $TMP_GIT_CLONE && chmod +x deploy.sh && ./deploy.sh
echo 'deploying complete.'
#删除临时目录
rm -rf $TMP_GIT_CLONE
fi
8,创建deploy.sh脚本,放到代码的根目录,到时和代码一起提交到版本库(开发电脑上操作)。内容如下
#!/bin/bash
# current dir is the same as this script
# current user is git
# make sure /home/git/rsync.passwd is exist and has 600 permission ( for thersync password )
# need rsync 3.10+ support also server should be 3.10+
# 删除不需要发布的文件,这里根据自己的情况而定(由于此脚本执行的是上一步创建的临时目录,所以大胆删,同步完整个目录会被删除的)
rm -rf .git
rm -r .gitignore
rm -rf api/runtime/cache
rm -rf api/runtime/logs
rm -rf backend/runtime/cache
rm -rf backend/runtime/logs
rm -rf frontend/runtime/cache
rm -rf frontend/runtime/logs
#发布到3台web服务器(此处ip地址后的::www就是之前配置web服务器的synecd.conf里指定的模块,同步的时候,会自动同步到模块指定的目录)
rsync -av ./ rsync@192.168.32.128::www --chown=www:www --password-file=/home/git/rsync.passwd --exclude=deploy.sh
rsync -av ./ rsync@192.168.32.129::www --chown=www:www --password-file=/home/git/rsync.passwd --exclude=deploy.sh
rsync -av ./ rsync@192.168.32.130::www --chown=www:www --password-file=/home/git/rsync.passwd --exclude=deploy.sh
deploy.sh脚本所在目录如下图,在网站根目标。
9,提交deploy.sh到版本库,修改网站代码,提交测试(开发电脑上操作,要确定deploy.sh会被提交到版本库)
$ git add –all
$ git commit –am”release_new_version”(如果没有这个release_new_version参数,是不会同步的)
$ git push
结果如图,大功告成