PHP と Bitbucket Pipelines

このガイドでは、Bitbucket Pipelines を使用して、Docker コンテナで PHP を使用するソフトウェア プロジェクトをビルドおよびテストする方法について説明します。

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.

手動でセットアップしたい場合、構成の大部分は Pipelines がビルドを定義するために使用する bitbucket-pipelines.yml ファイル内で行います。

Docker で PHP のバージョンを指定する

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. 

たとえば、次のようにbitbucket-pipelines.yml ファイルの先頭で指定して、PHP 7.1.1 を使用できます。

bitbucket-pipelines.yml の例

image: php:7.1.1 pipelines: default: - step: script: - php -v

異なるバージョンの PHP を使用したい場合、Docker イメージのタグを変更します。次の例は、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/.

アトラシアンのオンライン バリデーターbitbucket-pipelines.ymlファイルを確認できます。

依存関係をインストールする

Composer を使用して依存関係を管理する

ビルド スクリプトの一環として Composer をインストールおよび使用できます。

たとえば、次の手順をコピーして bitbucket-pipelines.yml ファイルに貼り付けると、依存関係として monolog が指定されます。

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

Pear を使用して依存関係を管理する

Pear は PHP Docker イメージにバンドルされており、次のようにビルド スクリプトで直接使用して依存関係をインストールできます。

bitbucket-pipelines.yml の例

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

PHP 拡張機能のインストールと有効化

The PHP Docker images come with 3 helper script commands that make installing and configuring extensions easier:

  • docker-php-ext-configure: このコマンドにより、エクステンションのカスタム引数の指定が可能になります。

  • docker-php-ext-install: このコマンドを使用してコンテナに新しいエクステンションをインストールします。

  • docker-php-ext-enable: このコマンドを使用して PHP エクステンションを有効化します。

たとえば、ビルド プロセスの一部として gd エクステンションをインストールする場合、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

PECL および docker-php-ext-enable ヘルパー スクリプトを使用してエクステンションをインストールすることもできます。

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 では、サービスを定義して適切な段階でインスタンス化することで、パイプラインの実行中に追加のサービスを起動できます。

お好みのデータベースで利用を開始するのに役立つよう、bitbucket-pipeline.ymlの一覧を用意しています。

テスト

PHPUnit でのテスト

Composer を使用して PHPUnit をインストールし、テストを実行できます。PHPUnit の依存関係がリポジトリにチェックインされたことを示す composer.json ファイルがある場合、composer install を実行してから phpunit コマンドを呼び出すだけでテストを実行できます。

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

ビルド スクリプトで PHPUnit を要求することで、composer.json ファイルなしで PHPUnit をインストールすることもできます。

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

bitbucket-pipelines.ymlファイルはアトラシアンのオンライン バリデーターで確認できることを覚えておいてください。

 

さらにヘルプが必要ですか?

アトラシアン コミュニティをご利用ください。