Maybe you're interested About this site or in some of my Projects or Articles. You might even be interested About me or My Friends. If all else fails head back Home.

Morethanseven is where plays with the web

How to deploy PHP sites with the Pake build tool

January 7th, 2008

So in case you hadn’t guessed project automation is the new black. I’ve been getting back into some development work recently with Is it Birthday? and getjobsin and trying to automate as much of the repetitive and boring work as possible.

I’m not absolutely sure that that many people outside large or particularly organised teams realise that large web sites are not generally deployed by someone with an FTP client and crossed fingers. This sort of effort, along with other repetitive tasks like running tests or generating documentation, can be automated. This is a win-win for everyone. It’s more reliable (scripts don’t forget to restart a service before they go home), quicker and removes the need for having one person in charge of deployments.

Even where people know about this build process idea they might not use it for their projects, probably for similar reasons why not everyone uses source control. I think the reason is probably that most web designers and developers (even particularly geeky ones) thing this software engineering stuff is maybe a step too far. Their is also something of a barrier to entry, knowing where to look and how to get started without reading lots of documentation (often filled with XML examples) and trial and error. Also project automation is apparently not sexy?

Anyway, if you’ve been working with Rails you will have come across Rake, which is a build tool used to automate various tasks. Well the nice symfony people have written a PHP version called Pake for use as part of the framework. It’s used for all the command line action, from running tests to clearing the cache and automating deployments. Pake is however a separate tool that you can use in your own projects, whatever framework or hand rolled codebase you are using.

Pake can be downloaded using PEAR on the command line:

pear upgrade PEAR
pear channel-discover pear.symfony-project.com
pear install symfony/pake

The documentation for Pake is pretty much non-existent as far as I can tell, but it is a really handy tool so worth a little effort. The best source of knowledge is to look through the default Pake tasks that are provided as part of symfony. One of my favourites, which we’ll look at now, allows for incremental deployments via Rsync over SSH. I’ve been using this with non-symfony projects too.

Rsync is a command line tool for syncronising two file structures. The Rsync command that does most of the heavy lifting for the deployment looks like the following. Note I’ve used {} to denote placeholders in the following examples.

rsync --progress --azC --force \
--delete -e ssh ./ {user}@{host}:{dir}

The sync task I’m using is straight from symfony, but the licence allows for distribution so here is an example zip of all the files needed to follow along. You’ll need these to follow along as I haven’t printed the full sourcecode for the pakefile here.

First a little configuration. Using YAML we define an environment, staging in this case_ and specify a host, port, user and the full path on the remote server. You can of course specify multiple environments in this file, we’ll see how to use them shortly.

[staging]
  host={host}
  port=22
  user={user}
  dir={dir}

You can also include an _rsync_exclude.txt_ or an _rsync_include.txt_ file. This gives you control over the files being synced when you run the Pake task. The following example is a good starting point, it stops you pushing those pesky .DS_Store files that OSX creates to you web server, as well as avoiding subversion metadata and the configuration files for the Pake task.

.svn
.DS_Store
/config/properties.ini
/config/rsync_exclude.txt
/config/rsync_include.txt
/config/rsync.txt

We can now run the following command, from the directory containing the pakefile.php script, using Pake. The first example will do a dry run, showing you what will happen. You’ll be prompted for your SSH password as part of the command unless you’re using keys for authentication.

pake sync staging

When you’re happy you can run the sync command with the go option which will instruct Rsync to do it’s thing.

pake sync staging go

Pake has a handy flag to find out what tasks are available.

pake -T

This should give you a list of tasks and a brief description, useful to find out what you can do if you’ve been away from a project for a while.

This is a pretty simple example but one I’m already finding useful. Rsync is but one way of deploying apps but with Pake has the advantage of being simple and in lots of situations good enough. It’s certainly better than a manual deployment process. It would be simple enough to build into the task a simple logging system so you have a log of all deployments; when they happened and who did them for instance.

If that has whet your appetite then their are other deployment tools you might want to look into; Capistrano (Ruby), Ant (Java), Maven (Java) and Phing (PHP) spring to mind. If anyone knows of a Python equivalent that would be useful too? I’m also using Phing for a few tasks on projects at the moment, mainly for some nifty Subversion tasks (and you can use Phing with Pake as symfony does), but that will have to wait until another post.

So, what are peoples experiences of build tools? Any good pointers? Or maybe reasons why you don’t use them in your projects?

Popularity: 16%

Tagged , , , , , , , ,

8 Responses to “How to deploy PHP sites with the Pake build tool”

  1. My head hurts… ;)

    I’d really like to get a decent deployment solution working for me, but until someone writes a tutorial that doesn’t assume I have a clue what I’m doing, I’m stuck with manual http://FTP. At least Aptana has a nice Synchronize tool so I can upload from within a project.

  2. At Multimap we manage this stuff with shell scripts at the moment. Build scripts to concatenate, minify, gzip JS/CSS – rename files and their references for aggressive expires headers – labeling builds in Perforce, etc…). And release scripts that rsync named builds across our production servers around the world.

    It’s flexible and works well once it’s in place, but does take some overhead in setting up and maintaining as things change. We’ve talked about taking a look at Ant in the future for some of this stuff. Nice round up of the options by the way – good to see people talking about this stuff.

  3. @Matt I know what you mean! I probably assumed a few things in their and wizzed over a few steps a little to quickly for a real tutorial. I plan on trying to put together a good resource for this sort of thing this year hopefully; maybe along the lines of the excellent highscalability. Let me know if that sounds interesting to anyone.

    @Andy Ant is pretty powerful but getting everything set up and having a few people understand it is something of a chore. I also missed off NANT which is a .NET tool modelled on Ant.

    These comments play out according to plan so far; larger organisations will have a, often custom, build process in place with most smaller teams or individuals finding the barrier to entry way too high.

  4. @Gareth – Why on earth would you think we’d be using .NET at Multimap? Ahem… ;)

  5. [...] How to deploy PHP sites with the Pake build tool [...]

  6. Has anyone checked out Phing? I’ve seen it being used on several projects and wondered how it stacked up to Pake.

  7. @Andy It took me an entire week to get that!

    @Micah I should have a follow up or two with some Phing info. Within symfony Pake actually calls a number of Phing tasks for some of it’s functionality. Phing is great, especially if you have some Ant experience but want something that’s a little easier to install if you already use PHP.

  8. [...] How to deploy PHP sites with the Pake build tool [...]

Leave a Reply