• News

Continuous Integration For Laravel With Jenkins And Git

1KShares
182.2KViews

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

  • Meet The Richest Tech Billionaires In 2023

    News

    Meet The Richest Tech Billionaires In 2023

    During the stock market's sharp drop, the richest tech billionaires lost a total of close to $500 billion. This is more than the market value of all but seven of the S&P 500 companies.

  • Political Opinion Analysis - Understanding Public Sentiment Using Programming Languages

    News

    Political Opinion Analysis - Understanding Public Sentiment Using Programming Languages

    Discover the Power of Political Opinion Analysis with Programming Languages: Learn how Python and R can help shape public discourse, inform policy decisions, and forecast election outcomes. Explore the applications and limitations of political opinion analysis, and delve into the ethical considerations that researchers need to keep in mind. Unlock the potential of political opinion analysis and stay ahead in the world of politics with programming languages.

  • What Is A Dogecoin Faucet?

    News

    What Is A Dogecoin Faucet?

    Dogecoin can be obtained for free using a "doge faucet" that distributes small quantities of doge in exchange for the user performing some action. You are, in essence, trading your time and attention in exchange for the opportunity to earn free Dogecoin.

  • The Importance Of Web Development In Digital Marketing

    News

    The Importance Of Web Development In Digital Marketing

    This article explains the web development process and the role of web design in creating an effective website optimized for SEO and user experience. It also discusses the essential elements of e-commerce web development and how to choose the right web development framework.

  • The Role Of Science And Technology In Our Daily Life

    News

    The Role Of Science And Technology In Our Daily Life

    Due to the role that they play in making life easier, faster, and safer, science and technology are essential to our daily lives. Let's examine the role that science and technology play in the modern world.

  • Technology Is Changing News - An Insight View About The Future Of Media

    News

    Technology Is Changing News - An Insight View About The Future Of Media

    The news industry is evolving quickly and technology is changing news. Laptops and pocket PCs have displaced desktops. Numerous brand-new material sources have been developed and connected via the internet.

  • The Role Of Programming In Architecture

    News

    The Role Of Programming In Architecture

    This article explores the role of programming in architecture, highlighting its impact on collaboration, sustainability, personalized spaces, cost efficiency, and innovative design. Discover how programming is revolutionizing the field of architecture and shaping the future of the industry.

  • Kenya Technology - A Hidden Industry

    News

    Kenya Technology - A Hidden Industry

    The northeastern African country of Kenya is a tiny coastal one. Kenya is praised as a top tourist destination for its tea exports, stunning scenery, and abundant biodiversity. Currently, Kenya technology industry is growing quickly.

  • 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

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

  • The Depths Of AngularJS's Watches

  • Famous Celebrities Who Learn To Code

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