Continuous integration (CI) allows a team to commit their work quickly, which means there will be multiple commits to a repository daily. Every time there is a commit it is verified by an automated build (including test) to alert you of any build or test errors immediately. Once the process of integrating and verifying your work into the master becomes automatic you are able to develop cohesive software rapidly.

What is Travis CI?

Travis CI automatically sets up a CI environment and makes it simple for anyone to test and deploy their app. Their build system supports many different languages, you just have to define which language this project is and Travis CI will take care of the rest. You can find more information about all the other environments they offer in their docs, but for this article we will focus on adding Travis CI to a PHP repository on GitHub.

Public repositories on Github can use Travis CI for free, but they share community boxes and sit in queues. To use a private repository with Travis CI you can subscribe to one of their plans and get your project built without waiting, plus you can have concurrent jobs.

Note: The open source site ends in ‘.org’ (https://travis-ci.org/) and the private repository site ends with ‘.com’ (https://travis-ci.com/).

Getting Started With Travis CI

To follow along just fork this example repository. Please feel free to change it’s files and see what the settings do.

Step 1: Sign In

Sign in to Travis CI with your GitHub account by clicking the Sign In link at the top right.

On this page you will see off to the left a list of public repositories being built. To watch their build live just click one of them.

Step 2: Sync Your GitHub Repos

After you’ve signed in Travis CI will synchronize your public repositories to your account under your profile page. You will then need to click the switch button into the On position. Now every time you commit to this repository GitHub will make a call to Travis CI and automatically initiate the build process.

If you create a repository and you do not see it listed click Sync now on your profile page and your newly created repository will appear there.

Step 3: Add .travis.yml file to your repository

To tell Travis CI what environments you want your repository built and tested under you’ll need to add a .travis.yml to the root of your repository. Travis CI will run your tests against a matrix of all possible combinations of your configuration using:

  • Runtimes you specified under php
  • Environment variables you define with env
  • Exclusions, inclusions, and allowed failures

Learn more about build matrices

# Required to run your project under the correct environment.
language: php

# Versions of PHP you want your project run with.
php:
  - 5.4
  - 5.5
  - 5.6
  - 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

Example Configurations

Learn more about .travis.yml PHP options

Step 4: Trigger A Build With a Git Push

Your first build must be triggered by a commit and push from GitHub, not the Restart Build button.

Every time you push a commit to a branch on your repository, Travis CI will initiate the build process. Once a worker is available it will start building your project. You can navigate to https://travis-ci.org/GITHUB_USERNAME/REPO_NAME to watch it’s build progress live.

If you want to restart your build, click the Restart Build button and you will see your project start building once a worker is available.

Step 5: Tweaking your build configuration

In the example repository you will find a .travis.yml file that includes the basics with a few extras to some show of the potential Travis CI has. If you want to customize your configuration you can configure almost everything about your build environment.

Learn more about Travis CI’s PHP configuration.

Step 6: Test it

To see if you configured your repository correctly edit your README.md file on GitHub and commit your edit. Go over to that repository’s Travis CI page and all the circles under Job should become yellow and once the build is running the duration should start incrementing.

Once the build is complete and the light goes red click it and read the output in the console. You can also setup Travis CI to send you an email letting you know what happened via the notifications option.
If your build doesn’t start try validating against a YAML Validator to make sure there are no mistakes in your configuration file.

Learn more

Travis CI workers come with many different services that you might depend on and there are guides for all of them.

Build Status Images

Travis CI provides a status image that you can place in your project’s README or general documentation. Then visitors can immediately see it’s build status.

Here’s a status image from the example repository. Hopefully it’s green right now.

Build Status

Travis CI PHP Example
===========

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

Image URL Format:
https://travis-ci.org/USERNAME/REPO_NAME.svg?branch=BRANCH_NAME

Quick Overview

Current

All of the builds that you have defined with your configuration options will appear here with a colored icon next to them noting their build status.

  • Blank screen: You have yet to run your first build
  • Yellow: In progress
  • Red: Error, check the console
  • Green: Success!

Build History

Every time a build is run it receives a build number and creates a history that you can see what commit triggered the build, a link to the commit on GitHub, the duration of the build, and when it happened.

Pull Requests

To give project managers a good idea of whether or not a pull request would break your build, Travis CI automatically builds your project with that pull request and shows it’s status on the PR.

Branch Summary

Travis CI not only builds your master branch but also all other branches and the Branch Summary tells you the current build status of each one.

Conclusion

As you can see, Travis CI is a robust and feature rich service aimed at keeping your projects alive and kicking through any and all contributions. Have you implemented it in your projects? Got tricks to share? Let us know!

Frequently Asked Questions (FAQs) about PHP Continuous Integration with Travis CI

How do I get started with Travis CI for PHP continuous integration?

To get started with Travis CI for PHP continuous integration, you first need to sign in to Travis CI with your GitHub account. After signing in, you can enable Travis CI for a specific repository by going to your profile page and flipping the repository switch on. Then, you need to add a .travis.yml file to your repository to tell Travis CI what to do. This file should be placed in the root of your repository and must specify the language and versions you want your project to be tested against.

What is the purpose of the .travis.yml file in Travis CI?

The .travis.yml file is a configuration file that Travis CI uses to understand your project and how you want your builds to be executed. It specifies the programming language of your project, the desired building and testing environment and various other parameters. This file should be placed in the root of your repository.

How can I customize my build in Travis CI?

Travis CI allows you to customize your build by adding a .travis.yml file to your repository. This file can include information like the language of your project, the versions of the language to test against, the scripts to run before or after the build, and environment variables. You can also specify different stages of a build lifecycle to run specific commands.

How do I monitor the status of my builds in Travis CI?

You can monitor the status of your builds directly on the Travis CI website. After logging in, you can see a list of your repositories and their build status. Clicking on a specific repository will show you more detailed information about the build. You can also receive notifications about build status changes via email, Slack, or other methods by configuring your .travis.yml file.

How can I troubleshoot a failed build in Travis CI?

If a build fails in Travis CI, you can start troubleshooting by checking the build log. The build log shows all the commands that were run during the build and their output, which can help you identify any errors or issues. You can also use the “Rerun job with SSH” option to interactively debug a failed build.

What is the role of environment variables in Travis CI?

Environment variables in Travis CI are used to store values that are needed by your build. These can be set in your .travis.yml file or in the repository settings in Travis CI. They can store sensitive information like API keys or credentials, which should not be hard-coded into your scripts.

How can I use Travis CI for a PHP project that uses a database?

Travis CI supports several databases including MySQL, PostgreSQL, and others. You can specify the database service in your .travis.yml file and Travis CI will set it up for you during the build. You can also specify a script to initialize the database or import data.

Can I use Travis CI with a private GitHub repository?

Yes, Travis CI supports both public and private repositories. For private repositories, you will need a paid Travis CI plan. Once you have a plan, you can enable Travis CI for your private repository just like you would for a public one.

How can I speed up my builds in Travis CI?

There are several ways to speed up your builds in Travis CI. One way is to use caching to save time on downloading dependencies. You can also split your tests into multiple jobs that run in parallel, or reduce the number of versions or environments you are testing against.

What is the difference between Travis CI and other continuous integration tools?

Travis CI is a hosted continuous integration service that is tightly integrated with GitHub, making it a great choice for projects hosted on GitHub. It supports a wide range of languages and services, and is known for its ease of use and comprehensive documentation. Other tools may offer different features or integrations, so the best choice depends on your specific needs and circumstances.

Michael CalkinsMichael Calkins
View Author
cicontinuous integrationPHPTestingtravistravis-ci
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week