パイプライン・アーキテクチャ

パイプラインは、GitLabにおけるCI/CDの基本的な構成要素です。 このページでは、パイプラインに関連する重要な概念を説明します。

パイプラインを構成する方法は主に3つあり、それぞれに利点があります。 これらの方法は必要に応じて組み合わせることができます。

  • Basic: すべての設定がわかりやすい場所にある、単純なプロジェクトに適しています。
  • Directed Acyclic Graph:効率的な実行を必要とする大規模で複雑なプロジェクトに適しています。
  • 子/親パイプライン:モノレポや、独立して定義されたコンポーネントが多数あるプロジェクトに適しています。

以下に使用されているキーワードの詳細については、CI YAMLのリファレンスをご確認ください。

基本的なパイプライン

これは、GitLabで最もシンプルなパイプラインです。 ビルドステージのすべてを同時に実行し、それらがすべて終了するとテストステージのすべてを同じように実行します。 最も効率的とは言えず、ステップ数が多いとかなり複雑になりますが、メンテナンスは容易です。

graph LR subgraph deploy stage deploy --> deploy_a deploy --> deploy_b end subgraph test stage test --> test_a test --> test_b end subgraph build stage build --> build_a build --> build_b end build_a -.-> test build_b -.-> test test_a -.-> deploy test_b -.-> deploy

図と一致する基本的な/.gitlab-ci.ymlパイプラインの設定例。

stages:
  - build
  - test
  - deploy

image: alpine

build_a:
  stage: build
  script:
    - echo "This job builds something."

build_b:
  stage: build
  script:
    - echo "This job builds something else."

test_a:
  stage: test
  script:
    - echo "This job tests something. It will only run when all jobs in the"
    - echo "build stage are complete."

test_b:
  stage: test
  script:
    - echo "This job tests something else. It will only run when all jobs in the"
    - echo "build stage are complete too. It will start at about the same time as test_a."

deploy_a:
  stage: deploy
  script:
    - echo "This job deploys something. It will only run when all jobs in the"
    - echo "test stage complete."

deploy_b:
  stage: deploy
  script:
    - echo "This job deploys something else. It will only run when all jobs in the"
    - echo "test stage complete. It will start at about the same time as deploy_a."

有向非環状グラフのパイプライン

効率性を重視し、できるだけ早くすべてを実行したい場合は、DAG (Directed Acyclic Graph)を使うことができます。needsキーワードを使ってジョブ間の依存関係を定義します。GitLabがジョブ間の関係を知っていれば、すべてをできるだけ早く実行でき、可能であれば後続のステージにスキップできます。

下の例では、build_btest_bよりもbuild_atest_aのほうがはるかに速い場合、build_bがまだ動いていてもGitLabはdeploy_aを開始します。

graph LR subgraph Pipeline using DAG build_a --> test_a --> deploy_a build_b --> test_b --> deploy_b end

図と一致するDAG/.gitlab-ci.ymlの設定例。

stages:
  - build
  - test
  - deploy

image: alpine

build_a:
  stage: build
  script:
    - echo "This job builds something quickly."

build_b:
  stage: build
  script:
    - echo "This job builds something else slowly."

test_a:
  stage: test
  needs: [build_a]
  script:
    - echo "This test job will start as soon as build_a finishes."
    - echo "It will not wait for build_b, or other jobs in the build stage, to finish."

test_b:
  stage: test
  needs: [build_b]
  script:
    - echo "This test job will start as soon as build_b finishes."
    - echo "It will not wait for other jobs in the build stage to finish."

deploy_a:
  stage: deploy
  needs: [test_a]
  script:
    - echo "Since build_a and test_a run quickly, this deploy job can run much earlier."
    - echo "It does not need to wait for build_b or test_b."

deploy_b:
  stage: deploy
  needs: [test_b]
  script:
    - echo "Since build_b and test_b run slowly, this deploy job will run much later."

子と親のパイプライン

上の例では、独立して構築できる2種類のものがあることがわかります。 これは、トリガーキーワードによる子/親パイプライン)を使用するのに理想的なケースです。 これにより、構成が複数のファイルに分割され、非常にシンプルになります。 また、これを組み合わせることもできます。

  • ルールキーワード:例えば、そのエリアに変更があった時のみ、子パイプラインがトリガーされるようにする。
  • インクルード・キーワード:共通の行動を取り入れ、同じことの繰り返しにならないようにする。
  • DAGパイプラインを子パイプラインの内側に配置することで、両者のメリットを実現しています。
graph LR subgraph Parent pipeline trigger_a -.-> build_a trigger_b -.-> build_b subgraph child pipeline B build_b --> test_b --> deploy_b end subgraph child pipeline A build_a --> test_a --> deploy_a end end

図に一致する親パイプラインの/.gitlab-ci.yml設定例。

stages:
  - triggers

trigger_a:
  stage: triggers
  trigger:
    include: a/.gitlab-ci.yml
  rules:
    - changes:
      - a/*

trigger_b:
  stage: triggers
  trigger:
    include: b/.gitlab-ci.yml
  rules:
    - changes:
      - b/*

/a/.gitlab-ci.ymlにあるDAGneeds:キーワードを利用したパイプライン設定例です。

stages:
  - build
  - test
  - deploy

image: alpine

build_a:
  stage: build
  script:
    - echo "This job builds something."

test_a:
  stage: test
  needs: [build_a]
  script:
    - echo "This job tests something."

deploy_a:
  stage: deploy
  needs: [test_a]
  script:
    - echo "This job deploys something."

DAGneeds:キーワードを使用した、/b/.gitlab-ci.ymlにある子bパイプラインの構成例。

stages:
  - build
  - test
  - deploy

image: alpine

build_b:
  stage: build
  script:
    - echo "This job builds something else."

test_b:
  stage: test
  needs: [build_b]
  script:
    - echo "This job tests something else."

deploy_b:
  stage: deploy
  needs: [test_b]
  script:
    - echo "This job deploys something else."

また、共通のセットアップ手順や最後に統一されたデプロイメントを行う場合など、子パイプラインをトリガーする前または後にジョブを実行するよう設定可能です。