Deploy PHP Project From SVN Using Phing

Automatic building with Phing makes deploying to a server nice and easy, and if you are using SVN as your source control system then you can easily deploy directly from your repository to your web server.

To allow Phing to utilise an SVN server you must first install the VersionControl_SVN pear library. Although this is in alpha release I have used it quite a bit with no issues. The only thing is that you will need to specify the version number to pear if you want to install it, like this:

pear install VersionControl_SVN-0.3.4

To get Phing to export from an SVN repository you can use the svnexport task. The following build file sets up some parameters (for use in this task), runs a target to delete the existing export and then runs a target that exports from the SVN repository we have set up.

<?xml version="1.0"?>
<project name="build" default="main">

    <property name="exportdir"  value="./svn_export/" />
    <property name="svnpath" value="/usr/bin/svn" />
    <property name="username" value="username" />
    <property name="password" value="password" />
    <property name="repo" value="http://www.yoursvnserver.com/yoursvnrepo" />
    <property name="revision" value="HEAD" />

    <target name="main">
        <phingcall target="svnexport" />  
    </target>

    <target name="clean">
        <delete dir="${exportdir}"/>
    </target>  

    <target name="svnexport" depends="clean">
        <svnexport
            repositoryurl="${repo}"
            todir="${exportdir}"
            svnpath="${svnpath}"
            username="${username}"
            password="${password}"
            nocache="true"
            revision="${revision}"
        />
    </target>
</project>

If you save this build file as svnexport.xml you can run it by using the following command:

phing -f svnexport.xml

Most of the parameters available for the svnexport task are self explanatory. The only one here that might not be immediately obvious is nocahce, which will cache the connection credentials between requests. Also, if you want to use an anonymous SVN server then just leave out the username and password parameters. There is documentation available on svnexport at the Phing site.

If you find (like I did) that this script exists and gives an error that talks about the svnexport task not existing then make sure your version of Phing is the most recent and that should solve the issue. This can be done by either running pear upgrade or pear upgrade phing.

For bonus points we can extract just the revision we need, rather than always defaulting with the HEAD or having to edit the build file every time we want to change it. This can be done by either passing a parameter to Phing at run time, or by using a propertyprompt task to ask the user for a revision number when the build file is run.

You can change the revision parameter at runtime by using the -D flag when calling Phing. The following will set the revision parameter to 2.

phing -Drevision=2 -f svnexport.xml

You can then add an echo statement to your build file so that it prints out the revision it has received from the command line.

    <target name="main">
        <echo>Extracting revision ${revision}</echo>   
        <phingcall target="svnexport" />  
    </target>

To use the propertyprompt task just add it somewhere above your svnexport task. In the example below I have altered my main target to add the propertyprompt task before anything else is run.

    <target name="main">
        <propertyprompt propertyName="revision" defaultValue="HEAD" promptText="Enter revision number" />
        <echo>Extracting revision ${revision}</echo>   
        <phingcall target="svnexport" />  
    </target>

When you run the build file as before, the script will stop with a prompt on the screen asking for the revision number you want to extract.

Enter revision number [HEAD] ?

Once you have your export you can run other targets to move it to an FTP server or do whatever you want with it, you can even clean up the export directory once you have completed the operation. It is also an idea to put a little file in your built application containing the revision information so you can check what revision was used to make the application.

Phing is also capable of using other version control systems like CVS and Git so if you don't use SVN you can always adapt the script to include the repository system of your choice.

Comments

Great article. very helpful. ;-)

But there has always been one thing that has always confused me about deploying a web app from a remote SVN server to a production server using Phing (or Ant, etc).

Phing needs to be installed on the production server. I SSH in and run the Phing targets, especially the `SvnExport` target, dumping to some export directory. As you note, additional Phing targets can then move this content directly into the web space (or to a directory symlinked to the web space), perhaps running unit-tests, cleaning up the export folder, performing db migrations, creating/setting some cache directories/permissions, etc.

But assuming that the build script (`build.xml`) is itself in source-control, then how does it get from the remote SVN to the production server where it can run? Must I make a distinct command-line `svn export` call just to pull over the build.xml?

Can you see the chicken-and-egg issue I am having?

Thanks and cheers,

David

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
13 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.