GitLab CI for PHP developer

GitLab CI for PHP Developers

If you are familiar source code version controlling you may heard about GitLab. GitLab is a wonderful ecosystem for any software engineering project to track codes, integrating CI/CD, issue tracking, etc. In recent days GitLab CI became a demanding skills in your resume. So let’s talk about GitLab CI/CD for PHP developers specifically.

What is CI?

CI is the short form of Continuous Integration. You are continuously and regularly updating your codebase, adding new features by yourself alone or with other team members. CI itself plays a vital role to make sure on every changes it will make sure that nothing bad is happening for that changes.

You will configure few configurations or steps or actions to check all are going well whatever code pushed or whatever commit your team members pushed to the repo.

So let’s see as a PHP developer how you can run a basic set of tools in your GitLab CI pipelines. I am running a tests stage where on every commit I will push it will run the PHPUnit tests, if it fails it will notify me or mark that commit or if it passes that tests, that means your recent commit is well.

GitLab CI for PHP application

If you prefer TDD (Test Driven Development) approach for your software development, you will write unit tests, functional tests for everything that you write or in your codebase to make sure it always passes all the tests.

Manually to check everytime someone push any changes to the project is cumbersome and it will take time and energy. In this case any CI tools can do that review job for you. So let’s setup a typical GitLab CI configuration for a PHP application that will run some unit tests written with PHPUnit.

Configuration for gitlab-ci.yml

So to run the CI job, you need to add a very special file in your project root. That is called gitlab-ci.yml. Everytime someone push code to the repository GitLab will scan for this file. If exists it will run CI job as per the configuration written on it.

I just wrote a simple CI job for one of my PHP application. Here is the configuration.

# gitlab-ci.yml

# Select what we should cache
cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - vendor/

# We need MySQL as database to test something that related to DB operation.
# it will pull docker image tagged with mysql:5.7 and start the service on every run
services:
  - mysql:5.7

# Configuring some environment variable that will be used on every CI pipeline job
variables:
  # Configure mysql service (https://hub.docker.com/_/mysql/)
  MYSQL_ROOT_PASSWORD: root

# This is our original task that will run
test:
# We are using php:7.4 docker image here
  image: php:7.4
  stage: test
  # Global variables for your PHP app
  variables:
    APP_NAME: "Hello World"
    # root is the DB password you set above. @mysql is the address of the MySQL service you added above
    DATABASE_URL: "mysql://root:root@mysql:3306/symfony"
  script:
    # Install git, the php image doesn't have installed
    - apt-get update -yqq
    - apt-get install git libzip-dev zip -yqq
    # Install xdebug specially for code coverage reporting
    - pecl install xdebug
    - docker-php-ext-enable xdebug

    # Install mysql driver
    - docker-php-ext-install pdo_mysql zip

    # Install composer
    - curl --show-error --silent https://getcomposer.org/installer | php

    # Install all project dependencies
    - php composer.phar install

    # Run PHPUnit with code coverage report
    - ./bin/phpunit --configuration phpunit.xml.dist --coverage-text --colors=never
# To let GitLab understand your code coverage rating, you should write the below regex
  coverage: '/^\s*Lines:\s*\d+.\d+\%/'

That’s it. Add this gitlab-ci.yml file in the root of your PHP application. Start commit and push, go to GitLab project’s jobs. You will see it’s running this job. If you face any trouble implementing GitLab CI for PHP application, write down your comments.

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.