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!
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.
Clone PHP Code on Cloudways
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:
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:
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.
Customer Review at
“Cloudways hosting has one of the best customer service and hosting speed”
Sanjit C [Website Developer]