PHP with Bitbucket Pipelines
Here's how you can configure the bitbucket-pipelines.yml file to build and test a PHP project.
Specify your PHP version with Docker
Bitbucket Pipelines runs all your builds in Docker containers using an image that you specify at the beginning of your configuration file. You can easily use PHP with Bitbucket Pipelines by using one of the official PHP Docker images on Docker Hub.
For instance, you can use PHP 7.1.1 by specifying it at the beginning of your bitbucket-pipelines.yml file like this:
If you want to use a different version of PHP you simply need to change the tag for the Docker image. For example, here's how you would start a container with PHP 5.6.30:
You can find a list of all supported PHP versions and corresponding image tags at https://hub.docker.com/_/php/.
Managing dependencies with Composer
You can simply install and use Composer as part of your build script by copying and pasting the following instructions to your bitbucket-pipelines.yml file:
Managing dependencies with Pear
Pear comes bundled with the PHP Docker images and you can use it directly in your build script to install dependencies:
Installing and enabling PHP extensions
The PHP Docker images come with 3 helper script commands that make installing and configuring extensions easier:
docker-php-ext-configure: This command allows you to provide the custom arguments for an extension.
docker-php-ext-install: Use this command to install new extensions in your container.
docker-php-ext-enable: This command can be used to enable PHP extensions.
For instance if you wanted to install the
gd extension as part of your build process you could do so with the following bitbucket-pipelines.yml:
You can also use PECL and the
docker-php-ext-enable helper script to install extension:
Working with databases
It is not yet possible to use multiple containers in a running pipeline, but you can use or create a Docker image that contains both PHP and the required database. You will find below some examples of images that you can re-use or adapt to meet your requirements.
Testing with PHPUnit
You can use Composer to install PHPUnit and run your test. If you have a composer.json file mentioning your PHPUnit dependency checked into your repository you just need to run
composer install before invoking the
phpunit command to execute your tests:
You can also install PHPUnit without a composer.json file by requiring it in your build script:
Was this helpful?
Thanks for your feedback!