All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

PHP Build Systems

3 August 2007

Increasingly, I’m finding that the applications I build are becoming more complex with more component parts, often spread across frontend and backend. Particularly with user interfaces becoming more advanced, the number of CSS and JavaScript files that need to be wrangled seems to be increasing, and with it I’m becoming more and more paranoid about frontend performance. This typically involves doing stuff like concatenating a bunch of small files into one, minifying JavaScript and so forth.

Up until now I’ve been doing this by hand. As with anything of this nature you have to do by hand, it’s far too easy to forget steps, to deviate from a standard process, and really it’s just plain boring and takes more time than it should. In short, I need a proper build system.

I could, of course, just write myself a command line PHP script to do what I need, but as I figure that this is not only a common problem but also one at which others have more experience, it’d be worth looking at existing build systems and seeing what I can learn from them.

The basic things I need to do can probably be boiled down to two or three main features. Firstly, I need to be able to export a version of the code from SVN (ideally by tag) to somewhere on the file system. Secondly I need to make some config changes (like switching dev/prod environment flags). Lastly, I need to be able run scripts across the build to do things like minify JavaScript, concatenate CSS, and perhaps run things like r3 to translate the site into multiple languages.

If it were to go a step further and offer the ability to scp the build up to a staging server, that would also be worth looking at. Another nice-to-have would be the ability to take a mysqldump of the structure of a number of databases and flag if there have been any schema changes since the last build that need to be manually addressed.

All this sounds fairly easily scriptable, but as I say, using an established build system might bring me advantages I’d never even think of myself, but that could save me time. I’ve had exposure to Ant before now but don’t really know it, and I’m aware of the Capistrano project from the Rails crowd (which I understand works well for more than just Rails apps).

What I’m really looking for is recommendations and war stories. What have you used? Would you recommend it? Do build systems require so much configuration that I might as well just script this (probably fairly simple) task myself? My projects are in PHP, but the build system could be anything, so I’d value any input.

- Drew McLellan

Comments

  1. § Angus McIntyre:

    I use Ant. For most of my sites, I author as XML and then use XSLT to turn the XML into (X)HTML. I have Ant build scripts to kick off the XSLT processing (I use a custom processor that does more than the basic Ant ‘style’ task), copy needed files into the right place, concatenate CSS files and so forth. For putting files on the server, I actually use NoLobe’s Interarchy (a Mac application) which can do ‘mirroring’, uploading only the changed files to the server; Interarchy can be kicked off by Ant using the ‘exec’ task.

    My system is a bit idiosyncratic, but it works, and Ant certainly offers many of the features you’d want for controlling your builds. It’s probably worth spending a little time playing with Ant to see if it will do what you want.

  2. § Pelle:

    In one book I’ve bought they recommends Phing which more or less seems to be Ant in PHP. As I’ve understood it should be able to do many of the things you want.

  3. § Nate Klaiber:

    The previous system I worked with I used scripts, simply because I had to build conversion scripts to get it from the ERP system to the web system. I had the main conversion script, and then I built one to concatenate the different CSS files based on our build.

    I usually use default.css and @import for reset, layout, color, and typography. All of these need to be combined and extra whitespace removed before being placed on the live server. This took place whenever I made changes or forced the changes, as it would write to a cached file.

    As for JavaScript, I never got around to building a script for that – I just used available services out there to minify what I had and combine them.

    So, I might not be much help – but I have found that if it is a relatively simple process, then its pretty easy to build a script to suit your needs.

  4. § Patrick Fitzgerald:

    GNUMake and Perl have met my needs for most build tasks (or you could probably just use Make and PHP).

    A bit off topic, but when I saw “php build systems” my first thought was an old technique I used to create multiple HTML template files (which had to be static HTML, no PHP or server-side includes). I still wanted to use PHP because the files each had to have the same headers / footers / navigation. So I wrote the pages on a localhost webserver using PHP, then I used a build script to wget the file from the local webserver. I probably could have used PHP directly to run the files, but as I recall I needed some of the PHP variables that the web server set up (plus I needed to have the web server to preview the PHP files anyway, so why not use it to do the work as well).

    Using this technique, you could even use PHP to write your CSS files, then the build system can turn them into static CSS files.

  5. § Noah Slater:

    For about a year now we have been using Debian packages to manage the build process of our application.

    The application is packaged as a Debian .deb package and various things (such as compressing files) is done at build time – when it’s done you have a .deb file that you can scp and effectively “apt-get install myapp” which then invokes the maintainer scripts which can take care of anything you can imagine to do with installing/upgrading the environment – from stopping and starting services through to prompting for if you are in the staging/production environment.

  6. § Walker Hamilton:

    I use capistrano to deploy cakePHP applications.

    I’m using it to deploy apps to mediatemple’s gridserver: http://walkerhamilton.com/451/capin-cake

    I’m also using it to deploy new versions of digital web (a site built on nothing much other than Smarty): http://www.digital-web.com/news/2007/03/digital_web_deployment/

  7. § biou:

    we are using makefile to effectively build a php application, and then we deploy it through debian package. If you need simple solutions perhaps writing php scripts for the command line is an option…

  8. § Matt Patterson:

    I’m using Capistrano a lot at work, and Cap 2 is a lovely thing.

    If what you need to do is ssh into a remote machine, check out some stuff from svn/cvs/whatever and then put it somewhere (maybe symlinking and running tests, or DB scripts, or whatever) then Capistrano may well be exactly what you’re looking for…

  9. § Peter:

    I also agree with Matt. I always use Capistrano and have to admit as well that it works perfectly.
    It really makes things a lot easier.

  10. § Jilles Oldenbeuving:

    With regards to your nice to have “database schema changes”, you need to be careful and decide what you really want:

    1 – a way to signal that before/during deployment schema changes to the target have to be made in order to keep the app working
    2 – a way to version control the schema
    3 – a way to automatically keep the application and db schema in synch across multiple (dev/staging/prod) environments

    all these three require different solutions, that may build on top of the others. Ping me if you’d like to have more info.

    Jilles

Photographs

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Follow me

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

I made

Perch - a really little cms

About Drew McLellan

Photo of Drew McLellan

Drew McLellan (@drewm) has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Cadburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.