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

Automated SVN Deployment with PHP - Revisited

Quite some time ago I posted about automated web deployment from a subversion repository.  Due to one of the projects I'm working on, I needed to make some changes to this, and so I've updated the approach I use.

The script

#!/bin/bash
DATE=`date +%H-%M-%e-%m-%y`
mkdir /home/account/public_html/projectdoamin.co.uk/staging/$DATE/
svn export --quiet --username cron --password password http://svnserver.com/svn/repo/trunk /home/account/public_html/projectdoamin.co.uk/staging/$DATE/public/
svn export --quiet --username cron --password password http://svnserver.com/svn/repo/deploy/config.php /home/account/public_html/projectdoamin.co.uk/staging/$DATE/public/config.php
chmod -R 0755 /home/account/public_html/projectdoamin.co.uk/staging/$DATE/public/
chmod -R 0777 /home/account/public_html/projectdoamin.co.uk/staging/$DATE/public/uploads/
chgrp -R user /home/account/public_html/projectdoamin.co.uk/staging/$DATE/
chown -R user /home/account/public_html/projectdoamin.co.uk/staging/$DATE/
rm /home/account/public_html/projectdoamin.co.uk/public
ln -s /home/account/public_html/projectdoamin.co.uk/staging/$DATE/public /home/account/public_html/projectdoamin.co.uk/public

What it does

So, what does the script do?

  • Firstly, it sets a variable DATE which is the current date and time as HH-MM-DD-MM-YY, this is used so we can label the deployed folder with the date, and keep a record of a few different versions on the server if we needed to.
  • Next, it creates a folder in a staging directory, named with the date.
  • It then exports (an oversight from one of my previous scripts was it used checkout by mistake) the trunk folder from the repository to a public folder in our new staging/date/ folder
  • The database configuration for the live/staging/development servers are stored seperately, so it then exports the relevant configuration file into the sites folder for use
  • Next, it sets the permissions for the folder, sets the permissions on the uploads folder, and sets the owner of the files.
  • Finally, it removes the public folder/symlink (not the staging/date/public folder), and replaces it with a symbolic link to the staging code.

Some notes

A few notes on how I use this:

  • The config file is added to the svn:ignore list so it isn't extracted in the initial export (if you have it in the repo, you may need to delete it before exporting the staging version if it generates an error)
  • I also have it extract a number of other folders, private (non URL accessible downloads) and database patches so that any database structure changes since the last export can be applied
  • I also copy out user uploads from the previous version, and re-import them into the latest exported version
  • I also have a seperate version which accepts an svn version to export

One-click deployment

I set this script up to work with one-click deployment (see my earlier blog post on the subject), and found it didn't work initially. The reason for this, was svn was complaining about having to store the password. To solve this, if you have the same problem, simply edit the servers file:

sudo nano /var/www/.subversion/servers

At the bottom of the file, uncomment the line:

store-plaintext-passwords = no

Automated deployment

For automated deployment, simply run the script from a cron job every day / week. If you run it was a non-root user, remember to add the user to the sudoers file, as per my instructions in Sudo Shell Commands via PHP for One Click Deployment.

Posted by Michael on 24th Nov 2010 at 04:04

There are no comments on this post yet.

Add a comment