Automation

PHP Continuous Integration Template using Composer and JenkinsCI

PHP-Jenkins Template

Jenkins is a very popular Continuous Integration tool these days and using it with PHP project is became so easy with help of Jenkins-PHP  template  project. This project gives us ultimate guide to get started with basic setup of the PHP related tools and Jenkins plugins.

You can install Jenkins on various platform, in my old blog post ‘Adventures with JenkinsCI on Mac OSX and Linux‘ we have covered Jenkins installation. I got my JenkinsCI instance running on Mac OSX [http://localhost:8080]

Install Jenkins-PHP Plugins

Let’s now install basic Jenkins plugins using Jenkins-CLI. In iTerm type following commands to install these required plugins:

Here are some more useful plug-ins like phing, Ruby, RVM, PHP and Rake. You can install and restart Jenkins.

Now, you should see something like this in your Terminal or iTerm:

jenkins-cli-restart

 Composer and PHP Tools

Composer is a great dependency management  tools for PHP. We can take benefit of Composer in order to install all PHP related dependencies locally or globally, choice is yours.

Let’s create composer.json file with all Jenkins-PHP dependencies.

Now, let’s add following dependencies to composer.json

Now, Let’s install composer

I found an awesome template project on GitHub ‘Jenkins-php-quickstart‘ and forked it immediately. This project has all the required files to generate necessary reports. Here is how I installed this project.

composer_install

In this project, if we execute:

This will run all phing tasks and generate necessary reports.

phing

List of Reports

Now we have list of all the reports generated in the /build/ directory. Let’s explore what we got there

  • Code-Browser

There are code browser reports generated in the ‘build/code-browser/’ directory. You can open ‘index.html’ in the browser to explore your code

  • CheckStyle

There is file called ‘checkstyle.xml’ in the  ‘build/logs’ directory which displays checktyle reports for the existing project.

  • JDpend and Junit

Two file ‘jdepend.xml’ and ‘junit.xml’ in the  ‘build/logs’ directory which displays Junit style reports for the unit tests.

  • Copy Paste Detector & Mess Detector

There are two file called ‘pmd-cpd.xml’ and ‘pmd.xml’ to detect smell in the PHP code in the ‘build/logs’ directory.

  • Pdepend

Pdepend file are .svg file to plot a graph of the code analysis.

 

Configure Jenkins

Now, that we got all the necessary  reports generated locally. Now, we need to configure all the reports on Jenkins. Let’s create a new job called ‘jenkins-php-quickstart’. You need to visit Jenkins dashboard and click ‘New Job’ or ‘New Item”

 

create-job

Now, we need to do little hack in order to display PDepend reports on the Jenkins Project home. Add following HTML code in the project description:

Let’s configure Github repo ‘jenkins-php-quickstart‘ as a SCM. We can then add ‘build’ as ‘Execute Shell’

Here it looks like:

jenkins-conf1

 Now, configure Checkstyle, PMD, CPD reports as shown below:

reports

 Now, we are done. Click on ‘Save” and “Build Now” to build this project. Once we build the project. We got following nice looking reports on the project home page.

jenkins_reports

 

Composer Best Practices on CI

We got all the dependencies checked into the source control for this demo but it’s not ideal. We can follow some practice to use composer smartly

  • Install composer globally on Jenkins box

This will help to access all those build dependencies across various Jenkins jobs. This will save lot of time as we are not downloading dependencies all the time.

  • Get benefit of various Jenkins plugins like Phing, PHP Or Rake plugins
  • Browse required packages on Packagist website

Source Code & Video Demo

Source Code is available on GitHub : ‘Jenkins-PHP-QuickStart

 

HAPPY CI !