setup-php

repository·main·Indexed 25 days ago

https://github.com/shivammathur/setup-php

A GitHub Action for automating the installation and configuration of PHP environments. It supports PHP versions 5.3 to 8.6 across Ubuntu, Windows, and macOS, allowing users to install specific extensions, global tools like Composer and PHPUnit, and configure code coverage drivers such as Xdebug and PCOV.

Tokens
8K
Snippets
22
Records
36
Agent score
84%

What's inside setup-php

  1. Overview of setup-php

    main
    The setup-php GitHub Action provides a cross-platform interface to set up PHP environments in GitHub Actions workflows. It allows you to install specific PHP versions, required extensions, and configure php.ini. It also supports tools like Composer and provides built-in support for code coverage (Xdebug, PCOV) and various debugging/testing configurations.
  2. Configure `intl` extension with specific ICU versions

    main

    For PHP 5.6 and above on Ubuntu, you can specify a specific ICU version for the intl extension by suffixing intl with the version number.

    • For PHP 8.4 and lower: Use ICU 50.2 or newer.
    • For PHP 8.5 and above: Use ICU 57.2 or newer.
    - name: Setup PHP with intl
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        extensions: intl-77.1
  3. Configure Xdebug for code coverage

    main

    To use Xdebug for code coverage and disable PCOV, set the coverage input to xdebug. This works on all supported PHP versions. By default, the latest compatible version of Xdebug is installed.

    If you specifically require Xdebug 2.x for older PHP versions (7.2, 7.3, or 7.4), use coverage: xdebug2.

    - name: Setup PHP with Xdebug
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        coverage: xdebug
  4. Setup PHP with JIT (Just-in-time) compilation

    main

    To enable JIT on PHP 8.0 and above, you must enable opcache in cli mode and set coverage: none (as JIT conflicts with Xdebug/PCOV). Use ini-values to configure JIT parameters.

    - name: Setup PHP with JIT in tracing mode
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        coverage: none
        ini-values: opcache.enable_cli=1, opcache.jit=tracing, opcache.jit_buffer_size=64M
  5. Configure PCOV for code coverage

    main

    To use PCOV for code coverage and disable Xdebug, set the coverage input to pcov. This is supported on PHP 7.1 and newer.

    If your source code is located in a directory other than src, lib, or app, you must specify the directory using the ini-values input with the pcov.directory key.

    PHPUnit Compatibility:

    • PHPUnit 8.x+ supports PCOV out of the box.
    • For PHPUnit 5.x, 6.x, or 7.x, you must install and run pcov/clobber before executing tests.
    - name: Setup PHP with PCOV
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        ini-values: pcov.directory=api #optional, see above for usage.
        coverage: pcov
  6. Install PHP extensions using the `extensions` input

    main

    You can install PHP extensions by providing a comma-separated list to the extensions input.

    • Ubuntu: Supports extensions available as packages, on PECL, or via git repositories.
    • macOS: Supports extensions available on PECL or via git repositories.
    • Windows: Supports extensions available on PECL that have a DLL binary.

    To install a specific version of a PECL extension, suffix the name with the version (e.g., extension-1.2.3). For pre-release versions, use suffixes like alpha, beta, devel, or snapshot (e.g., extension-beta).

    - name: Setup PHP with PECL extension
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        extensions: imagick, redis
  7. Disable PHP extensions

    main

    You can disable specific shared extensions by prefixing their name with a colon (:). This also disables any extensions that depend on the specified one.

    To disable all shared extensions, use the keyword none. If you use none along with other extensions, none is processed first, and then your requested extensions are enabled. It is recommended to list required extensions after none to ensure they are available for your tools.

    # Disable a specific extension
    - name: Setup PHP and disable mbstring
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        extensions: :mbstring
    
    # Disable all shared extensions except mbstring
    - name: Setup PHP without any shared extensions except mbstring
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        extensions: none, mbstring
  8. Authenticate Composer with private repositories

    main

    You can authenticate private Composer dependencies using several methods:

    1. GitHub/Private Packagist: Use the github-token input for GitHub or PACKAGIST_TOKEN env var for Private Packagist.
    2. Manual JSON: Use the COMPOSER_AUTH_JSON environment variable to provide credentials in JSON format.

    Note: COMPOSER_TOKEN and GITHUB_TOKEN environment variables are deprecated in favor of the github-token input.

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        github-token: ${{ secrets.YOUR_PAT_TOKEN }}
      env:
        COMPOSER_AUTH_JSON: |
          {
            "http-basic": {
              "example.org": {
                "username": "${{ secrets.EXAMPLE_ORG_USERNAME }}",
                "password": "${{ secrets.EXAMPLE_ORG_PASSWORD }}"
              }
            }
          }
  9. Install global PHP tools

    main

    Use the tools input to install PHP tools globally. It accepts a comma-separated list of supported tools or Composer packages.

    • Standard Tools: Includes phpunit, phpstan, php-cs-fixer, etc.
    • Composer Packages: Any package on Packagist can be installed using the vendor/package format.
    • Version Control: Specify versions using Semver (tool:1.2.3), Major (tool:1), or Major/Minor (tool:1.2).
    • Checksum Pinning: For tools downloaded as phar archives, you can harden against supply chain attacks by pinning to a checksum: tool:version@sha256:<hash> or tool:version@sha512:<hash>.
    # Install specific tools
    - name: Setup PHP with tools
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        tools: php-cs-fixer, phpunit
    
    # Install a Composer package globally
    - name: Setup PHP with tools
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        tools: vimeo/psalm
    
    # Pin a tool to a checksum
    - name: Setup PHP with tools pinned to a checksum
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        tools: composer:2.9.8@sha256:59b2c50e10cafa0d8efc19ede9a326d782f096c674a26baf98cf042ce23de890
  10. Setup PHP with debugging symbols

    main

    To set up a PHP build with debugging symbols (for PHP 5.6 and above), set the debug environment variable to true.

    steps:
    - name: Setup PHP with debugging symbols
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
      env:
        debug: true # specify true or false
  11. Disable code coverage drivers

    main

    To disable both Xdebug and PCOV, set the coverage input to none.

    It is recommended to disable coverage if:

    • You are not generating coverage reports.
    • You are using phpdbg for tests.
    • You are profiling code with blackfire.
    • You are using PHP in JIT mode.
    - name: Setup PHP with no coverage driver
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
        coverage: none
  12. Set up PHP problem matchers

    main

    To surface errors and warnings from PHP, PHPUnit, or other tools directly in the GitHub Actions UI, add the corresponding problem matcher step after setup-php.

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.5'
    
    - name: Setup problem matchers for PHP
      run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
    
    - name: Setup problem matchers for PHPUnit
      run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"