Staging / Development / Live SVN with Magento


Whether you are making £10 per hour, or £100,000 per hour - making adjustments to your live site is a practice best avoided. Whenever we start work on a new site, the first thing we do is set up an SVN repository and the relevant staging/dev domains. In this example, we are going to create a repository for "sonassi.com". You'll want to change the highlighted lines as necessary.
Install Subversion
First things first, you'll want SVN installed. For Debian/Ubuntu:apt-get install subversionFor CentOS/RedHat:
yum install subversion
Create the repository
Next we want to create the initial repository and perform the initial check-in.cd /tmp mkdir svntmp cd svntmp mkdir trunk branches tags branches/dev cd /home/sonassi mkdir svn cd svn svnadmin create sonassi.com cd /tmp/svntmp/ svn import . file:///home/sonassi/svn/sonassi.com -m "Initial check in and folder structure"
Install an SVN browser
We also like to install a nice web based SVN browser, available from https://websvn.tigris.org/cd /home/sonassi/subdomains/ wget -O svn-browser.tgz https://websvn.tigris.org/files/documents/1380/49056/websvn-2.3.3.tar.gz tar xvfz svn-browser.tgz rm svn-browser.tgz mv websvn-* svn-browserNow add the repository to the config
cp svn-browser/include/distconfig.php svn-browser/include/config.php nano svn-browser/include/config.phpAnd around line 79,
$config->addRepository('Sonassi.com Repository', 'file:///home/sonassi/svn/sonassi.com');Now we want to restrict access to this tool, IP address is fine for now
nano svn-browser/.htaccess order deny,allow deny from all allow from my.ip.addr.ess
Prepare for first check in
Now we need to exclude certain files from version control and include others.cd /home/sonassi/public_html/ svn co file:///home/sonassi/svn/sonassi.com/trunk .This is up to you, but we like to exclude the following from version control. We do this by launching the SVN property editor,
svn propedit svn:ignore /home/sonassi/public_html/
media LICENSE.html pear downloader lib includes shell cron.sh .htaccess js robots.txt cron.php var install.php index.php LICENSE.txt
svn status app/ | grep "^?" | awk '{print $2}' | xargs svn add svn status skin/ | grep "^?" | awk '{print $2}' | xargs svn addWe want to ignore
local.xml
svn revert app/etc/local.xml svn propedit svn:ignore app/etc/Then add
local.xml
and save the editor.
svn ci . -m "Initial file check in" rm -rf /tmp/svntmpCongratulations, you have now set up your first SVN repository. But it doesn't end here, you'll want to set up the staging site now.
Setting Up The Staging Site
You want the latest copy of the store database and files. We like to keep the staging site as a working copy of the trunk - as it allows quick updates without the complexity of merging/switching.cp -par /home/sonassi/public_html /home/sonassi/subdomains/staging rm -rf /home/sonassi/subdomains/staging/var/session/* /home/sonassi/subdomains/staging/var/cache/*Unless you have unlimited disk space, it is normally worthwhile to create a common directory for media (and any other big folders). You do not want to symlink it to your live site - otherwise, and accidental product delete or image-cache flush will cause problems.
cd /home/sonassi/subdomains mkdir common mv /home/sonassi/subdomains/staging/media /home/sonassi/subdomains/common/ ln -s /home/sonassi/subdomains/common/media /home/sonassi/subdomains/staging/mediaCreate your new MySQL database for your staging site and have your credentials to hand, then dump the current DB and import it into the new DB. You can do this in 1 command using pipe - but we do not advocate this for large sites (it can lock up your live store for a few minutes).
mysqldump -h db.sonassi.com -u username_live datbase_live --single-transaction -p > /home/sonassi/subdomains/staging/var/backups/db_latest.sql sed -i 's/www.sonassi.com/www.sonassi.com/g' /home/sonassi/subdomains/staging/var/backups/db_latest.sql mysql -h db.sonassi.com -u username_staging datbase_staging -p < /home/sonassi/subdomains/staging/var/backups/db_latest.sqlThen edit your
./app/etc/local.xml
file to reflect the new database details.
Now we want to restrict access to this site, IP address is fine for now
nano /home/sonassi/subdomains/staging/.htaccess order deny,allow deny from all allow from my.ip.addr.essThat's it, you now have a working copy of your trunk in a staging environment. You should use this site only for minor CSS changes, previews and pre-live modifications. You shouldn't use this site for any work that will take longer than 1 day to complete, otherwise it prevents other staging/live updates from being able to take place. You just edit files within as necessary, preview - when you are happy, just check in the changes, head to your live directory and update.
cd /home/sonassi/subdomains/staging/ svn ci . -m "I just changed style.css" cd /home/sonassi/public_html/ svn update .
Setting Up a Development Branch
For anything that is going to take some real time to complete (eg. building a new module), it is worth create a separate branch for it. Begin by cloning the site using the same process above for the staging site. Then you just SVN copy and switch the working copy to be your development branches.svn cp file:///home/sonassi/svn/sonassi.com/trunk file:///home/sonassi/svn/sonassi.com/branches/dev -m "Branching from trunk to branches/dev at r2" cd /home/sonassi/subdomains/dev svn switch file:///home/sonassi/svn/sonassi.com/branches/dev .Once you have finished with your work, add the new files to the repository, check in and feel free to delete the files and DB - branches can be considered disposable (to an extent), when the work is complete.
cd /home/sonassi/subdomains/dev svn add app/etc/modules/Sonassi_MyNewModule.xml app/code/community/Sonassi/MyNewModule svn ci . -m "I just added a Sonassi_MyNewModule"
Merge your branch into the trunk
Now that you have added your new module in a branch, you'll need to merge it into the trunk. Assuming you have no conflicts - it is quite a painless process. Change directory to your live site and run SVN merge to merge your development branch with your current directorycd /home/sonassi/public_html svn merge file:///home/sonassi/svn/sonassi.com/trunk file:///home/sonassi/svn/sonassi.com/branches/dev .Update the status of your live repository
svn status . | grep "^?" | awk '{print $2}' | xargs svn addThen finally, check your changes in
svn ci . -m "Merged branch dev to trunk at r6"