Skip to main content

PHP

This guide will help build PHP projects on Semaphore.

Overview

The PHP interpreter is pre-installed in the Linux Semaphore environment. You can switch the active interpreter using sem-version. You may also use Docker images.

How to select PHP version

Change the active Ruby versions on Linux with sem-version.

sem-version php 8.2.20

You can also use phpbrew to build a specific PHP version from source. This is slower than using sem-version.

phpbrew --no-progress install 8.2.20

Using Docker containers

The sem-version tool does not work on Docker containers. You must use a pre-built Docker image with the language versions you need and run the job using Docker environments.

You can use the pre-build PHP images or build your own. Find Dockerfiles to build your custom images in the semaphoreci/docker-images repository.

How to cache dependencies

Composer is preinstalled, so you can use the cache command to store and restore the vendor/ directory.

The first job in the pipeline should install and cache the dependencies:

checkout
cache restore
composer install
cache store

The rest of the jobs can now use dependencies cached in the vendor folder.

checkout
cache restore
export PATH=$PWD/vendor/bin:${PATH}

How to set up test reports

This section explains how to set up test reports (and flaky tests) for PHP and PHPUnit.

  1. Run your tests using the --log-junit argument

    phpunit --log-junit junit.xml path/to/tests
  2. Create an after_pipeline job with the following command:

    test-results publish junit.xml
Example pipeline definition
Using test reports on PHP
- name: Tests
task:
prologue:
commands:
- checkout
- cache restore
- composer install
- cache store

job:
name: "Tests"
commands:
- checkout
- cache restore
- export "PATH=./vendor/bin:${PATH}"
- phpunit --log-junit junit.xml tests/*.php

epilogue:
always:
commands:
- test-results publish junit.xml