Bitbucket Cloud の使用を開始する
Bitbucket Cloud を初めてお使いですか? 新規ユーザー用のガイドをご利用ください。
Bitbucket Pipelines では、ビルド パイプラインから複数の Docker コンテナを実行できます。アプリケーションのテストと運用で、パイプラインで追加サービスが必要になる場合、追加のコンテナを起動する必要がある場合があります。この追加サービスには、データ ストア、コード分析ツール、およびスタブ Web サービスなどが含まれます。
このような追加サービスやその他のリソースは、bitbucket-pipelines.yml ファイルの definitions セクションで定義します。これらのサービスはその後、サービスを必要とする任意のパイプラインの構成で参照されます。
パイプラインの実行時、bitbucket-pipeline.yml のステップで参照されているサービスが、パイプライン ステップ内で実行されるようにスケジューリングされます。これらのサービスは、ビルド コンテナとネットワーク アダプタを共有するほか、localhost でオープンなポートを共有します。ポート マッピングやホスト名は不要です。たとえば、Postgres を使用している場合、localhost のポート 5432 に接続するだけでテストを行えます。デバッグを行う場合、Pipelines の UI でサービスのログを確認できます。
Pipelines では、ビルド ステップあたりのサービス コンテナは最大 5 つに制限されます。サービス コンテナへのメモリの割り当て方法については以下のセクションを参照してください。
次のチュートリアルでは、サービスを定義する方法、それをパイプラインで使用する方法について説明します。
Pipelines のサービスには次の制限があります。
1 ステップあたり最大 5 サービス
Memory limits as described below
パイプライン結果のサービスやログにアクセスするための REST API は提供していません。
No mechanism to wait for service startup
If you want to run a larger number of small services, use Docker run or docker-compose
Port 29418 can’t be used
サービスは、bitbucket-pipelines.yml ファイルの definitions セクションで定義されます。
たとえば、次の例では 2 つのサービスを定義しています。redis はライブラリ イメージ redis を Docker ハブ (バージョン 3.2) から使用し、database は Docker Hub の公式 MySQL イメージ (バージョン 5.7) を使用します。
variables セクションでは、リテラル値または既存のパイプライン変数として変数を定義できます。
1
2
3
4
5
6
7
8
9
definitions:
services:
redis:
image: redis:3.2
mysql:
image: mysql:5.7
variables:
MYSQL_DATABASE: my-db
MYSQL_ROOT_PASSWORD: $password
各サービス定義では、memory キーワード (メガバイト単位) を使用してサービス コンテナーにカスタムのメモリ制限を定義することもできます。
関連するメモリ制限および既定の割り当ては次のとおりです。
標準ステップのメモリの合計は 4096 MB、大規模なビルド ステップ (サイズ: 2x を使用して定義可能) のメモリの合計は 8192 MB です。
The total memory allocated to the build step is distributed to the build container and any service containers defined in the build step. The build container executes the scripts defined in the build step. The service containers run the services, if there are any.
The remaining memory after the allocation to the service containers will be allocated to the build container (see examples below). All the memory allocated to the build step will be allocated to the build container if there is no service defined.
The build container requires a minimum 1024 MB of memory. This build container memory covers your build process and some Pipelines overhead (agent container, logging, etc). This will result in a maximum 3072/7128 MB of memory remaining for 1x/2x steps respectively to be allocated for the service containers.
Service containers get 1024 MB memory by default, but can be configured to use between 128 MB and the step maximum allowed (3072/7128 MB).
The Docker service used for operations in Pipelines has a 1024 MB default memory, but this value can be adjusted to any value between 128 MB and 3027/7128 MB by changing the memory setting on the built-in Docker service in the Definitions section. Note: all pipes require Docker service even if it is not explicitly specified.
In the example below, the build container is allocated 2048 MB of memory from the total memory available for the build step (4096 MB):
Docker service is allocated 512 MB of memory
Redis service is allocated 512 MB of memory
MySQL service allocated default memory of 1024 MB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
default:
# "Build step is allocated 4096 MB of memory"
- step:
services:
- redis
- mysql
- docker
script:
- echo "Build container is allocated 2048 MB of memory"
- echo "Services are allocated the memory configured. docker 512 MB, redis 512 MB, mysql 1024 MB"
definitions:
services:
redis:
image: redis:3.2
memory: 512
docker:
memory: 512 # reduce memory for docker-in-docker from 1GB to 512MB
mysql:
image: mysql:5.7
# memory: 1024 # default value
variables:
MYSQL_DATABASE: my-db
MYSQL_ROOT_PASSWORD: $password
In the example below, no service is being used hence the build container is allocated all the memory available for the build step (4096 MB):
1
2
3
4
5
6
default:
# "Build step is allocated 4096 MB of memory"
- step:
script:
- echo "Build container allocated 4096 MB of memory"
In the example below, pipe is treated as a service container, and has a default memory allocated of 1024 MB. The build container is allocated 3072 MB of memory from the total memory available for the build step (4096 MB):
1
2
3
4
5
6
7
8
9
10
11
12
default:
# "Build step is allocated 4096 MB of memory"
- step:
script:
- echo "Build container is allocated 3072 MB of memory"
- echo "Pipe use Docker service that use 1024 MB of memory"
- pipe: atlassian/scp-deploy:1.4.1
variables:
USER: 'ec2-user'
SERVER: '127.0.0.1'
REMOTE_PATH: '/var/www/build/'
LOCAL_PATH: '${BITBUCKET_CLONE_DIR}/*'
bitbucket-pipelines.yml ファイルの "definitions" セクションでサービスを定義したら、そのサービスを任意のパイプライン ステップで参照できます。
たとえば、次の例では redis サービスをステップで実行します。
1
2
3
4
5
6
7
8
default:
- step:
image: node
script:
- npm install
- npm test
services:
- redis
アクセスを制限するサービスを次の例のように定義できます。
1
2
3
4
5
6
services:
redis:
image:
name: redis:3.2
username: username@organisation.com
password: $DOCKER_PASSWORD
異なるレジストリやフォーマットの Docker イメージを使用するためのその他の例については、「Docker イメージをビルド環境として使用する」をご参照ください。
この bitbucket-pipelines.yml のサンプル ファイルは、サービスの定義と、パイプライン ステップでのサービスの使用方法の両方を示します。動作のブレイクダウンを後述しています。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
pipelines:
branches:
main:
- step:
image: redis
script:
- redis-cli -h localhost ping
services:
- redis
- mysql
definitions:
services:
redis:
image: redis:3.2
mysql:
image: mysql:5.7
variables:
MYSQL_DATABASE: my-db
MYSQL_ROOT_PASSWORD: $password
データベースをテストする際には、サービス コンテナを使用して、データベース サービスをリンクされたコンテナ内で実行することをおすすめします。Docker の Docker Hub では、一般的なデータベースに対応する多数の公式イメージが提供されています。
このページでは、次のデータベース タイプに接続するための bitbucket-pipelines.yml ファイルの例を示しています。
オンライン バリデーターを使用して bitbucket-pipelines.yml ファイルをチェックすることができます。
「Bitbucket Pipelines でサービスとデータベースを使用する」もご参照ください。
また、必要なデータベースを含む Docker イメージを使用することもできます。このページの「データベースを構成済みの Docker イメージを使用する」をご参照ください。
Docker Hub の Mongo イメージを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- npm test
services:
- mongo
definitions:
services:
mongo:
image: mongo
MongoDB は 127.0.0.1:27017 で認証なしで利用できます。データベースに接続すると、MongoDB が代理で作成します。
MongoDB の既定構成は IPv4 の接続のみをリッスンしますが、Mongo 接続で localhost を使用するように構成している場合、Ruby などの一部のプラットフォームは既定で IPv6 経由での通信を行うことにご注意ください。接続先として localhost ではなく 127.0.0.1 を推奨しているのはこのためです。
Docker Hub の MySQL イメージを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- npm test
services:
- mysql
definitions:
services:
mysql:
image: mysql:5.7
variables:
MYSQL_DATABASE: 'pipelines'
MYSQL_RANDOM_ROOT_PASSWORD: 'yes'
MYSQL_USER: 'test_user'
MYSQL_PASSWORD: 'test_user_password'
上述の例を使用した場合、MySQL (バージョン 5.7) を次のように利用できます。
ホスト名: 127.0.0.1 (localhost の使用は避けます。一部のクライアントがローカルの "Unix socket" 経由での接続を試みるが、これは Pipelinesでは動作しないためです。)
ポート: 3306
既定のデータベース: pipelines
ユーザー: test_user、パスワード: test_user_password(MySQL の root ユーザーにアクセスすることはできません)。
pipelines データベースにテーブルやスキームを入力する必要があります。基盤となるデータベースをさらに構成する必要がある場合、詳細について公式の Docker Hub イメージをご参照ください。
Docker Hub の MySQL イメージを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- npm test
services:
- mysql
definitions:
services:
mysql:
image: mysql:5.7
variables:
MYSQL_DATABASE: 'pipelines'
MYSQL_ROOT_PASSWORD: 'let_me_in'
上述の例を使用した場合、MySQL (バージョン 5.7) を次のように利用できます。
ホスト名: 127.0.0.1 (localhost の使用は避けます。一部のクライアントがローカルの "Unix socket" 経由での接続を試みるが、これは Pipelinesでは動作しないためです。)
ポート: 3306
既定のデータベース: pipelines
ユーザー: root、パスワード: let_me_in
pipelines データベースにテーブルやスキームを入力する必要があります。基盤となるデータベースをさらに構成する必要がある場合、詳細について公式の Docker Hub イメージをご参照ください。
Docker Hub の Postgres イメージを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- npm test
services:
- postgres
definitions:
services:
postgres:
image: postgres
PostgreSQL は localhost:5432 で、既定データベース "postgres"、ユーザー "postgres"、パスワードはなしで利用できます。postgres データベースにテーブルやスキーマのデータを入力するか、使用するデータベースを新しく作成する必要があります。基盤となるデータベース エンジンをさらに構成する必要がある場合、詳細について公式の Docker Hub イメージをご参照ください。
Docker Hub の Postgres イメージを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- npm test
services:
- postgres
definitions:
services:
postgres:
image: postgres
variables:
POSTGRES_DB: 'pipelines'
POSTGRES_USER: 'test_user'
POSTGRES_PASSWORD: 'test_user_password'
PostgreSQL は localhost:5432 で、既定データベース "pipelines"、ユーザー "test_user"、パスワード "test_user_password" で利用できます。pipelines データベースにテーブルやスキームを入力する必要があります。基盤となるデータベース エンジンをさらに構成する必要がある場合、詳細について公式の Docker Hub イメージをご参照ください。
Docker Hub の Redis イメージを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- npm test
services:
- redis
definitions:
services:
redis:
image: redis
Redis は localhost:6379 で認証なしで利用できます。
Docker Hub の Cassandra イメージを使用します。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
image: node:10.15.0
pipelines:
default:
- step:
script:
- npm install
- sleep 10 # wait for cassandra
- npm test
services:
- cassandra
definitions:
services:
cassandra:
image: cassandra
variables:
MAX_HEAP_SIZE: '512M' # Need to restrict the heapsize or else Cassandra will OOM
HEAP_NEWSIZE: '128M'
Cassandra は localhost:9042 で利用できます。
データベース用に独立したコンテナを実行する手順 (推奨手順) の代替案として、データベースをインストール済みの Docker イメージを使用できます。Node や Ruby の次のイメージはデータベースを含み、他の言語やデータベース用に拡張可能です。
また、「docker-custom」呼び出しを明示的に追加してカスタム名で「タイプ」を定義することで、docker サービスにカスタム名を使用できます。次の例をご参照ください。
カスタム名の Docker サービス:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
definitions:
services:
docker-custom:
type: docker
image: docker:dind
pipelines:
default:
- step:
runs-on:
- 'self.hosted'
- 'my.custom.label'
services:
- docker
script:
- docker info
この内容はお役に立ちましたか?