Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://qicheng0211.blog.51cto.com/3958621/1563159
SVN's post-commit hook script runs after each commit (commit), and we can implement some useful features in this script, such as sending email reminders, automatically backing up repositories, automatically synchronizing code to Web servers, and more.
Here with Post-commit to achieve multi-project automatic synchronization, thinking: In the SVN version of the repository root directory to divide multiple project folders, project team members when the file is submitted, Post-commit automatically determine the project to which the file belongs, and then sync to the appropriate Web server.
Test environment configuration:
SVN server: 172.16.4.234
Project 1WEB Server: 172.16.4.235
Item 1 Code Base: Svn://172.16.4.234/project1
Project 2WEB Server: 172.16.4.236
Item 2 code base: Svn://172.16.4.234/project2
Here are the steps to proceed:
One, the Web server
The Web server acts as an SVN client, checkout a copy of the code from the SVN server to local. Note To switch to WWW user checkout, because WWW is an Apache or nginx execution User (your environment may not be the same).
Project 1WEB Server, the site root directory is/data/apps/project1:
123 |
shell# su wwwshell$ cd/data/apps/shell$ svn co--username zb--password 123456 Svn://172.16.4.234/project1 |
Project 2WEB Server, the site root directory is/data/apps/project2:
123 |
shell# su wwwshell$ cd/data/apps/shell$ svn co--username zb--password 123456 svn://172.16.4.234/project2 |
Second, configure SSH password-free access
SVN server requires no password SSH access to the Web server, convenient post-commit script ssh to the Web server to perform SVN up. Note here that the WWW user is not password-accessible because the code on the Web server is checkout by the WWW user.
Execute ssh-keygen-t RSA on the SVN server, then press ENTER all the time, generate the public key and the private key are saved in/root/.ssh/.
Then on the Web server first switch to the WWW user, establish the Authorized_keys file:
12345 |
[[email protected] ~]# su www[[email protected] root]$ cd[[email protected] ~]$ mkdir. Ssh[[email protected] ~]$ chmod 755 . Ssh/[[email protected] ~]$ vi. Ssh/authorized_keys |
Copy the contents of the/root/.ssh/id_rsa.pub public key file on the SVN server to the Authorized_keys file. Then set the Authorized_keys file permission to 600.
1 |
[Email protected] ~]$ chmod Ssh/authorized_keys |
Test, SVN server login Project 1WEB server:
12 |
[[email protected] ~]# ssh [email protected][[email protected] ~]$ |
Third, SVN server post-commit
Post-commit is under the hooks directory of SVN.
Post-commit Script content:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
#!/bin/sh repos= "$" # Warehouse Path rev= "$" # newly submitted version number logfile=/var/log/svn.log # Hook script log # the standard output and standard error output of the script are printed to the log file exec 1>> "$LOGFILE" exec 2>&1 svnlook=/usr/bin/svnlooktime=$ (date "+%y-%m-%d %h:%m:%s") author=$ ($SVNLOOK author -r $REV $REPOS) #提交作者CHANGEDDIRS =$ ($SVNLOOK dirs-changed $REPOS) #修改的目录集合MESSAGE =$ ($SVNLOOK log -r $REV "$REPOS") #提交时的备注信息, not recommended in Chinese # SVN client configuration, you need to modify it yourself **********************************client1=172.16.4.235 #project1的服务器CLIENT2 =172.16.4.236 # Project2 Server Clientsvnroot=/data/apps #WEB服务器的代码根目录SVNUSER = "ZB" svnpasswd= "123456" #********************************************** function myecho () { echo "$TIME" "$*"} myecho "**************************************************************" myecho "submitted version: $REV $ AUTHOR "myecho " Submission Note: $MESSAGE "myecho " Directory: $ (echo $CHANGEDDIRS | tr ' \ n ' ' ') " masterdir=$ (echo " $CHANGEDDIRS " | head -1) #CHANGEDDIRS里的最上级目录 # Traverse the submitted code directory, sync to the Web server while [ "$CHANGEDDIRS" != " ];d o project=$ (echo $MASTERDIR | awk -F / ' {print $1} ') # Judging the project folder if [ $PROJECT == Project1 ];then myecho myecho "Project: $ project Synchronization Directory: $MASTERdir " myecho " sync $MASTERDIR to $CLIENT 1:$ clientsvnroot/$MASTERDIR " # SSH connection to client server without password, execute svn up /usr/bin/ssh [email protected] $CLIENT 1 "Export lang=en_us. utf-8; svn up --non-interactive --username $SVNUSER --password $SVNPASSWD ' $CLIENTSVNROOT/$MASTERDIR ' " elif [ $PROJECT" == "Project2" ];then myecho myecho "Project: $PROJECT Sync directory: $MASTERDIR" myecho "Sync $MASTERDIR to $CLIENT 2: $CLIENTSVNROOT/$MASTERDIR" /usr/bin/ssh [email protected] $CLIENT 2 "Export lang=en_us. utf-8; svn up --non-interactive --username $SVNUSER --password $SVNPASSWD ' $CLIENTSVNROOT/$ Masterdir ' " else : fi # Delete subdirectories in catalog collection changeddirs=$ (echo "$ Changeddirs " | grep -v " ^ $MASTERDIR ") # get new parent directory to synchronize masterdir=$ (echo "$CHANGEDDIRS" | head -1) Done |
Don't forget to give Post-commit executable permissions.
Iv. Testing
In the "Project1/client" and "Project1/server" directories in Project 1, upload a "new text document. txt" separately to view the log file/var/log/svn.log:
The log shows that the file has been synced to the project 1WEB server.
This article is from the "Start Linux blog" blog, make sure to keep this source http://qicheng0211.blog.51cto.com/3958621/1563159
Multi-Project Auto-synchronization using SVN's Post-commit hooks