Taking the pain out of CakePHP deployment: Batch Scripts and SVN

If you take a look at the update rate of this blog, or the wait time on my email responses, or even the changelogs from my Subversion repository, you will notice one thing — I am a horrible procrastinator.

Well, not actually true. I’m a great procrastinator.

I found out a long time ago that something I think will take 10 minutes ends up taking an hour, and things I think will take an hour end up taking 10 minutes. The downside to this thought pattern is that it makes it hard for me to start anything I know that I can’t get done in 1 minute, unless I have a lot of time to kill on it.

The more steps there are to a process, the less likely I am to feel that I can get it done in the small amount of time I have allowed.

What is the process for deploying a project with CakePHP?

The process for deploying a cakePHP website is monotonous enough, but at the same time contains enough fiddly bits to be annoying to do more than once in a blue moon.

  1. Export the data from the SVN to the directory of your choice (dev or live in my case)
  2. If you’re anal about keeping logs of what you exported (like me), write the version info to a file.
  3. Update the config file so that your local version (which is probably running in debug 1 or 2) runs without debug info.
  4. Remove all the old cache data in case you made any changes to your models
  5. Done

Kind of a pain in the rear.

And we all know what are great for repetitive tasks with lots of fiddly steps that are a pain in the rear:

Enter the bash file

I created a simple batch file that I can run directly on my SliceHost server. It allows me to deploy a specific folder from my SVN, pick whether to deploy to the dev site or the live site, clear the cache, and set the debug settings.

Now let’s do it in one line

./deploy live app

Done.

Here’s what comes out the other end:

Setting LIVE
.....
Lots of SVN Export Data
.....
Reset Config File: [livesite]/app/config/core.php
Cleared out Cache

This gives me a sense of security when updating my site, because I know that I won’t have forgotten any of the tiny repetitive details, and can fill my deploy checklist with more important things (such as “Check that users can login”) instead of “Set Debug to 0.”

Here’s the Code

I know, I know. All you want is the code. Well here you go. Feel free to edit and use as you like, but be sure to set your svn and target paths.

#!/bin/sh
# usage: ./deploy TYPE DIR
# TYPE = live | (anything else)
#
# Be sure to edit the following:
# _MY_SVN_DIR_ -- the base SVN dir for this project
# _MY_LIVE_PATH_ ex: /home/ippatsu/japanesetesting.com/public
# _MY_DEV_PATH_ ex: /home/ippatsu/beta.japanesetesting.com/public

# Set as Life or Dev
if [ "$1" != "live" ]; then
echo "Setting DEVsite"
TARGETP="_MY_DEV_PATH_"
else
echo "Setting LIVE"
TARGETP="_MY_LIVE_PATH_"
fi

# Export Data
svn export --force svn://127.0.0.1/_MY_SVN_DIR_/$2  $TARGETP/$2

# Write Version to File
svn info svn://127.0.0.1/_MY_SVN_DIR_/$2 > $TARGETP/version.txt
svn info svn://127.0.0.1/_MY_SVN_DIR_/$2

# Update Config File
OLD="Configure::write('debug', 2);"
NEW="Configure::write('debug', 0);"
CONFIGFILE="$TARGETP/app/config/core.php"

sed -i "s/$OLD/$NEW/g" $CONFIGFILE
echo "Reset Config File: $CONFIGFILE"

# Clearout Cake Model Cache
rm $TARGETP/app/tmp/cache/models/cake*
rm $TARGETP/app/tmp/cache/persistent/cake*
rm $TARGETP/app/tmp/cache/views/cake*
echo "Cleared out Cache"

exit 0

Hope you enjoy it, and if you have any advice or questions, just leave them in the comments!