Build and test PHP apps

Azure Pipelines

Create a pipeline that continuously builds, deploys, and tests your PHP projects.

For an end-to-end walkthrough of deploying to Azure App Service on Linux with a pipeline, see Build and deploy to a PHP web app.

Create your first pipeline

Are you new to Azure Pipelines? If so, then we recommend you try this section before moving on to other sections.

Get the code

Fork this repo in GitHub:

https://github.com/MicrosoftDocs/pipelines-php

Sign in to Azure Pipelines

Sign in to Azure Pipelines. After you sign in, your browser goes to https://dev.azure.com/my-organization-name and displays your Azure DevOps dashboard.

Within your selected organization, create a project. If you don't have any projects in your organization, you see a Create a project to get started screen. Otherwise, select the Create Project button in the upper-right corner of the dashboard.

When the Configure tab appears, select PHP.

  1. When your new pipeline appears, take a look at the YAML to see what it does. When you're ready, select Save and run.

    Save and run button in a new YAML pipeline

  2. You're prompted to commit a new azure-pipelines.yml file to your repository. After you're happy with the message, select Save and run again.

    If you want to watch your pipeline in action, select the build job.

    You now have a working YAML pipeline (azure-pipelines.yml) in your repository that's ready for you to customize!

  3. When you're ready to make changes to your pipeline, select it in the Pipelines page, and then Edit the azure-pipelines.yml file.

  4. See the sections below to learn some of the more common ways to customize your pipeline.

Build environment

You can use Azure Pipelines to build your PHP projects without needing to set up any infrastructure of your own. PHP is preinstalled on Microsoft-hosted agents in Azure Pipelines, along with many common libraries per PHP version. You can use Linux, macOS, or Windows agents to run your builds.

For the exact versions of PHP that are preinstalled, refer to Microsoft-hosted agents.

Use a specific PHP version

On the Microsoft-hosted Ubuntu agent, multiple versions of PHP are installed. A symlink at /usr/bin/php points to the currently set PHP version, so that when you run php, the set version executes.

To use a PHP version other than the default, the symlink can be pointed to that version using the update-alternatives tool. Set the PHP version that you prefer by adding the following snippet to your azure-pipelines.yml file and changing the value of the phpVersion variable accordingly.

pool:
  vmImage: 'ubuntu-latest'

variables:
  phpVersion: 7.2

steps:
- script: |
    sudo update-alternatives --set php /usr/bin/php$(phpVersion)
    sudo update-alternatives --set phar /usr/bin/phar$(phpVersion)
    sudo update-alternatives --set phpdbg /usr/bin/phpdbg$(phpVersion)
    sudo update-alternatives --set php-cgi /usr/bin/php-cgi$(phpVersion)
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar$(phpVersion)
    php -version
  displayName: 'Use PHP version $(phpVersion)'

Install dependencies

To use Composer to install dependencies, add the following snippet to your azure-pipelines.yml file.

- script: composer install --no-interaction --prefer-dist
  displayName: 'composer install'

Test with phpunit

To run tests with phpunit, add the following snippet to your azure-pipelines.yml file.

- script: ./phpunit
  displayName: 'Run tests with phpunit'

Retain the PHP app with the build record

To save the artifacts of this build with the build record, add the following snippet to your azure-pipelines.yml file. Optionally, customize the value of rootFolderOrFile to alter what is included in the archive.

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(system.defaultWorkingDirectory)'
    includeRootFolder: false
- task: PublishBuildArtifacts@1

Using a custom composer location

If your composer.json is in a subfolder instead of the root directory, you can use the --working-dir argument to tell composer what directory to use. For example, if your composer.json is inside the subfolder pkgs

composer install --no-interaction --working-dir=pkgs

You can also specify the absolute path, using the built-in system variables:

composer install --no-interaction --working-dir='$(system.defaultWorkingDirectory)/pkgs'

Build image and push to container registry

For your PHP app, you can also build an image and push it to a container registry.