Pragmatism in the real world

Using CircleCI for a PHP project

For a new client project, I’ve decided to use CircleCI to run my tests every time I push to GitHub.

This turned out to be quite easy; this is how I did it.

I started by creating a config file, .circleci/config.yml containing the following:

version: 2
jobs:
  build:
    working_directory: /var/www/html
    docker:
      - image: php:7.1-apache
        environment:
          APP_ENV: test
    steps:
      - run:
          name: Install system packages
          command: apt-get update && apt-get -y install git
      - run:
          name: Install PHP extensions
          command: docker-php-ext-install pdo
      - checkout
      - run:
          name: Install Composer
          command: |
            php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
            php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer invalid'; unlink('composer-setup.php'); } echo PHP_EOL;"
            php composer-setup.php
            php -r "unlink('composer-setup.php');"
      - run:
          name: Display PHP information
          command: |
            php -v
            php composer.phar --version
      - run:
          name: Install project dependencies
          command: php composer.phar install
      - run:
          name: Run CS tests
          command: vendor/bin/phpcs
      - run:
          name: Run Unit tests
          command: vendor/bin/phpunit

The documentation is really good, but the file’s organisation is pretty self-expanatory.

The config file has a list of jobs. The build job is run on a push to GitHub, so that’s the one I’ve created. Inside the docker section contains a list of docker images that are required – in my case, I just need a single container running PHP 7.1. The other section in the job is the list of steps to run. Each step has a name and a command which is a bash command.

For my job, I need to install git and the PDO PHP extension. I then run the “magic” step called checkout which as per its name, checks out my source code. I then install composer, including verifying that it’s valid and display the PHP version number and composer version number in case I ever need them to work out why a build failed.

I then turn my attention to the project itself and run composer install and then the tests themselves: phpcs and phpunit.

Running the build

To actually get builds to run, you log into CircleCI – usefully you authenticate via GitHub – and select the project to build. From now on, pushing to GitHub or a branch will result in the build running. On success, you get something like this:

Circle ci

Hooking up to Slack

Hooking up to Slack is done by going to Slack’s Apps & Integrations section and searching for CircleCI. Add the configuration and follow the wizard. Once done, you get a URL that you add to the project’s Chat Notifications settings in CircleCI. This is found by clicking on the “cog” next to the project’s name in the builds list. There’s also a setting callled “Fixed/Failed only” which I check.

Once that’s done, you get Slack notifications on failures and then on the first success after a failure and you are now secure in the knowledge that your tests are being run reliably.

6 thoughts on “Using CircleCI for a PHP project

  1. How do you determine the working directory?

    I see on another example, they set it to ~/somedirectory

Comments are closed.