Application code deployment from SVN with fast and simple rollback

Most important thing in every new release is the rollback procedure. Once you discover issues in new version then you need to be able to swithc back to previous version until you work on a fix. I have developed shell script to handle the automated deployment process and which allows quick and simple rollbacks if there are no database changes that dont allow easy rollback. Main idea is to

  • download code from SVN, GIT or similar
  • copy all code to new folder
  • replace all configuration with environment related conf
  • use symlink to switch between code versions

Here is example script that handles the automated deployment for you 

#Script for downloading code from SVN and oackaging it ready for deploying to website
 #When ready for deployment the symlink to new htdocs folder must be done manually
#check out the code and remember version number
 cd /svn/project/trunk
 svn update
 svnresult=`svn update`
 echo $svnresult
 vers=`echo $svnresult |cut -d' ' -f3 |cut -d'.' -f1`
 while true; do
 read -p "Continue with version $vers? y/n " yn
 case $yn in
 [Yy]* ) break;;
 [Nn]* ) exit;;
 * ) echo "Please answer yes or no.";;
#check if copying will succeed 
if [ -d /storage/project/$vers/ ]; then
 echo /storage/project/$vers/ version exists already, cannot proceed
#copy files to be ready for deployment but skip subversion related .svn and other unneccesary files
 mkdir /storage/project/$vers/
 find . |egrep -v '.svn|settings/' |xargs -n1 -I {} cp --parents {} /storage/project/$vers/ >/dev/null 2>&1

#replace "public" development environment configuration with production conf
 rm -rf /storage/project/$vers/settings >/dev/null 2>&1
 ln -s /storage/project/resources/settings /storage/project/$vers/settings

#activate the new version
rm -rf /var/www/vhostname
ln -s /storage/project/$vers/ /var/www/vhostname
chown -R www-data:www-data /storage/project/$vers/

Now when you need to rollback then execute below command and you are back running old version with a blink of an eye

rm /var/www/vhostname;ln -s /storage/project/versionnumber/ /var/www/vhostname

Leave a Reply

Your email address will not be published.