Deploy PHP Project From SVN Using Phing

16th November 2010 - 5 minutes read time

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:

  1. 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.

  1. <?xml version="1.0"?>
  2. <project name="build" default="main">
  4. <property name="exportdir" value="./svn_export/" />
  5. <property name="svnpath" value="/usr/bin/svn" />
  6. <property name="username" value="username" />
  7. <property name="password" value="password" />
  8. <property name="repo" value="" />
  9. <property name="revision" value="HEAD" />
  11. <target name="main">
  12. <phingcall target="svnexport" />
  13. </target>
  15. <target name="clean">
  16. <delete dir="${exportdir}"/>
  17. </target>
  19. <target name="svnexport" depends="clean">
  20. <svnexport
  21. repositoryurl="${repo}"
  22. todir="${exportdir}"
  23. svnpath="${svnpath}"
  24. username="${username}"
  25. password="${password}"
  26. nocache="true"
  27. revision="${revision}"
  28. />
  29. </target>
  30. </project>

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

  1. 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.

  1. 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.

  1. <target name="main">
  2. <echo>Extracting revision ${revision}</echo>
  3. <phingcall target="svnexport" />
  4. </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.

  1. <target name="main">
  2. <propertyprompt propertyName="revision" defaultValue="HEAD" promptText="Enter revision number" />
  3. <echo>Extracting revision ${revision}</echo>
  4. <phingcall target="svnexport" />
  5. </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.

  1. 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.



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 Weinraub (Fri, 03/30/2012 - 11:14)

Add new comment

The content of this field is kept private and will not be shown publicly.