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!

  1. Import this repository by visiting bitbucket.org and log in. Then:

     

  2. On the left navigation bar, click Pipelines and scroll down to view the bitbucket-pipelines.yml file. Click Enable

     

  3. 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:

1 2 3 4 5 6 7 8 9 10 bitbucket-pipelines.yml image: php:7.1.1 pipelines: default: - step: script: - php -v

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:

1 image: 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.

Install dependencies

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:

1 2 3 4 5 6 7 8 9 10 11 12 bitbucket-pipelines.yml image: php:7.1.1 pipelines: default: - step: script: - apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng-dev - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer require monolog/monolog - composer install

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:

1 2 3 4 5 6 7 8 9 10 bitbucket-pipelines.yml image: php:7.1.1 pipelines: default: - step: script: - pear -V - pear install pear/PHP_CodeSniffer

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 bitbucket-pipelines.yml image: php:7.1.1 pipelines: default: - step: script: # Installing first the libraries necessary to configure and install gd - apt-get update && apt-get install -y libfreetype6-dev libjpeg62-turbo-dev libpng12-dev # Now we can configure and install the extension - docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ - docker-php-ext-install -j$(nproc) gd

You can also use PECL and the docker-php-ext-enable helper script to install extension:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 bitbucket-pipelines.yml image: php:7.1.1 pipelines: default: - step: script: # Installing first the libraries necessary to configure and install memcached - apt-get update && apt-get install -y libmemcached-dev zlib1g-dev # Now we can configure and install the extension - pecl install memcached-2.2.0 - docker-php-ext-enable memcached

Databases

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.

Testing

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 bitbucket-pipelines.yml image: php:7.1.1 pipelines: default: - step: script: - apt-get update && apt-get install -y unzip - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer install - vendor/bin/phpunit

You can also install PHPUnit without a composer.json file by requiring it in your build script:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 bitbucket-pipelines.yml image: php:7.1.1 pipelines: default: - step: script: - apt-get update && apt-get install -y unzip - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer require phpunit/phpunit - vendor/bin/phpunit

 

Remember, you can check your bitbucket-pipelines.yml file with our online validator.

Additional Help

Ask the Community