Upload
suren-rodrigo
View
1.326
Download
2
Embed Size (px)
Citation preview
1 http://surenrodrigo.blogspot.com
Continues Integration Server for PHP
In this article we’ll take a close look at how to setup a complete enterprise application development
environment for PHP. In my previous posts, I have justified the use of PHP in enterprise level application
development and also given details about advantage of using proper set of tools for enterprise level
application development.
Below diagram shows a typical development environment for an enterprise application. The diagram is
only for reference purposes and not to show you how a real enterprise application development
environment looks like, these environments can be very complex with multiple teams over different
countries working on the same application connected via a VPN. As I said before, the diagram is just for
reference and more importantly shows how normally a version control server and a continues
integration server will exists in an enterprise application development environment.
Source Code Repository
(SVN) Server
Continues Integration Server
Developer
workstation
Developer
workstation
Developer
workstation
Developer
workstationDeveloper
workstation
In a typical PHP enterprise application development environment, both the version control server and
the continues integration server will run on some flavor of Linux, however, for demonstration purposes
and to negate the common belief that windows cannot be used for PHP/MySQL development (I know
it’s a lame excuse ) I’ve decided to setup the complete environment on my Windows XP desktop.
2 http://surenrodrigo.blogspot.com
You’ll be surprise how well this works on windows. I’ve installed the same system in Windows 7 and it’s
also working perfectly. What’s even more surprising is that the setup process for this platform is almost
exactly the same for both Linux and Windows platforms.
One of the greatest things about PHP/MySQL based development is the availability of so many excellent
Free and Open Source tools that enable the developer to develop application based on their chosen
platform. All the tools I’m using below are running under Windows XP/Windows 7.
1. XAMPP (http://www.apachefriends.org/en/xampp.html)
XAMPP is one of the best (definitely the best in my opinion) integrated WAMPP (Windows,
Apache, MySQL, PHP, PERL) distribution that is free and contains all the tools that you need to
develop applications in PHP. Download and install xampp.
2. Hudson (http://hudson-ci.org/)
The Continues Integration tool of our choice, this works really great with PHP and is completely
written in Java. Hence, we’ll need JDK 5.x for Hudson to work. Complete guideline to install
Hudson on windows platform is given here (http://wiki.hudson-
ci.org/display/HUDSON/Installing+Hudson+as+a+Windows+service).
1. Download and Install the latest version of JDK.
2. Download and install the latest version of XAMPP from
http://www.apachefriends.org/en/xampp.html . By default the installation wizard will install
XAMPP on “C:\XAMPP” folder, for this article I’ll presume XAMPP is installed on “X:\XAMPP”
folder, where “X:\” is the drive of your preference.
3. Add “X:\xampp\php” to your “PATH” environment variable.
http://vlaurie.com/computers2/Articles/environment.htm will provide you more details on this
and it shouldn’t be a problem for you to set it up.
4. Download Hudson from
5. Run Hudson for the first time by entering the command at the command prompt
java -jar hudson.war
3 http://surenrodrigo.blogspot.com
6. Once the server starts, type http://localhost:8080 on a browser and you will see the Hudson,
home page. Now, follow the instructions given in http://wiki.hudson-
ci.org/display/HUDSON/Installing+Hudson+as+a+Windows+service to install Hudson as a service
in your environment, this will allow you to start Hudson when the computer is started.
7. At this point you should have a working Hudson installation on your computer. Now, we need to
install “Phing”, “Phing” is a build tool based on Apache Ant. Phing will ensure that your web
application is created in a structured way from your source code. In a compiled language such as
Java (only complied to byte codes) or C++ build tools such as Mavan or Make is responsible for
compiling and building the application from the source, but, in none compiled languages such as
PHP, build tools main responsibility is to ensure that the application is properly assembled and
all the pre installation (ex: copying necessary configuration files to specific locations) and post
installation tasks(ex: running unit and integration tests, setting up file permissions etc…) tasks
are done in a organized and a repetitive manner. In large applications, this is an essential tool to
have and will assure an error free deployment of the final application.
To install Phing (http://phing.info/trac/) we are using PEAR tool that comes bundled with
XAMPP distribution. In order to install Phing using Pear, issue the following commands on the
command prompt (CMD)
pear channel-discover pear.phing.info
pear install phing/phing
Below is the CMD output of the above commands,
4 http://surenrodrigo.blogspot.com
Your output may vary depending on your Pear version and Phing version, however, the basic
structure should be the same and the last line “install ok: channel://….” Should appear if
everything goes well.
8. Next, we’ll install PHPDocumentor, for those who are coming from Java background;
PHPDocumentor is exactly same as JDocs. PHPDocumentor will generate documentation from
your source code. It’s an invaluable tool in large scale application development which allows us
to document the APIs or Frameworks we may create in our application so other developers can
refer the documentation and exactly knows what our source code dose.
To install PHPDocumentor we are using Pear again. Issue the following command on the CMD
pear install PhpDocumentor
CMD output given below.
5 http://surenrodrigo.blogspot.com
9. PHP CPD (PHP Copy and Paste Detector - http://github.com/sebastianbergmann/phpcpd) is a
valuable tool that shows us duplicated code within our source repository. This helps us to take
design decision which may lead to refactoring certain functions to element code duplication
which will lead to an error free source code and will improve maintainability.
Issue the following command in CMD to install PHP CPD.
pear channel-discover pear.phpunit.de
pear install phpunit/phpcpd
CMD output is given below.
10. Now, let’s install one of the most important tools, PHPUnit (http://www.phpunit.de/). PHPUnit
is the equaling of JUnit in the PHP World. It’s comprehensive Unit test framework which allows
us to test our code and is absolutely necessary if you are planning to do any kind of enterprise
application development.
To install PHPUnit, issue the following commands in your CMD
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit
CMD output is given below.
6 http://surenrodrigo.blogspot.com
11. We need to now install XDebug. The following command should install xdebug into your system.
pecl install xdebug
However, sometimes this method doesn’t work. In that case please visit http://xdebug.org/find-binary.php and follow the instructions given. This is a sure way of installing xdebug into your system. XDebug is an invaluable tool and you must configure this properly if you are serious about doing any kind of enterprise level application development in PHP.
12. PHP Depend (http://www.pdepend.org/documentation/getting-started.html) and PHP Code
Sniffer (http://pear.php.net/package/PHP_CodeSniffer) are two important tools that can be
used heavily to improve the code quality. Writing maintainable and quality code is absolutely a
must in an environment where multiple teams are working on the same application. These tools
will allow smooth way of ensuring that everybody is following a defined coding standard.
To install PHPDepend issue the following commands for the CMD
pear channel-discover pear.pdepend.org
pear install pdepend/PHP_Depend-beta
CMD output is given below.
7 http://surenrodrigo.blogspot.com
To install PHP_Codesniffer, issue the following commands on the CMD
pear install PHP_CodeSniffer-1.2.0
CMD output is given below
OK, now we have all the tools necessary installed in our CI server. Now, it’s time to install our SVN
server. Chances are that you are already using a SVN server for your development. If you are not, I’m
providing you details about installing and configuring a SVN server in a windows environment.
13. I’m using Visual SVN server which allows you to setup a windows based SVN server environment
quickly and easily. Please read the license agreement and install the software product
accordingly. I have also setup TortoiseSVN as my svn client to connect to the svn server. Setting
up Visual SVN Server and TortoiseSVN is pretty straight forward hence; I’ll not go into details
here.
14. Create a new Repository using Visual SVN management panel. And checkout/commit the project
code you wish to manage using the CI environment.
8 http://surenrodrigo.blogspot.com
15. We’ll now install the necessary Hudson plug-in to work with our installed tools. Load Hudson in
a browser and navigate to “Hudson » Plug-in Manager”. Selected “Available” tab and select
(check) the following plug-in
a. Checkstyle b. Clover c. DRY d. Green Balls (handy because it shows successful builds as green circles rather than blue) e. JDepend f. xUnit (will handle the output of PHPUnit test results XML)
Now press the install button in the bottom of the page. Installation will take some time
depending on your internet connection. Once ever y thing is installed successfully you should
see a similar screen as below
Now click on the “Restart when no jobs are running” button on the top left hand side to restart
Hudson.
Now we have successfully setup the environment necessary to using Hudson as a productive CI server in
our build server. The following section will explain how to setup a build script using Phing and how to
setup a Hudson job to use the build script and do continues integration of our project.
9 http://surenrodrigo.blogspot.com
16. Now we’ll setup the build script for our project. By now it’s assumed you have setup a SVN
server account for your project and your project files are ready to be committed for the SVN
server. The following Phing build script can be used with any of the PHP projects you wish to
manage with a CI server. I’m assuming that our project is called “TestDrivenDevelopment”
Please change the name according to your project name.
<?xml version="1.0" encoding="UTF-8"?>
<project name="TestDrivenDevelopment" basedir="." default="app">
<property name="builddir" value="${ws}/build" />
<target name="clean">
<echo msg="Clean..." />
<delete dir="${builddir}" />
</target>
<target name="prepare">
<echo msg="Prepare..." />
<mkdir dir="${builddir}" />
<mkdir dir="${builddir}\logs" />
<mkdir dir="${builddir}\logs\coverage" />
<mkdir dir="${builddir}\docs" />
<mkdir dir="${builddir}\app" />
</target>
<!-- Deploy app -->
<target name="app">
<echo msg="We do nothing yet!" />
</target>
<!-- PHP API Documentation -->
<target name="phpdoc">
<echo msg="PHP Documentor..." />
<phpdoc title="API Documentation"
destdir="${builddir}\docs"
sourcecode="yes"
defaultpackagename="MHTest"
output="HTML:Smarty:PHP">
<fileset dir="source">
<include name="**/*.php" />
</fileset>
</phpdoc>
</target>
10 http://surenrodrigo.blogspot.com
<!-- PHP copy/paste analysis -->
<target name="phpcpd">
<echo msg="PHP Copy/Paste..." />
<exec command="phpcpd --log-pmd=${builddir}\logs\pmd.xml source" escape="false" />
</target>
<!-- PHP dependency checker -->
<target name="pdepend">
<echo msg="PHP Depend..." />
<exec command="pdepend --jdepend-xml=${builddir}\logs\jdepend.xml ${ws}\source"
escape="false" />
</target>
<!-- PHP CodeSniffer -->
<target name="phpcs">
<echo msg="PHP CodeSniffer..." />
<exec command="phpcs --standard=ZEND --report=checkstyle ${ws}\source >
${builddir}\logs\checkstyle.xml" escape="false" />
</target>
<!-- Unit Tests & coverage analysis -->
<target name="phpunit">
<echo msg="PHP Unit..." />
<exec command="phpunit --log-junit ${builddir}\logs\phpunit.xml --coverage-clover
${builddir}\logs\coverage\clover.xml --coverage-html ${builddir}\logs\coverage\
${ws}\source\tests"/>
</target>
</project>
Please make sure the build script is placed in the root folder of the project always.
17. Now, we’ll setup a Hudson Job for our project. In the Hudson home page click “New Job” fill in
the project details as shown in the below screenshot.
11 http://surenrodrigo.blogspot.com
This creates a new Hudson job, and you will be taken into the configuration screen.
Below points covers configuring the Hudson job for our project. For each project you wish to manage
with Hudson you need to create different jobs.
18. In the “Source Code Management” section select “Subversion” and click on the “?” adjacent to
the “Repository URL” text box. This should display a help description as shown below in the
screenshot. Click on the “this link” to load the Subversion configuration screen.
19. In the Subversion configuration screen enter the URL of the SVN repository you have created for
the project. Also, you need to enter the Username and Password and select the protocol (HTTPS
or HTTP) you are using to connect to the repository and press “OK”. Please see the below screen
shot.
12 http://surenrodrigo.blogspot.com
You will be taken back to the configuration screen explained in the above point (point 18). You
need to Enter and select the following here.
a. Repository URL: (URL you just configured) b. Local module directory: source c. Check “Use update” which speeds up checkout
20. Under “Builder Triggers” section, select “Poll SCM” and enter the following.
5 * * * *
10 * * * *
15 * * * *
20 * * * *
25 * * * *
30 * * * *
35 * * * *
40 * * * *
45 * * * *
50 * * * *
55 * * * *
This ensures that Hudson will check the SVN repository every 5 minutes for changes and if there are changes it will trigger a build automatically.
21. Under “Build” Section click “Add build step” button and select “Execute Windows batch command”. In the “Command” text box enter the following. phing -f %WORKSPACE%\source\build.xml prepare app phpdoc phpcpd pdepend phpcs phpunit -Dws=%WORKSPACE%
This is our Phing build command.
13 http://surenrodrigo.blogspot.com
22. The Configuration of the “Post Build Action” section is very important. Please configure the
section so it looks like as shown in the below screen shots
Press the “Save” button after you ensure that you have entered all the directory path values and other
configurations exactly as shown above.
14 http://surenrodrigo.blogspot.com
We are actually configuration the Hudson modules we have installed in a previous step to work with this
project. For every new Hudson job you create you can use the same configuration values for this section
(as long as you use the same build.xml script I have listed above).
Configuration is now complete. You will be taken to the new Job screen. Press “Build now” from your
left hand side navigation links to trigger a new build (make sure you have the project source code
committed to the repository and your build.xml is located in the root folder of the project), you will see
the building happening visually within the “Build History” section. If everything goes well you’ll have a
successful build with a green ball appearing near your build number.
A Green ball represents a successful build and a Red ball indicates a build failure. If for some reason your
build fails, click on the build within the “Build History” section. This will take you the build description
screen as shown below,
Click on the “Console Output” to see the complete console output of the build. This will provide you
with all the information that is required to fix the build failure.
It will take few successful builds for Hudson to generate all the necessary reports. Once you have
completed few builds it will be obvious how valuable Hudson will be. A matured Hudson job screen is
shown below.
15 http://surenrodrigo.blogspot.com
Well, that’s it. Now you have an invaluable tool at your disposal to manage your builds, track the quality
of your code and test cases. Not only that it will also document your PHP Code using PHPDocumenter
and you can manage all of this within easy to use user interface of Hudson.
Hope this information has helped you to see how PHP can be used successfully to develop Enterprise
level applications.