Our ultimate aim is to automatically update the files in the specified Web directory on the server side after the local commit.
Implementation method:
Find the server-side SVN repository directory (directory name is repositories), this directory is installed VISUALSVN server settings, according to your installation settings to find repositories this directory, under this directory to find the project directory you created, Locate the hooks directory below the project directory and create a batch file named Post-commit.bat in this directory, as follows:
@echo off
"D:\VisualSVN server\bin\svn.exe" Update "D:\ Your Site Directory"--username XXXXX--password XXXXX
Set Svn.exe directory to your VISUALSVN server installation directory, set your user name and password.
Synchronization program idea: The user submits the program to the SVN,SVN trigger hooks, according to the different hooks processing, here uses is Post-commit, uses the Post-commit to the code to check out to the SVN server's local hard disk directory, Then sync to the remote Web server via rsync.
Hooks file Details
# Start-commit triggers a transaction before committing
# Pre-commit triggers transaction before commit completes
# Post-commit triggers transaction when commit is complete
# Pre-revprop-change triggers a transaction before the Version property is modified
# Post-revprop-change triggers a transaction after the version property has been modified
Scripts written with these names can be implemented in a variety of functions and are quite powerful.
2, Synchronous command rsync specific parameters to use
3. Programming ability in the language of the program Bash Python perl can be implemented
Post-commit Specific Implementation Details
Post-commit Script
#!/bin/sh
# -------------------------------------------------------------------------------
# Filename:post-commit
# Description:web server with synchronization code by SVN
# -------------------------------------------------------------------------------
#Version 1.0
#当用戶把代碼提交完成时, synchronize the latest changes in the code to the Web server, and note that the delete operation is not included.
#Set variable
Svn=/usr/bin/svn
web=/home/test_nokia/
Rsync=/usr/bin/rsync
Log=/tmp/rsync_test_nokia.log
webip= "192.168.0.23"
Export Lang=en_us. UTF-8
#update the code from the SVN
$SVN update $WEB--username user--password password
#如果前面的代码成功完成, the following code will continue to execute
If [$? = = 0]
Then
echo "" >> $LOG
echo ' Date ' >> $LOG
echo "##############################" >> $LOG
Chown-r nobody:nobody/home/test_nokia/
#同步代码从SVN服务器到WEB服务器 Notes:by the key
$RSYNC-vaztph--timeout=90--exclude-from=/home/svn/exclude.list $WEB [email protected]$WEBIP:/www/> > $LOG
Fi
The above is the specific post-commit procedure
Precautions:
1, be sure to define variables, mainly used by the path of the command. Because of the security concerns of SVN, there is no call to the system variables, if manual execution is not a problem, but SVN automatic execution will not be able to execute.
2, SVN Update before you must manually checkout a copy out, and here must add the user and password if only manual will be updated, but the same can not be automatic.
3, plus the judgment of the previous command, if the update is a problem, the program does not quit will continue to synchronize the code to the Web server, which will cause code problems
4, remember to set up the user, because rsync can synchronize file attributes, and our web server is generally not the root user, the user is not correct will cause the Web program does not work properly.
5, it is advisable to record the log, when the error can be quickly wrong
6, the last most critical data synchronization, rsync parameters must be clear, this will not say. Note Several scenarios:
The environment here is that the SVN server and the Web server are open
Define the SVN server as the destination server for the source server Web server
Scenario one, if the destination Web server is mixed, like only a Web static resource, user-submitted, automatically generated in a directory of the Web, it is recommended not to use –delete this parameter
This is the program above, the implementation of the source server to the destination server updates and additions, without deleting operations, the content of the Web server will be more than the source SVN server
Scene Two, the implementation of the image, that is, the destination Web server and the source SVN server the same data, svn any changes on the web, the need to –delete parameters
Scene three, do not need to synchronize some subdirectories, some directories may be cached temporary garbage directory, or a dedicated picture directory (not style or layout) to use exclude this parameter
Note: The use of this parameter does not have to write absolute path, as long as the directory name on the line AA represents the file aa/representative directory, the disadvantage is that if more than one subdirectory is the same name then these names will not be synchronized
It is recommended to use –exclude-from=/home/svn/exclude.list in the form of files can be easily added and deleted
Exclude.list
. svn/
. Ds_store
images/
SVN hooks can also be used to write a lot of programs to control SVN, such as before the code submitted to see if there is a log, whether there is a tab, there will be a space, whether there is not allowed to upload files, whether there are more than the size of the file and so on.
How to use SVN to automatically synchronize updates to a Web server