setup-php/README.md
2020-06-03 01:56:43 +05:30

25 KiB
Raw Blame History

Setup PHP in GitHub Actions

Setup PHP in GitHub Actions

GitHub Actions status Codecov Code Coverage LICENSE PHP Versions Supported

Setup PHP with required extensions, php.ini configuration, code-coverage support and various tools like composer in GitHub Actions. This action gives you a cross platform interface to setup the PHP environment you need to test your application. Refer to Usage section and examples to see how to use this.

Contents

🎉 PHP Support

PHP Version Stability Release Support Runner Support
5.3 Stable End of life GitHub
5.4 Stable End of life GitHub
5.5 Stable End of life GitHub
5.6 Stable End of life GitHub, self-hosted
7.0 Stable End of life GitHub, self-hosted
7.1 Stable End of life GitHub, self-hosted
7.2 Stable Security fixes only GitHub, self-hosted
7.3 Stable Active GitHub, self-hosted
7.4 Stable Active GitHub, self-hosted
8.0 Experimental In development GitHub, self-hosted

Note: Specifying 8.0 in php-version input installs a nightly build of PHP 8.0.0-dev with PHP JIT, Union Types v2 and other new features. See experimental setup for more information.

☁️ OS/Platform Support

Virtual environment matrix.operating-system
Windows Server 2019 windows-latest or windows-2019
Ubuntu 18.04 ubuntu-latest or ubuntu-18.04
Ubuntu 16.04 ubuntu-16.04
MacOS X Catalina 10.15 macos-latest or macos-10.15
Self Hosted self-hosted

Note: Refer to the self-hosted setup to use the action on self-hosted runners.

PHP Extension Support

  • On ubuntu by default extensions which are available as a package can be installed. PECL extensions if not available as a package can be installed by specifying pecl in the tools input.
uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'
  tools: pecl
  extensions: swoole
  • On windows PECL extensions which have the DLL binary can be installed.

  • On macOS PECL extensions can be installed.

  • Extensions installed along with PHP if specified are enabled.

  • Specific versions of PECL extensions can be installed by suffixing the version. This is useful for installing old versions of extensions which support end of life PHP versions.

uses: shivammathur/setup-php@v2
with:
  php-version: '5.4'
  tools: pecl
  extensions: swoole-1.9.3
  • Pre-release versions of PECL extensions can be setup by suffixing the extension with its state i.e alpha, beta, devel or snapshot.
uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'
  tools: pecl
  extensions: xdebug-beta
  • Non-default extensions can be removed by prefixing it with a :.
uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'  
  extensions: :opcache
  • Extensions which cannot be added or removed gracefully leave an error message in the logs, the action is not interrupted.

  • These extensions have custom support - gearman on ubuntu, blackfire, phalcon3 and phalcon4 on all supported OS.

🔧 Tools Support

These tools can be setup globally using the tools input.

blackfire, blackfire-player, codeception, composer, composer-prefetcher, cs2pr, deployer, flex, infection, pecl, phan, phinx, phive, phpcbf, phpcpd, php-config, php-cs-fixer, phpcs, phpize, phpmd, phpstan, phpunit, prestissimo, psalm, symfony, vapor-cli

uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'
  tools: php-cs-fixer, phpunit

To setup a particular version of a tool, specify it in this form tool:version.
Version should be in semver format and a valid release of the tool.

uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'
  tools: php-cs-fixer:2.15.5, phpunit:8.5.1

Notes

  • composer is setup by default.
  • Specifying version for composer and pecl has no effect, latest versions of both tools which are compatible with the PHP version will be setup.
  • Both agent and client will be setup when blackfire is specified.
  • If the version specified for the tool is not in semver format, latest version of the tool will be setup.
  • Tools which cannot be setup gracefully leave an error message in the logs, the action is not interrupted.

📶 Coverage support

Xdebug

Specify coverage: xdebug to use Xdebug.
Runs on all PHP versions supported except 8.0.

uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'
  coverage: xdebug

PCOV

Specify coverage: pcov to use PCOV and disable Xdebug.
It is much faster than Xdebug.
PCOV needs PHP >= 7.1.
If your source code directory is other than src, lib or, app, specify pcov.directory using the ini-values input.

uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'
  ini-values: pcov.directory=api #optional, see above for usage.
  coverage: pcov

Disable Coverage

Specify coverage: none to remove both Xdebug and PCOV.
Consider disabling the coverage using this PHP action for these reasons.

  • You are not generating coverage reports while testing.
  • It will remove Xdebug, which will have a positive impact on PHP performance.
  • You are using phpdbg for running your tests.
  • You are profiling your code using blackfire.
uses: shivammathur/setup-php@v2
with:
  php-version: '7.4'
  coverage: none

📝 Usage

Inputs

php-version (required)

  • Specify the PHP version you want to setup.
  • Accepts a string. For example '7.4'.
  • See PHP support for supported PHP versions.

extensions (optional)

  • Specify the extensions you want to add or remove.
  • Accepts a string in csv-format. For example mbstring, :opcache.
  • Non-default extensions prefixed with : are removed.
  • See PHP extension support for more info.

ini-values (optional)

  • Specify the values you want to add to php.ini.
  • Accepts a string in csv-format. For example post_max_size=256M, short_open_tag=On.

coverage (optional)

  • Specify the code coverage driver you want to setup.
  • Accepts xdebug, pcov or none.
  • See coverage support for more info.

tools (optional)

  • Specify the tools you want to setup.
  • Accepts a string in csv-format. For example phpunit, phpcs
  • See tools Support for tools supported.

See below for more info.

Basic Setup

Setup a particular PHP version.

steps:
- name: Checkout
  uses: actions/checkout@v2

- name: Setup PHP
  uses: shivammathur/setup-php@v2
  with:
    php-version: '7.4'
    extensions: mbstring, intl
    ini-values: post_max_size=256M, short_open_tag=On
    coverage: xdebug    
    tools: php-cs-fixer, phpunit

Matrix Setup

Setup multiple PHP versions on multiple operating systems.

jobs:
  run:
    runs-on: ${{ matrix.operating-system }}
    strategy:
      matrix:
        operating-system: [ubuntu-latest, windows-latest, macos-latest]
        php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4']
    name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php-versions }}
        extensions: mbstring, intl
        ini-values: post_max_size=256M, short_open_tag=On
        coverage: xdebug        
        tools: php-cs-fixer, phpunit

Experimental Setup

Setup a nightly build of PHP 8.0.0-dev from the master branch of PHP.

  • This version is currently in development and is an experimental feature on this action.
  • PECL is installed by default with this version on ubuntu.
  • Some extensions might not support this version currently.
  • Refer to this RFC for configuring PHP JIT on this version.
  • Refer to this list of RFCs implemented in this version.
steps:
- name: Checkout
  uses: actions/checkout@v2

- name: Setup PHP
  uses: shivammathur/setup-php@v2
  with:
    php-version: '8.0'
    extensions: mbstring
    ini-values: opcache.jit_buffer_size=256M, opcache.jit=1235, pcre.jit=1
    coverage: pcov
    tools: php-cs-fixer, phpunit

Self Hosted Setup

Setup PHP on a self-hosted runner.

  • PHP 5.6 and newer versions are supported on self-hosted runners.
  • Windows 7 and newer, Windows Server 2012 R2 and newer, Ubuntu 18.04, Ubuntu 16.04 and MacOS X Catalina 10.15 are supported.
  • To setup a dockerized self-hosted runner, refer to this guide to setup in an Ubuntu container and refer to this guide to setup in a Windows container.
  • To setup the runner directly on the host OS or in a VM, follow this requirements guide before setting up the self-hosted runner.

Specify the environment variable runner with the value self-hosted. Without this your workflow will fail.

jobs:
  run:
    runs-on: self-hosted
    strategy:
      matrix:        
        php-versions: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0']
    name: PHP ${{ matrix.php-versions }}
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php-versions }}
      env:
        runner: self-hosted # Specify the runner.

Local Testing Setup

Test your Ubuntu workflow locally using nektos/act.

jobs:
  run:
    runs-on: ubuntu-latest
    name: PHP 7.4 Test
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: 7.4

Run the workflow locally with act using shivammathur/node docker image.

# For runs-on: ubuntu-latest
act -P ubuntu-latest=shivammathur/node:latest

# For runs-on: ubuntu-18.04
act -P ubuntu-18.04=shivammathur/node:bionic

# For runs-on: ubuntu-16.04
act -P ubuntu-16.04=shivammathur/node:xenial

Thread Safe Setup

Setup both TS and NTS PHP on Windows.

  • NTS versions are setup by default.
  • On Ubuntu and macOS only NTS versions are supported.
  • On Windows both TS and NTS versions are supported.
jobs:
  run:
    runs-on: windows-latest
    name: Setup PHP TS on Windows
    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '7.4'
      env:
        phpts: ts # specify ts or nts

Force Update

Update to latest patch of PHP versions.

  • Pre-installed PHP versions on the GitHub Actions runner are not updated to their latest patch release by default.
  • You can specify the update environment variable to true to force update to the latest release.
- name: Setup PHP
  uses: shivammathur/setup-php@v2
  with:
    php-version: '7.4'
  env:
    update: true # specify true or false

Verbose Setup

Debug your workflow

To debug any issues, you can use the verbose tag instead of v2.

- name: Setup PHP
  uses: shivammathur/setup-php@verbose
  with:
    php-version: '7.4'

Cache Extensions

You can cache PHP extensions using shivammathur/cache-extensions and action/cache GitHub Actions. Extensions which take very long to setup when cached are available in the next workflow run and are enabled directly. This reduces the workflow execution time.

runs-on: ${{ matrix.operating-system }}
strategy:
  matrix:
    operating-system: [ubuntu-latest, windows-latest, macos-latest]
    php-versions: ['7.2', '7.3', '7.4']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
env:
  extensions: intl, pcov
  key: cache-v1 # can be any string, change to clear the extension cache.
steps:
- name: Checkout
  uses: actions/checkout@v2

- name: Setup cache environment
  id: cache-env
  uses: shivammathur/cache-extensions@v1
  with:
    php-version: ${{ matrix.php-versions }}
    extensions: ${{ env.extensions }}
    key: ${{ env.key }}

- name: Cache extensions
  uses: actions/cache@v2
  with:
    path: ${{ steps.cache-env.outputs.dir }}
    key: ${{ steps.cache-env.outputs.key }}
    restore-keys: ${{ steps.cache-env.outputs.key }}

- name: Setup PHP
  uses: shivammathur/setup-php@v2
  with:
    php-version: ${{ matrix.php-versions }}
    extensions: ${{ env.extensions }}

Note: If you setup both TS and NTS PHP versions on windows, add ${{ env.phpts }} to key and restore-keys inputs in actions/cache step.

Cache Composer Dependencies

If your project uses composer, you can persist composer's internal cache directory. Dependencies cached are loaded directly instead of downloading them while installation. The files cached are available across check-runs and will reduce the workflow execution time.

- name: Get composer cache directory
  id: composer-cache
  run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
  uses: actions/cache@v2
  with:
    path: ${{ steps.composer-cache.outputs.dir }}
    key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
    restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
  run: composer install --prefer-dist

Notes

  • Please do not cache vendor directory using action/cache as that will have side-effects.
  • In the above example, if you support a range of composer dependencies and do not commit composer.lock, you can use the hash of composer.json as the key for your cache.
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}

Cache Node.js Dependencies

If your project has node.js dependencies, you can persist npm's or yarn's cache directory. The cached files are available across check-runs and will reduce the workflow execution time.

- name: Get node.js cache directory
  id: node-cache-dir
  run: echo "::set-output name=dir::$(npm config get cache)" # Use $(yarn cache dir) for yarn

- name: Cache dependencies
  uses: actions/cache@v2
  with:
    path: ${{ steps.node-cache-dir.outputs.dir }}
    key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} # Use '**/yarn.lock' for yarn
    restore-keys: ${{ runner.os }}-node-

Note: Please do not cache node_modules directory as that will have side-effects.

Composer GitHub OAuth

If you have a number of workflows which setup multiple tools or have many composer dependencies, you might hit the GitHub's rate limit for composer. To avoid that you can add a OAuth token to the composer's config by setting COMPOSER_TOKEN environment variable. You can use GITHUB_TOKEN secret for this purpose.

- name: Setup PHP
  uses: shivammathur/setup-php@v2
  with:
    php-version: '7.4'
  env:
    COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Problem Matchers

PHP

Setup problem matchers for your PHP output by adding this step after the setup-php step. This will scan the logs for PHP errors and warnings, and surface them prominently in the GitHub Actions UI by creating annotations and log file decorations.

- name: Setup problem matchers for PHP
  run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

PHPUnit

Setup problem matchers for your PHPUnit output by adding this step after the setup-php step. This will scan the logs for failing tests and surface that information prominently in the GitHub Actions UI by creating annotations and log file decorations.

- name: Setup problem matchers for PHPUnit
  run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

Other Tools

For tools that support checkstyle reporting like phpstan, psalm, php-cs-fixer and phpcs you can use cs2pr to annotate your code.
For examples refer to cs2pr documentation.

Here is an example with phpstan.

- name: Setup PHP
  uses: shivammathur/setup-php@v2
  with:
    php-version: '7.4'
    tools: cs2pr, phpstan

- name: PHPStan
  run: phpstan analyse src --error-format=checkstyle | cs2pr

Examples

Examples for setting up this GitHub Action with different PHP Frameworks/Packages.

Framework/Package Runs on Workflow
Blackfire macOS, ubuntu and windows blackfire.yml
Blackfire Player macOS, ubuntu and windows blackfire-player.yml
CakePHP with MySQL and Redis ubuntu cakephp-mysql.yml
CakePHP with PostgreSQL and Redis ubuntu cakephp-postgres.yml
CakePHP without services macOS, ubuntu and windows cakephp.yml
CodeIgniter macOS, ubuntu and windows codeigniter.yml
Laravel with MySQL and Redis ubuntu laravel-mysql.yml
Laravel with PostgreSQL and Redis ubuntu laravel-postgres.yml
Laravel without services macOS, ubuntu and windows laravel.yml
Lumen with MySQL and Redis ubuntu lumen-mysql.yml
Lumen with PostgreSQL and Redis ubuntu lumen-postgres.yml
Lumen without services macOS, ubuntu and windows lumen.yml
Phalcon with MySQL ubuntu phalcon-mysql.yml
Phalcon with PostgreSQL ubuntu phalcon-postgres.yml
Roots/bedrock ubuntu bedrock.yml
Roots/sage ubuntu sage.yml
Slim Framework macOS, ubuntu and windows slim-framework.yml
Symfony with MySQL ubuntu symfony-mysql.yml
Symfony with PostgreSQL ubuntu symfony-postgres.yml
Symfony without services macOS, ubuntu and windows symfony.yml
Yii2 Starter Kit with MySQL ubuntu yii2-mysql.yml
Yii2 Starter Kit with PostgreSQL ubuntu yii2-postgres.yml
Zend Framework macOS, ubuntu and windows zend-framework.yml

📜 License

The scripts and documentation in this project are released under the MIT License. This project has multiple dependencies. Their licenses can be found in their respective repositories.

👍 Contributions

Contributions are welcome! See Contributor's Guide. If you face any issues while using this or want to suggest a feature/improvement, create an issue here.

💖 Support This Project

If this action helped you.

  • To support this project subscribe on Patreon or sponsor using Paypal.
  • Please star the project and share it with the community.
  • If you blog, write about your experience of using this action.
  • If you need any help using this, please contact me using Codementor

🔖 Dependencies

📑 Further Reading