PHP

This guide covers configuring PHP projects on Semaphore. If you are new to Semaphore we recommend reading our Guided tour first.

Table of contents:

Hello world

# .semaphore/semaphore.yml
version: v1.0
name: PHP example
agent:
  machine:
    type: e1-standard-2
    os_image: ubuntu1804
blocks:
  - name: Hello world
    task:
      jobs:
        - name: Run some code
          commands:
            - echo '<?php print "Hello World!\n"; ?>' > hello.php && php hello.php

Laravel example

Semaphore provides a tutorial and demo Laravel application with a working CI pipeline that you can use to get started quickly:

Supported PHP versions

Semaphore supports all versions of PHP. You have the following options:

Follow the links above for details on currently available language versions and additional tools.

Selecting a PHP version on Linux

Semaphore uses phpbrew to manage PHP versions. Any version installable with phpbrew is supported on Semaphore. Version 7.2 is pre-installed. You can install and switch versions using phpbrew and sem-version. Here's an example:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - phpbrew --no-progress install 5.6
          - sem-version php 5.6
      jobs:
        - name: Tests
          commands:
            - php --version

If the version of PHP that you need is not currently available in the Linux VM, we recommend running your jobs in a custom Docker image.

Dependency caching

Composer is preinstalled, so you can use the cache command to store and restore the vendor directory. In the following configuration example, we install dependencies and warm the cache in the first block, then use the cache in subsequent blocks.

version: "v1.0"
name: PHP Example
agent:
  machine:
    type: e1-standard-2
    os_image: ubuntu1804

blocks:
  - name: Install dependencies
    task:
      jobs:
        - name: Composer install
          commands:
            - checkout
            - cache restore composer-$SEMAPHORE_GIT_BRANCH-$(checksum composer.lock),composer-$SEMAPHORE_GIT_BRANCH,composer-master
            - composer install
            - cache store composer-$SEMAPHORE_GIT_BRANCH-$(checksum composer.lock) vendor

  - name: Tests
    task:
      prologue:
        commands:
          - checkout
          - cache restore composer-$SEMAPHORE_GIT_BRANCH-$(checksum composer.lock),composer-$SEMAPHORE_GIT_BRANCH,composer-master
          # Prepend vendor/bin to the path so you can use dependency executables
          - export "PATH=./vendor/bin:${PATH}"
      jobs:
        - name: Everything
          commands:
            - codecept test

Environment variables

Semaphore does not set specific environment variables like APP_ENV. You can set these at the task level.

blocks:
  - name: Tests
    task:
      env_vars:
        - name: APP_ENV
          value: test
      jobs:
        - name: Everything
          commands:
            - codecept run

System dependencies

Projects may need system packages to install libraries for things like database drivers. Semaphore provides full sudo access so you can install all required packages. Here's an example:

blocks:
  - name: Tests
    task:
      prologue:
        commands:
          - sudo apt-get update && sudo apt-get install -y libpq-dev
          - composer install
      jobs:
        - name: Everything
          commands:
            - codecept run

Still need help? Contact Us Contact Us