New to Bitbucket Cloud? Check out our get started guides for new users.
PHP with Bitbucket Pipelines
This guide shows you how to use Bitbucket Pipelines for building and testing a PHP software project in a Docker container.
If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo php repo.
Instructions to import the demo repo
For this demo, all you need to do is import this repo and enable pipelines to get your first pipeline to run!
Import this repository by visiting bitbucket.org and log in. Then:
click the plus sign
under import select Repository
give your imported repo a name
On the left navigation bar, click Pipelines and scroll down to view the bitbucket-pipelines.yml file. Click Enable
Watch your build run!
Note: Bitbucket Pipelines includes fifty free minutes per account, at the time of writing. You can check your team or account's minutes usage for the month by clicking your Avatar > Bitbucket settings > Plan details.
If you'd like to set it up by hand,most of the configuration happens in the bitbucket-pipelines.yml file that Pipelines uses to define the build.
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/.
You can check your bitbucket-pipelines.yml file with our online validator.
Manage dependencies with Composer
You can install and use Composer as part of your build script.
For example, copying and pasting the following instructions to your bitbucket-pipelines.yml file specifies monolog as a dependency:
Manage dependencies with Pear
Pear comes bundled with the PHP Docker images and you can use it directly in your build script to install dependencies:
Install and enable 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:
Bitbucket Pipelines allows you to launch extra services during the execution of your pipeline by defining the service, and instantiating it on the appropriate step.
We've compiled a list of of bitbucket-pipeline.yml examples to help get started with your favourite database.
Test 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:
Remember, you can check your bitbucket-pipelines.yml file with our online validator.
Was this helpful?