Bitbucket Cloud の使用を開始する
Bitbucket Cloud を初めてお使いですか? 新規ユーザー用のガイドをご利用ください。
Bitbucket Pipelines では、ビルド パイプラインから複数の Docker コンテナを実行できます。アプリケーションのテストと運用で、パイプラインで追加サービスが必要になる場合、追加のコンテナを起動する必要がある場合があります。この追加サービスには、データ ストア、コード分析ツール、およびスタブ Web サービスなどが含まれます。
You define these additional services (and other resources) in the definitions section of the bitbucket-pipelines.yml file. These services can then be referenced in the configuration of any pipeline that needs them.
パイプラインの実行時、bitbucket-pipeline.yml のステップで参照されているサービスが、パイプライン ステップ内で実行されるようにスケジューリングされます。これらのサービスは、ビルド コンテナとネットワーク アダプタを共有するほか、localhost でオープンなポートを共有します。ポート マッピングやホスト名は不要です。たとえば、Postgres を使用している場合、localhost のポート 5432 に接続するだけでテストを行えます。デバッグを行う場合、Pipelines の UI でサービスのログを確認できます。
Pipelines では、ビルド ステップあたりのサービス コンテナは最大 5 つに制限されます。サービス コンテナへのメモリの割り当て方法については以下のセクションを参照してください。
次のチュートリアルでは、サービスを定義する方法、それをパイプラインで使用する方法について説明します。
Pipelines のサービスには次の制限があります。
1 ステップあたり最大 5 サービス
上述のメモリ制限
パイプライン結果のサービスやログにアクセスするための REST API は提供していません。
サービスの起動を待機するためのメカニズムは提供していません。
小さなサービスを大量に実行したい場合、Docker run または docker-compose を使用します。
ポート 29418 は使用できません。
Services are defined in the definitions section of the bitbucket-pipelines.yml file.
For example, the following defines two services: one named redis that uses the library image redis from Docker Hub (version 3.2), and another named database that uses the official Docker Hub MySQL image (version 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
Each service definition can also define a custom memory limit for the service container, by using the memory keyword (in megabytes).
関連するメモリ制限および既定の割り当ては次のとおりです。
標準ステップのメモリの合計は 4096 MB、大規模なビルド ステップ (サイズ: 2x を使用して定義可能) のメモリの合計は 8192 MB です。
ビルド コンテナには合計 1024 MB のメモリが割り当てられ、これでビルド プロセスと一部の Pipelines オーバーヘッド (エージェント コンテナ、ログなど) が網羅されます。
各パイプライン ステップでのサービスの合計メモリは、残りのメモリ (1x/2x ステップでそれぞれ 3072/7128 MB) を超えることはできません。
サービス コンテナは既定で 1024 MB のメモリを取得しますが、128 MB とステップの最大値 (3072/7128 MB) の間で使用量を構成できます。
Pipelines の Docker 操作で使用される Docker-in-Docker デーモンはサービス コンテナとして扱われるため、既定のメモリ制限は 1024 MB となります。この値は、definitions セクションで組み込みの docker サービスのメモリ設定を変更することで、128 MB - 3072/7128 MB の間の任意の値に調整できます。
以下の例は、ビルド コンテナに 2048 MB のメモリ制限があることを示しています。
Docker が 512 MB
Redis が 512 MB
MySQL が既定のメモリ制限 (1024 MB) を使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
default:
- step:
services:
- redis
- mysql
- docker
script:
- echo "This step is only allowed to consume 2048 MB of memory"
- echo "Services are consuming the rest. 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
bitbucket-pipelines.yml ファイルの "definitions" セクションでサービスを定義したら、そのサービスを任意のパイプライン ステップで参照できます。
For example, the following causes the redis service to run with the step:
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 will be available on 127.0.0.1:27017 without authentication. As you connect to a database, MongoDB will create it for you.
Note that MongoDB's default configuration only listens for connections on IPv4, whereas some platforms (like Ruby) default to connecting via IPv6 if your Mongo connection is configured to use localhost. This is why we recommend connecting on 127.0.0.1 rather than localhost.
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
User: test_user, password: test_user_password. (The root user of MySQL will not be accessible.)
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
この内容はお役に立ちましたか?