• News

Continuous Integration For Laravel With Jenkins And Git

618Shares
103KViews

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

COPYRIGHT_CSWAG: Published on https://codingswag.com/laravel-with-jenkins-and-git/ by - on 2022-04-13T16:25:44.168Z

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.

Share: Twitter | Facebook | Linkedin

Recent Articles

  • Best Streaming Services For 2023 That You Should Check Out

    News

    Best Streaming Services For 2023 That You Should Check Out

    Our best recommendations for both on-demand and live streaming services are included in our ranking of the best streaming services for 2023. We've evaluated market leaders like Netflix and Hulu as well as services with a more specialized focus, such as anime streaming portal Crunchyroll and sports-focused fuboTV.

  • Top Travel Apps To Find You Next Destination

    News

    Top Travel Apps To Find You Next Destination

    How do you choose your travel destination? How do you navigate a new location once you are there? All of these queries are answered by the top travel apps.

  • New Social Media Platforms And Apps That Are Changing The Game

    News

    New Social Media Platforms And Apps That Are Changing The Game

    There are always new social media platforms and apps coming out. Check out this article to learn about new marketing trends and important apps.

  • The Depths Of AngularJS's Watches

    News

    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.

  • Famous Celebrities Who Learn To Code

    News

    Famous Celebrities Who Learn To Code

    Of course, the typical middle or high school kid might not find those careers appealing. However, the truth is that learning to code can set you on a rewarding career path. In fact, a lot famous celebrities who learn to code have mastered coding, and the combined star power of the business may help make it more appealing.

  • Best Political Campaign Software - How Do Politicians Do Modern Campaigns?

    News

    Best Political Campaign Software - How Do Politicians Do Modern Campaigns?

    The best political campaign software for involving contributors, voters, and supporters is discussed in this article. Additionally, you will learn about the software's capabilities and its main features and pricing options in depth.

  • Cyclomatic And NPath Complexity Explained

    News

    Cyclomatic And NPath Complexity Explained

    Cyclomatic And NPath Complexity Explained

  • Best Shopping Apps For Saving Time While Shopping At Home

    News

    Best Shopping Apps For Saving Time While Shopping At Home

    We've compiled a list of the top shopping apps in this article to help you get the best prices, find the ideal goods and services, and save time. If you work as a marketer for companies in these sectors, think about collaborating with these applications to increase brand recognition and customer loyalty.

  • The Top Android NSFW Apps - Unleash Your Sins In Your Phone With The Next Apps

    News

    The Top Android NSFW Apps - Unleash Your Sins In Your Phone With The Next Apps

    No one is the wiser these days because you can stream and download all the HD porn you want for free. You probably came across this page while searching for the top Android NSFW apps because technology has made it simpler than ever to Netflix and unwind.

  • The Top Major Crypto-currency Exchanges In Nigeria - Be Free With Cryptos In Nigeria

  • Top Websites To Buy Jewelry Online For Fashion Fans

  • Computer Science Trends - Build An IT Career Studying These Trends

  • Most Interesting Technological Trends - The 2030 Technology

  • All About Git Pretty Stats