はじめまして。小國です。
今回は、Docker Compose で構築されたプロジェクトを、Bitbucket Pipelines を使ってインテグレーションテストする方法をご紹介したいと思います。
はじめに
シーズのプロジェクトの開発環境・手法は、主に以下のような構成となっています。
- VCS に Git を使い、Bitbucket にホスティングしている
- 各プロジェクトは Docker Compose を使い、ローカル環境を構築し、開発を行っている
- PHPUnit などを使って、テストケースを書いている
Bitbucket Pipelines で Docker Compose を使うには
Bitbucket には Pipelines という CI/CD サービスがあり、Docker を使ったテストが行なえます。
ですが、Pipelines で Docker Compose を使うためには、自身で Docker Compose バイナリを作成しなければなりません。
今回は、Docker Compose バイナリの作成・公開し、サンプルプロジェクトを使って、Pipelines で PHPUnit のテストを実行するまでを行います。
やってみましょう
主な流れは以下のようになります。
- Docker Compose バイナリの作成と、作成したバイナリを Docker Hub に公開
- サンプルプロジェクトの作成
- bitbucket-pipelines.yml の作成
- Bitbucket で Pipelines の有効化
Docker Compose バイナリの作成と、作成したバイナリを Docker Hub に公開
なお、https://hub.docker.com/r/seedsstd/seeds_bitbucket_pipelines にほぼ同様のイメージを公開していますので、こちらを使用する方は、このステップは不要です。
- Docker Compose バイナリの作成
$ mkdir seeds_bitbucket_pipelines && cd seeds_bitbucket_pipelines $ cat <<EOF > Dockerfile FROM docker:stable # Add python pip and bash RUN apk add --no-cache py-pip RUN apk add --no-cache python-dev libffi-dev openssl-dev gcc libc-dev make RUN apk add --no-cache bash # Install docker-compose via pip RUN pip install --no-cache-dir docker-compose EOF
- 作成したバイナリを Docker Hub に公開
docker build -t <YOUR_ACCOUNT>/seeds_bitbucket_pipelines:stable . docker push <YOUR_ACCOUNT>/seeds_bitbucket_pipelines:stable
<YOUR_ACCOUNT>
には、自身の Docker Hub アカウント、またはオーガニゼーションを指定してください。
docker build -t seedsstd/seeds_bitbucket_pipelines:stable . docker push seedsstd/seeds_bitbucket_pipelines:stable
サンプルプロジェクトの作成
サンプルプロジェクトを作成ます。ファイル構成、内容は以下のとおりです。
$ tree . ├── composer.json ├── composer.lock ├── composer.phar ├── docker-compose.yml ├── php-apache │ └── Dockerfile └── tests └── SampleTest.php 2 directories, 6 files
- composer.json
{ "name": "seeds-std/blog_bitbucket_pipelines", "authors": [ { "name": "SEEDS Co.,Ltd", "email": "info@seeds-std.co.jp" } ], "require": {}, "require-dev": { "phpunit/phpunit": "^8.3" } }
- docker-compose.yml
version: "3" services: web: build: php-apache # php7.2-apache に git が入っていなかったため作成 volumes: - ./:/var/www/html
- php-apache/Dockerfile
FROM php:7.2-apache RUN apt-get update -y && apt-get install -y git
- tests/SampleTest.php
<?php class SampleTest extends \PHPUnit\Framework\TestCase { /** * @return void */ public function testTrueIsTrue() { $this->assertTrue(true); } }
ここでのポイントは docker-compose run --rm web bash -c "php composer.phar install && vendor/bin/phpunit tests/SampleTest.php"
というような形で、ホスト側から PHPUnit のテストが実行できることです。
ためしに、実行してテストが通ることを確認しましょう。
$ docker-compose run web /bin/bash -c "php composer.phar install && vendor/bin/phpunit tests/SampleTest.php" Do not run Composer as root/super user! See https://getcomposer.org/root for details Loading composer repositories with package information Installing dependencies (including require-dev) from lock file Nothing to install or update Generating autoload files PHPUnit 8.3.5 by Sebastian Bergmann and contributors. . 1 / 1 (100%) Time: 545 ms, Memory: 4.00 MB OK (1 test, 1 assertion)
良さそうですね。
bitbucket-pipelines.yml の作成
以下のような bitbucket-pipelines.yml を作成し、リポジトリの直下に含めます。
内容は、上記で作成した Docker Compose バイナリを使い、そのホストからコンテナを起動しテストする内容になっています。
image: seedsstd/seeds_bitbucket_pipelines:stable options: docker: true default_script: &default_script - docker-compose run --rm web /bin/bash -c "php composer.phar install && vendor/bin/phpunit tests/SampleTest.php" - docker-compose down pipelines: default: - step: script: *default_script
Pipelines の有効化
最後に、Bitbucket の画面から Pipelines を有効化します。
Pipelines を有効化すると、以下のように bitbucket-pipelines.yml で指定したテストが実行されます。
テストが全て正常に終わり、以下のようになればおkです。
最後に
Bitbucket Pipelines で Docker Compose を使用したプロジェクトのテストを実行することができました。