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:

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:

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/.

Installing dependencies

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:

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 -V
          - composer install

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:

image: php:7.1.1
pipelines:
  default:
    - step:
        script:
          - pear -V
          - pear install pear/PHP_CodeSniffer

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:

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:

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

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

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:

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:

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

Was this helpful?

Thanks for your feedback!

Why was this unhelpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport