User Level: Intermediate
This article assumes that you already have the following installed and working:-
* Linux
* Subversion (SVN)
* A SVN Repository
* GCC (To compile your C scripts)
* Vi (or another text editor)
You will also need shell access to your server to perform the following functions, either you are directly on the machine or you are using a utility like Putty to access your server.
The idea of this post is to allow you to automatically update your files in a public html directory after you commit to your SVN repository, you may want to create a subdomain like preview.domain-name.com or sandbox.domain-name.com. You may deploy a particular folder from your svn repository or the complete repository.
$ svn checkout /full/path/to/your/svn/db /full/path/to/your/public/html/directory
To begin this process, login to your server and checkout the SVN repository (svn checkout) to your public html directory, some servers the public html directory will be "/var/www/html", "/var/www/public_html" or "/var/www/vhosts/domain-name.com/httpdocs". There are many different configurations so check where your public html directory is, you may also create a directory under your public html directory to check out the SVN directory to. You will need to remember the directory to which you have checked out your SVN database to for later. This article does assume you already have a SVN database set-up and working.
$ cd /full/svn/path/hooks
Change directory (cd) to your SVN database hooks sub-folder.
$ cp post-commit.tmpl post-commit
Copy (cp) the template file "post-commit.tmpl" to "post-commit" (without the quotation marks).
$ chmod +x post-commit
Change file system mode (chmod) of the "post-commit" file to execute (-x).
$ vi svnupdate.c
We need to write a little C program that will do the work. The command above will open up the vi editor to create the "svnupdate.c" file, you may use any text editor of your choice.
#include <stddef .h> #include <stdlib .h> #include <unistd .h> int main(void) { system("svn update /full/path/to/your/public/html/svn/directory"); } </unistd></stdlib></stddef>
The above script will execute the command to update the SVN repository in the public html directory we created earlier.
$ gcc -o svnupdate svnupdate.c
We need to compile our little C program with GCC. The above command will compile our "svnupdate.c" file and output our little program "svnupdate".
$ env - ./svnupdate
Lets test our little program, your SVN repository should update with the latest changes. If you receive any errors here then you either a) haven't specified the correct directory in the "svnupdate.c" script or b) you didn't checkout the SVN database properly. If you receive any errors, go back and fix them, there is no use continuing on from here.
$ chown root:root svnupdate
Change the owner (chown) of our compiled C program to root.
$ chmod +s svnupdate
Change the mode (chmod) of the "svnupdate" file.
$ vi post-commit
Now we need to edit our "post-commit" file to execute our "svnupdate" file.
/full/file/path/to/the/file/svnupdate
Add the above line to the "post-commit" file and save the file.
Now when you commit files from your SVN directory, the server will update the SVN repository in your public html folder. To protect your files, you may want to protect the public html directory with a login using a .htaccess file.
I'm happy for any suggestions or any corrections, so please don't forget to leave a comment below.