steps:
- name: Setup PHP with tools
uses: shivammathur/setup-php@v2
setup-php
GitHub action to set up PHP with extensions, php.ini configuration, coverage drivers, and various tools.
What is setup-php?
We can configure our PHP environments across various versions and operating systems using the setup-php action within GitHub Actions. This tool supports a wide array of PHP versions and allows for extensive customization such as adding specific PHP extensions, setting custom ini files, and integrating numerous PHP tools like Composer and PHPUnit.
This setup not only streamlines the initial configuration but also optimizes our testing and deployment processes by allowing us to run tests across multiple environments using a matrix strategy. Moreover, it supports caching for PHP extensions and Composer dependencies, which can speed up our build times considerably.
Supported Platforms
- GitHub-Hosted Runners: Supports multiple operating systems including different versions of Ubuntu, Windows Server, and macOS.
- Self-Hosted Runners: Supports a variety of operating systems with detailed guidelines for setting up PHP in a self-hosted environment. Key Features:
- PHP Versions: Ranges from PHP 5.3 to PHP 8.4, including stable, end-of-life, and nightly builds.
- PHP Extensions: Flexible configuration of PHP extensions via CSV format, with support for PECL and custom installations.
- Tools Integration: Comprehensive support for various PHP-related tools like Composer, PHPUnit, PHP CS Fixer, and more.
- Code Coverage: Options to enable code coverage using Xdebug or PCOV, including settings to disable coverage for performance optimization.
How to Configure PHP Extensions
To configure PHP extensions in your GitHub Actions workflow:
- List the extensions you need in CSV format using the
extensions
input. - To disable an extension, prefix it with a colon (:) or use 'none' to disable all shared extensions.
Example of setting up and disabling extensions:
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring, :opcache
How to Integrate and Use Tools in Your Workflow
You can integrate tools like PHPUnit or PHP CS Fixer by specifying them in the tools
input:
- List the tools in CSV format.
- If you need a specific version of a tool, specify it using the format
tool:version
.
Example of setting up tools:
steps:
- name: Setup PHP with tools
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: php-cs-fixer, phpunit
How to Manage Code Coverage with Xdebug and PCOV
To manage code coverage:
- Use
coverage
input to select between Xdebug or PCOV. - Specify
xdebug
orpcov
as needed, ornone
to disable both for performance.
Example of setting up Xdebug:
steps:
- name: Setup PHP with Xdebug
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
coverage: xdebug
How to Set Up PHP for Different Operating Systems
In your workflow, specify the operating system using the runs-on
field. Setup PHP using the shivammathur/setup-php@v2
for various OS like Ubuntu, Windows, and macOS.
Example of setting up PHP on Ubuntu:
jobs:
run:
runs-on: ubuntu-latest
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
How to Optimize Your Workflow with Caching
To optimize your workflow using caching:
- Use the GitHub Actions cache to store PHP extensions and Composer dependencies.
- Configure the cache paths and keys in your workflow to reduce setup times on subsequent runs.
Example of caching Composer dependencies:
steps:
- name: Cache Composer dependencies
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
How to Authenticate with Private Repositories
To authenticate with private repositories:
- Set the
GITHUB_TOKEN
orPACKAGIST_TOKEN
environment variables in your workflow. - Use these tokens in the
env
field of your PHP setup step for secure access to private packages.
Example of setting up authentication:
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}