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
COPYRIGHT_CSWAG: Published on https://codingswag.com/laravel-with-jenkins-and-git/ by - on 2022-04-13T16:25:44.168Z
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.
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:
- 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)
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
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.