Laravel PHP Continuous Integration#

This guide shows you how to use Semaphore to set up a continuous integration (CI) pipeline for a Laravel PHP web application.

Demo project#

Semaphore maintains an example Laravel project:

In the repository you will find an annotated Semaphore configuration file .semaphore/semaphore.yml.

The application uses the latest stable version of Laravel, Composer, NPM, PHP Mess Detector, Code Sniffer, Copy Detector, PHPUnit, Laravel Dusk with Chrome, Sensiolabs and PostgreSQL as the database.

Overview of the CI pipeline#

The demo Laravel PHP CI pipeline performs the following tasks:

  • Code analysis by running:
  • PHP Mess Detector
  • Code Sniffer
  • Copy Detector
  • Runs PHPUnit unit tests
  • Runs browser tests through Laravel Dusk
  • Runs security Tests through Sensiolabs checker

The CI pipeline is composed of a dependency installation block and four blocks of tests:

Laravel PHP CI pipeline

Sample configuration#

# .semaphore/semaphore.yml
# Use the latest stable version of Semaphore 2.0 YML syntax:
version: v1.0

# Name your pipeline. In case you connect multiple pipelines with promotions,
# the name will help you differentiate between, for example, a CI build phase
# and delivery phases.
name: Semaphore PHP Example Pipeline

# An agent defines the environment in which your code runs.
# It is a combination of one of available machine types and operating
# system images.
# See
# and
    type: e1-standard-2
    os_image: ubuntu1804

# Blocks are the heart of a pipeline and are executed sequentially.
# Each block has a task that defines one or more jobs. Jobs define the
# commands to execute.
# See
  - name: "Install Dependencies"
        - name: APP_ENV
          value: prod
        - name: composer
            # Checkout code from Git repository. This step is mandatory if the
            # job is to work with your code.
            # Optionally you may use --use-cache flag to avoid roundtrip to
            # remote repository.
            # See
            - checkout
            # Try and find a cached version of our /vendor dependencies folder.
            # Semaphore tries to find a partial match.
            # Read about caching:
            - cache restore
            # Install our project composer
            - composer install
            # Install node dependencies
            - npm install
            # Store the /vendor and node_modules folders into cache for later use.
            - cache store
            # We are setting up the .env file from our example file which contains Semaphore DB data and proper app URL
            - cp .env.example .env
            # We need to generate an application key for Laravel to work.
            - php artisan key:generate

  - name: "Run Code Analysis"
          - checkout
          - cache restore
        - name: phpmd
            # Run the PHP Mess Detector from our project dependency binary
            - php vendor/bin/phpmd app/ text phpmd_ruleset.xml
        - name: phpcs
            # Run the PHP Code Sniffer from our project dependency binary
            - php vendor/bin/phpcs app --report-full --standard=PSR2
        - name: phpcpd
            # Run the PHP Copy Paste Detector from online repository.
            - curl -L -o phpcpd.phar
            - php phpcpd.phar app/ --min-lines=50

  - name: "Run Unit tests"
      - name: phpunit
          - checkout
          - cache restore
          # Run the unit tests from the phpunit binary in vendor folder
          - ./vendor/bin/phpunit

  - name: "Run Browser tests"
        - name: laravel dusk
            - checkout
            - cp .env.example .env
            # Create an empty .sqlite DB
            - touch database/database.sqlite
            - cache restore
            # Create an application key again.
            - php artisan key:generate
            - php artisan dusk:update --detect
            # Start Laravel's built-in web server so the web driver used by Dusk can connect.
            # We start the server using the .env.dusk.local environment file that uses SQLITE.
            - php artisan serve --env=dusk.local --port=8010 &
            # Run the tests
            - php artisan dusk

  - name: "Run Security Tests"
        - name: sensiolabs
            - checkout
            # We clone the Security Checker repository, and cd into it.
            - git clone
            - cd security-checker
            # Install Secuirity Checker dependencies (not to be confused by our project dependencies)
            - composer install
            # Finally, run the check
            - php security-checker security:check ../composer.lock

Run the demo Laravel project yourself#

A good way to start using Semaphore is to take a demo project and run it yourself. Here’s how to build the demo project with your own account:

  1. Fork the project on GitHub to your own account.
  2. Clone the repository on your local machine.
  3. In Semaphore, follow the link in the sidebar to create a new project. Follow the instructions to install sem CLI, connect it to your organization.
  4. Run sem init inside your repository.
  5. Edit the .semaphore/semaphore.yml file and make a commit. When you push a commit to GitHub, Semaphore will run the CI pipeline.

Next steps#

Congratulations! You have set up your first PHP continuous integration project on Semaphore. Here’s some recommended reading:

See also#