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.

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:

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:

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 simply install and use Composer as part of your build script by copying and pasting the following instructions to your bitbucket-pipelines.yml file:

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 -V
          - 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:

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:

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:

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

データベース

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:

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:

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.

最終更新日 2017 年 8 月 2 日

この翻訳に満足しましたか?

はい
いいえ
この記事についてのフィードバックを送信する

お探しの情報が見つかりませんか?

コミュニティへの質問

Powered by Confluence and Scroll Viewport.