PHP と Bitbucket Pipelines

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

実際に動作するパイプラインを持つデモ リポジトリをインポートする場合、「デモ用の php リポジトリ」を参照してください。

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

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

Bitbucket Pipelines は、構成ファイルの最初に指定したイメージを使用し、すべてのビルドを Docker コンテナで実行します。Docker Hub にあるいずれかの公式 PHP Docker イメージを使用することで、Bitbucket Pipelines で簡単に PHP を使用できます。 

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

bitbucket-pipelines.yml

1 2 3 4 5 6 image: php:7.1.1 pipelines: default: - step: script: - php -v

異なるバージョンの PHP を使用したい場合、Docker イメージのタグを変更します。次の例は、PHP 5.6.30 のコンテナを開始します。

1 image: php:5.6.30

サポートされている全 PHP バージョンと対応するイメージ タグの一覧については、https://hub.docker.com/_/php/ を参照してください。

Atlassian のオンライン バリデーターを使用して、bitbucket-pipelines.yml ファイルをチェックできます。

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

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

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

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

bitbucket-pipelines.yml

1 2 3 4 5 6 7 8 9 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

1 2 3 4 5 6 7 image: php:7.1.1 pipelines: default: - step: script: - pear -V - pear install pear/PHP_CodeSniffer

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

PHP Docker イメージには拡張機能のインストールと構成を容易にする次の 3 つのヘルパー スクリプト コマンドが付属しています。

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

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

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

たとえば、ビルド プロセスの一部として gd エクステンションをインストールする場合、bitbucket-pipelines.yml を次のように構成します。

bitbucket-pipelines.yml

1 2 3 4 5 6 7 8 9 10 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

1 2 3 4 5 6 7 8 9 10 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

1 2 3 4 5 6 7 8 9 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

composer.json ファイルがない場合、ビルド スクリプトで PHPUnit を指定することによって、PHPUnit をインストールできます。

bitbucket-pipelines.yml

1 2 3 4 5 6 7 8 9 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

Atlassian のオンライン バリデーターを使用して、bitbucket-pipelines.yml ファイルを確認できます。

その他のヘルプ