Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

PHP 7.3 Is Now Available On Cloudways. Try Now!

How to do PHP Continuous Integration With Travis CI

May 10, 2018

7 Min Read
Reading Time: 7 minutes

In this article, I will demonstrate the process of PHP continuous integration with Travis CI. If you are interested in using Travis CI as your continuous integration environment, read on!

php continuous integration

Code versioning has become a standard practice in development circles, with GitHub being a popular platform for hosting code repos. However, a common issue is the testing of the code as it is pushed by a team member. As the volume of commits increases, ensuring the quality and accuracy of code becomes a challenge.

This is where the idea of Continuous Integration comes in. it is defined as a development subprocess that automates build creation and code testing. As soon as a team member pushes a commit, the continuous integration process compiles the code, execute it and checks for bugs. Thus, the developers always have a fair idea about the quality of their code.  

The good news for PHP developers is that continuous integrations could be easily setup, thanks to tools that integrate right into GitHub.

How Continuous Integration in PHP Works With Travis CI?
1. Travis CI for PHP Continuous Integration
2. Integrate GitHub Repository with Travis
3. Host PHP Code on GitHub
4. Clone PHP Code on Cloudways
5. Add .travis.yml file
5. Push Changes to GitHub
6. Add Build Badge to README.md

Travis CI for PHP Continuous Integration

Travis CI, is a “PHP Continuous Integration” environment.

Whenever you commit code changes, it can execute the unit tests, and other tools such as codesniffer, mess detection, copy-paste detection, document builder, etc. Even more interesting, it can run pre-written tests against different PHP versions of PHP.

Since it reports the results of the test runs, you always know whether your changes are stable orif any part of your code is broken. Travis CI is very popular among developers because it allows extensive testing of code snippets and make successful builds for the various versions of applications.

Deploy PHP Apps Without Worrying About Servers

Our Managed Cloud Hosting for PHP applications takes care of all hosting related hassles

Integrate GitHub Repository with Travis

The first step in building a continuous integration with Travis is to connect your GitHub account and integrate GitHub repos with Travis website. Once you sign up, you will be asked to authorize Travis CI to sync your repositories. Enter the GitHub password and hit Enter. In a few seconds, your repositories will appear and now you can use any repo for integrating with Travis:

Host PHP Code on GitHub

Travis CI works with the code hosted on GitHub. Thus, you need to host all the source code in a GitHub repo. You can follow this simple guide to deploying code at GitHub. Start by installing Git on your local machine and then use the following commands:

git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/username/your-repo.git
git push -u origin master

You will need to enter your GitHub credentials (username and password) once the code has been deployed on the repository.

Read more: Enhanced Cloudways Staging Environment Is Now Available for All Users

Clone PHP Code on Cloudways

The second step is to clone your code from GitHub to your server. In this case, I am using Cloudways PHP 7 hosting. Simply sign up for a Cloudways and launch a PHP server.

Next move to the application via SSH terminal using the Master Credentials available in the Access Details page

Navigate to the public_html folder and run the following command:

$ git clone https://github.com/shahroznawaz/php-notifications.git

Add .travis.yml file

Next, I will add .travis.yml file that is the backbone of this workflow. This file contains the rules which will be validated against your code. Travis CI supports several PHP versions, all of which include XDebug and PHPUnit for PHP unit testing.

The rules can be customized to fit the requirements of your projects. A simple example is adding the file to the root directory where you wish to clone the project. Add the following code to the file:

# Required to run your project under the correct environment.

language: php

# Versions of PHP you want your project run with.

php:

 - 5.5

 - 5.6

 - 7.0

 - 7.1

 - 7.2

 - hhvm

# Commands to be run before your environment runs.

before_script:

 - composer self-update

 - composer install --prefer-source --no-interaction --dev

# Commands you want to run that will verify your build.

script: phpunit

# allow_failures: Allow this build to fail under the specified environments.

# fast_finish: If your build fails do not continue trying to build, just stop.

matrix:

 allow_failures:

   - php: 5.6

   - php: hhvm

 fast_finish: true

# Customize when the notification emails are sent.

notifications:

   on_success: never

   on_failure: always

Here I’ve added the PHP versions that are allowed for the projects.

before_script allows you to run command before the environment actually executes.

allow_failures contains the conditions that allow your build to fail.

notifications will notify you in all scenarios of build success or failure.

If you are using Symfony 3, you can add this script provided by Antonio Jiménez

# Project language
language: php

# Allows use container-based infrastructure
sudo: false

# Start mysql service
services:
 - mysql

# Cache composer packages so "composer install" is faster
cache:
 directories:
   - $HOME/.composer/cache/files

# Matrix to test in every php version
matrix:
 # Fast finish allows to set the build as "finished" even if the "allow_failures" matrix elements are not finished yet.
 fast_finish: true
 include:
   - php: 5.5
   - php: 5.6
   - php: 7.0
   - php: hhvm
 allow_failures:
   - php: hhvm

# Define an environment variable
env:
 - SYMFONY_VERSION="3.0.*" DB=mysql

# Update composer
before-install:
 - composer self-update

# Install composer dependencies,
# Create database, schema and fixtures
install:
 - composer install
 - cp app/config/parameters.yml.dist app/config/parameters.yml
 - php bin/console doctrine:database:create --env=test
 - php bin/console doctrine:schema:create --env=test
 - php bin/console doctrine:fixtures:load -n --env=test

# Run script
script:
 - phpunit

# After a build, send email notification with the build results
notifications:
 email: your_email

First it defines the language (all permitted version of PHP) and turn off the sudo usage. Next, it defined MySQL  as a service. Next, the PHP versions to be test with Symfony are defined. Environment variables can also be added for Symfony and database. Next, composer dependencies, create database, schema and fixtures are defined and in the notifications, you can add your email to get the status.

If you are using Symfony 4, set the desired version in SYMFONY_VERSION.

Replace – cp app/config/parameters.yml.dist app/config/parameters.yml with – cp .env.dist .env

If your database configuration in .env.dist doesn’t work with Travis, consider adjusting it or create a new .env.travis with suitable database configuration for Travis-CI (in this case use – cp .env.travis .env).

Push Changes to GitHub

It is time to make changes to the code. When done, remember to commit the code and push the code to the repository.

Next, go to Travis website, you could that the build has started executing and the repo has turned yellow.

Shortly, you will see the results (success or failure) of the build run. In my case, you can see that it fails. No need to worry about it. I will just troubleshoot the code and fix the issues.

Add Build Badge to README.md

On the GitHub, you might have seen the build badges like this:

Since I’ve also integrated Travis CI, I can also add this badge to let the users  know the status of the current build. Edit the README.md file and add the following code to it:

[![Build Status](https://travis-ci.org/shahroznawaz/php-notifications.svg?branch=master)](https://travis -ci.org/ shahroznawaz/php-notifications)

Now when you push the code to GitHub, you will see the badge like this:

FAQs

What is the main purpose of continuous integration?

The main purpose of Continuous integration is to validate and test codebase at every time it is changed. It automates the build every time whenever something is changed in the code by the team members working on the project, identifies possible errors in the code and lets everyone know about it so that could be corrected accordingly. Continuous integration gives all the team members the privilege to share their code and unit tests by accumulating their changes into a shared repository after each task completion.

What are the best practices of continuous integration in PHP?

The best practices for Continuous integration in PHP are given as follows:

  • Maintain a code repository
  • Automate the build
  • Do integration testing before unit testing
  • Make the build self-testing
  • Always test the application in a clone environment

When to use continuous integration?

Continuous integration should be used when your application is been undergoing with rapid changes and when its manual testing becomes a bit hefty task. Because continuous integration gives you the privilege to test each change done to your code automatically, so that no errors gets remained at disposal and all the codebase runs correctly every time.

Why continuous integration is important for agile?

Continuous integration is important in Agile because it gives faster checking of codebase and faster correction to its errors and that too at any stage of the deployment, which is also what the basic concept of Agile methodology is. That is why CI becomes important for opting in agile working way.

What are best tools for PHP Continuous Integration

Following are the list of best tools for Continuous integration in PHP.

  • Jenkins
  • Team City
  • Travis CI
  • Bamboo
  • Cruise Control
  • GitLab
  • Circle CI

Wrapping Up

Travis is a great tool for implementing PHP continuous integration within your projects. As you could see, Travis simplifies the process and ensures that the developers could automate and streamline the build management process. The result is a very clean Master branch that ensures the highest quality of code at all times.

If you need help in integrating Travis or the continuous integration process just drop a comment below.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at shahroze.nawaz@cloudways.com

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!