• News

Continuous Integration For Laravel With Jenkins And Git

This will be a hands on guide for setting up automated builds for Laravel using Jenkins. Pretty much that when you do a commit, Jenkins will automagically make a build and in that check code errors and syntax, run unit tests and provide visual code coverage for your code base. Along with other goodies.

I’ve completely switched over to Laravel as my weapon of choice for PHP frameworks, I’m not going to get into details about why but the philosophy behind it is just as awesome as the code. One of the concepts is that the entire framework is unit tested with 100% code coverage, which makes it a perfect candidate for continuous integration.

Jenkins is an open source continuous integration server that has gotten a lot of attention recently as the #1 open source continuous integration server. Mostly because its vast amount of plugins, which currently is 600+ as I’m writing this. We’re going to use 10 of those to automate our test environment.

This guide will use the following:

  • Ubuntu 12.10
  • PHP 5.4.6
  • Git
  • Jenkins

Installing Stuff

We’re going to start off by installing all the necessary software for this to be possible. We’ll start with PHP, Git and Jenkins.

1 sudo apt-get install php5 git-core curl jenkins

You will now be able to open Jenkins in your browser by going to http://localhost:8080 to see if the installation worked.

Then it’s time to setup a Laravel installation.

1 cd /var/www

2 git clone https://github.com/laravel/laravel.git laravel

And now you’ll a fresh installation of Laravel in /var/www/laravel. Now we continue by installing all the PHP packages through PEAR and also the plugins Jenkins need. Most of this is based on jenkins-php. We also download the latest jenkins.war file since the one shipped with the standard package in Ubuntu has caused me nothing but problem.

1 sudo apt-get install php-pear

2 sudo pear config-set auto_discover

3 sudo pear install pear.phpqatools.org/phpqatools

4 curl -L http://updates.jenkins-ci.org/update-center.json | sed '1d;$d' | curl -X POST -H 'Accept: application/json' -d @- http://localhost:8080/updateCenter/byId/default/postBack

5 jenkins-cli -s http://localhost:8080 install-plugin checkstyle cloverphp dry htmlpublisher jdepend plot pmd violations xunit git

6 sudo wget -O /usr/share/jenkins/jenkins.war http://mirrors.jenkins-ci.org/war/latest/jenkins.war

7 sudo /etc/init.d/jenkins restart

Go to Jenkins web interface (when it has finished restarting) and go to Manage Jenkins -> Configure System, then scroll down until you find Git Plugin. Then fill in the git configuration for Jenkins, such as jenkins as jenkins@localhost and hit Save.

Configure Build

Now clone my github repository laravel-jenkins which is the boilerplate for all the config files and the Jenkins job.

1 cd /var/www

2 git clone git://github.com/nerdklers/laravel-jenkins.git

3 mv laravel-jenkins/* laravel/

4 cd /var/www/laravel

Now you should have these files in your Laravel directory as well:

build/

  • code-browser/
  • coverage/
  • logs/
  • pdepend/
  • phpcs.xml (PHP Code Sniffer config)
  • phpmd.xml (PHP Mess Detector config)

build.xml (build config)

config.xml (Jenkins job config)

phpunit-bootstrap.php (PHPUnit bootstrap script)

phpunit.xml.dist (PHPUnit config)

Configure Jenkins

Now we have to setup a job in Jenkins for building your application on commit. How this will work is that you add a hook to git which will trigger Jenkins to pull the code and start the automated build. Lucky you I have just like a TV chef prepared that for you.

First we start by setting up the Job by moving it to Jenkins folder for jobs and reloading the configuration.

1 cat config.xml | jenkins-cli -s http://localhost:8080/ create-job laravel-job

2sudo chown -R jenkins:jenkins /var/lib/jenkins/jobs/laravel-job

3 jenkins-cli -s http://localhost:8080 reload-configuration

Then you must (but it’s optional..) add a post-commit git hook, that will trigger every time you do a commit. What it does is notify the Jenkins that a commit has been made and that it should fetch the code and do an automated build.

1 nano .git/hooks/post-commit

Then add this to that file and save

1 #!/bin/sh

2 curl http://localhost:8080/git/notifyCommit?url=/var/www/laravel

And we also need to make the git executable

1 chmod +x .git/hooks/post-commit

Try It Out

All you have to do now is make a commit.

1 git add .

2 git commit -m "Test autobuild in Jenkins"

After your commit you should see this in Jenkins, and the build should pass. Since Laravel ships with an example test (that just asserts true is true) you should have one passed test as well.

Comments (0 comments)

    Recent Articles

    • All About Git Pretty Stats

      All About Git Pretty Stats

      The git status command is used to show what's going on with the repository and the staging area. It lets us see the files that have been tracked and the ones that haven't. This command won't show any commit records or other information, so don't use this command.

    • Laravel Vs Symfony2 - Is There Really A Difference?

      Laravel Vs Symfony2 - Is There Really A Difference?

      The two frameworks share a lot of things. Laravel also uses most of its third-party libraries from Symfony components, so about 30% of Laravel code is written in Symfony. Symfony, on the other hand, is a more complicated framework that is hard to learn, but it is better for big projects because it is more complicated.

    • How To Get Raw POST Data In Laravel

      How To Get Raw POST Data In Laravel

      Laravel's IlluminateHttpRequest class gives you an object-oriented way to interact with the current HTTP request that your application is handling. You can also get the input, cookies, and files that were sent with the request.

    • Cyclomatic And Npath Complexity Explained

      Cyclomatic And Npath Complexity Explained

      Cyclic complexity and NPath complexity are both measured for a method of a function, just like cyclic complexity. It is the number of ways a given piece of code can be run (ignoring cycles).

    • LeetCode – Essential? Optional? Big Deal Or Not?

      LeetCode – Essential? Optional? Big Deal Or Not?

      Are you a developer aiming to put Facebook or Google (or even both!) on your résumé? There are many ways to improve one’s computer skills. LeetCode offers help in that capacity and even promises to get you employed in giant tech companies.

    • The Depths Of AngularJS's Watches

      The Depths Of AngularJS's Watches

      When you want to watch for changes in AngularJS, it is fairly kind to you by default. The kindness comes from trying to optimize performance for you. This is because each watch expressions will run at least once during each $digest loop. I'll not go into when the $digest loop executes and why, and I'll also use watches in controllers in my examples which you should never do, but that's a whole other discussion.

    • Cyclomatic And NPath Complexity Explained

      Cyclomatic And NPath Complexity Explained

      Cyclomatic And NPath Complexity Explained