I'm a web developer, freelancer, author, speaker, entrepreneur, technical reviewer and blogger based in the North East, living in a village just outside of Chester-le-Street.

My blog

SVN Deployment Script

I've always loved the idea of a 'one click deployment' setup, and it is something I've been playing around with for a while now.

Up until recently, I had a script which would checkout a repository and move it to the public_html folder on the development server. Not a bad setup (it worked), but it is problematic as a deployment script onto a live or public-facing development server as there is time when it removes the old site, and copies in the new files when the site would be unavailable.

Since signing up with slicehost.com to see what they were like, I've got more into fiddling around with Linux, and finally, I've made a deployment script which I quite like.

So, what does this deployment script do?

  • Removes a copy of the website (this copy isn't there until this script has been run for the first time).
  • Copies the current deployment to another location (public2)
  • Updates the domains vhosts file to tell Apache the new location for the website.
  • Reloads Apache, so the new location is used for the site for now.
  • Checks out the repository to the old location of the site and renames any config files (this could be done better with svn ignore I believe).
  • Updates the vhosts file to point to the new location
  • Reloads apache

By doing it this way, while the project is being checked out and placed in the public folder, anyone accessing the website won't be disturbed, as Apache is temporarily using another location to serve the website from. Of course, this would have problems if the website/webapp makes use of full file paths (e.g. if handling user uploads), but for basic use is pretty good.

At the moment, I'm using this to checkout repositories for development projects, and place them into the public facing development server, so our clients can see work on their project as it happens. Currently, this is set to run as a cron job every 30 minutes, so it is automatically kept upto date you will notice that I've created a user 'cron' with access to the repository. I know this script isn't the best, so I'm open to suggestions for improvement!

#!/bin/bash
rm --force --recursive /home/pca/public_html/myproject.com/public2/
cp /home/pca/public_html/myproject.com/public/ /home/pca/public_html/myproject.com/public2/
sed 's|/home/pca/public_html/myproject.com/public|/home/pca/public_html/myproject.com/public2|g' /etc/apache2/sites-enabled/myproject.com > /etc/apache2/sites-enabled/myproject.com2
rm --force /etc/apache2/sites-enabled/myproject.com
mv /etc/apache2/sites-enabled/myproject.com2 /etc/apache2/sites-enabled/myproject.com
/etc/init.d/apache2 reload
rm --force --recursive /home/pca/public_html/myproject.com/public/
mkdir /home/pca/public_html/myproject.com/public/
svn checkout --username cron --password cronpassword http://dev.peacockcarter.co.uk/svn/project/trunk /home/pca/public_html/myproject.com/public/
chmod -R 0777 /home/pca/public_html/myproject.com/public/
chgrp -R pca /home/pca/public_html/myproject.com/public/
chown -R pca /home/pca/public_html/myproject.com/public/
mv /home/pca/public_html/myproject.com/public/config.php /home/pca/public_html/myproject.com/public/config.local.php
mv /home/pca/public_html/myproject.com/public/config.dev.php /home/pca/public_html/myproject.com/public/config.php
mv /home/pca/public_html/myproject.com/public/.htaccess /home/pca/public_html/myproject.com/public/local.htaccess
mv /home/pca/public_html/myproject.com/public/dev.htaccess /home/pca/public_html/myproject.com/public/.htaccess
mv /home/pca/public_html/myproject.com/public/toucan/.htaccess /home/pca/public_html/myproject.com/public/toucan/local.htaccess
mv /home/pca/public_html/myproject.com/public/toucan/dev.htaccess /home/pca/public_html/myproject.com/public/toucan/.htaccess
sed 's|/home/pca/public_html/myproject.com/public2|/home/pca/public_html/myproject.com/public|g' /etc/apache2/sites-enabled/myproject.com > /etc/apache2/sites-enabled/myproject.com2
rm --force /etc/apache2/sites-enabled/myproject.com
mv /etc/apache2/sites-enabled/myproject.com2 /etc/apache2/sites-enabled/myproject.com
/etc/init.d/apache2 reload

Posted by Michael on 11th Nov 2008 at 11:11

Comments

thanks very nice post. how could you handle the transcation happend at public 2 while you migrate public1. i think you need some sort of database synchronization from public2 to public1 after some time. what do you think

Posted by m El-kady on 5th June 2011 at 01:01

Add a comment