Installing Hudson, Phing, PHPUnit and Git on Ubuntu

In this post, I'm just trying to get the simplest setup. I'll try to post details in a subsequent post.

Here are three posts, in descending order, on this that I found useful :

http://toptopic.wordpress.com/2009/02/26/php-and-hudson/

http://blog.jepamedia.org/2009/10/28/continuous-integration-for-php-with...

http://www.davegardner.me.uk/blog/2009/11/09/continuous-integration-for-...

First, I had to install java :

    $ sudo aptitude install sun-java6-jre  

Next, install Hudson on Ubuntu per these instructions (http://hudson-ci.org/debian/). I guess you can just download a .war file for Hudson and start with "java -jar hudson.war" but I wanted a service that would start automatically on boot, so I installed from packages.

Now install pear, phing, and PHPUnit :


    $ sudo aptitude install php-pear
    $ sudo pear channel-discover pear.phing.info
    $ sudo pear install phing/phing
    $ sudo pear channel-discover pear.phpunit.de
    $ sudo sudo pear upgrade-all                 (needed for my older Ubuntu)
    $ pear install phpunit/PHPUnit

I assume you have Git installed. If not, check some of my posts on Git.

Now create a simple class and a test class. Here's the test class Calculator.php :


class Calculator
{
    /**
     * @assert (0, 0) == 0
     * @assert (0, 1) == 1
     * @assert (1, 0) == 1
     * @assert (1, 1) == 2
     */
    public function add($a, $b)
    {
        return $a + $b;
    }
}

Note the asserts will automatically give us test cases witht the skeleton test generator in PHPUnit 3.4+. Now separate the class and test class into separate directories. We'll put the build.xml into the src directory as well and commit to Git.


$ phpunit --skeleton-test Calculator
$ mv Calculator.php src
$ mv CalculatorTest.php test
$ touch src/build.xml
$ git init
$ git add .
$ git commit -m "Initial commit"


Now pull up Hudson in a browser on the default port 8080. Install the following plugins with Manage Hudson => Manage Plugins => Available.

  • Git
  • xUnit
  • Phing
  • ChuckNorris (yeah!)

Now create the following build.xml in your src directory :


<?xml version="1.0" encoding="UTF-8"?>
<project name="start_page" basedir="." default="main">
<property name="tmp" value="/tmp" />
<property name="wsname" value="src" />
<property name="package"  value="${phing.project.name}" override="true" />
<property name="builddir" value="${tmp}/build/${phing.project.name}" override="true" />
<property name="srcdir"   value="./src/" override="true" />

<!-- Main Target -->
<target name="main" description="main target">

  <!-- Create dirs -->
  <mkdir dir="${builddir}/reports"/>
  <mkdir dir="${builddir}/reports/coverage"/>

  <!-- PHPUnit -->
  <exec command="phpunit --log-xml ${builddir}/reports/phpunit.xml  tests/CalculatorTest.php"/>

</target>
</project>

Now pull up Hudson and create a new project :

Project Name : PhpCalc - Freestyle software project

Git URL : /home/me/projects/PhpCalc

Build => Execute Shell :
phing -f $WORKSPACE/source/build.xml -Dws=$WORKSPACE -Dtmp=$WORKSPACE

Test report XMLs = build/start_page/reports/phpunit.xml

Now hit Build Now in Hudson's browser. If you get errors, look at the "Console" output to debug the issue. You'll need to make sure Git is initialized (name/email) for user hudson :

$ sudo su - hudson
$ cd /var/lib/hudson/jobs/PhpCalc/workspace
$ git config --global user.email "you@example.com"
$ git config --global user.name "Your Name"
$ exit

When you make your builds, note changes won't propagate to the build until you commit them in Git (doh!).

Addendum :

Here are some additional posts that give some good details on how to setup a Continuous Integration server for PHP :

Getting PHPUnit results

If you're having trouble getting PHPUnit results to be found, you'll need to enable the xUnit plugin for Hudson. In other words, DO NOT configure "Publish JUnit test result". Instead, configure "Publish testing tools result". The latter plugin transforms PHPUnit's log format into a format more compatible for reporting under Hudson.

http://stackoverflow.com/questions/518083/how-might-i-integrate-phpunit-...

http://blog.uncommons.org/2009/03/25/using-phpunit-with-hudson/

There is 1 Comment