

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# AWS CodePipeline CodeBuild のサンプル
<a name="sample-codepipeline"></a>

このセクションでは、CodePipeline と CodeBuild 間のサンプル統合について説明します。


| サンプル | 説明 | 
| --- | --- | 
|  [CodePipeline/CodeBuild 統合とバッチビルドのサンプル](#sample-pipeline-batch)  |  これらのサンプルは、 AWS CodePipeline を使用してバッチビルドを使用するビルドプロジェクトを作成する方法を示しています。  | 
|  [複数の入力ソースおよび出力アーティファクトを持つ CodePipeline/CodeBuild の統合のサンプル](#sample-pipeline-multi-input-output)  |  このサンプルでは、 AWS CodePipeline を使用して、複数の入力ソースを使用して複数の出力アーティファクトを作成するビルドプロジェクトを作成する方法を示します。  | 

## CodePipeline/CodeBuild 統合とバッチビルドのサンプル
<a name="sample-pipeline-batch"></a>

AWS CodeBuild はバッチビルドをサポートしています。次のサンプルは、 AWS CodePipeline を使用してバッチビルドを使用するビルドプロジェクトを作成する方法を示しています。

パイプラインの構造を定義する JSON 形式のファイルを使用し、それを で使用 AWS CLI してパイプラインを作成できます。詳細については、『*AWS CodePipeline ユーザーガイド*』の「[AWS CodePipeline パイプライン構造のリファレンス](https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html)」を参照してください。

### 個々のアーティファクトを使用した Batch 構築
<a name="sample-pipeline-batch.separate-artifacts"></a>

個別のアーティファクトを含むバッチビルドを作成するパイプライン構造の例として、次の JSON ファイルを使用してください。CodePipeline でバッチビルドを有効にするには、「`BatchEnabled`」パラメータのパラメータ「`configuration`」オブジェクトを「`true`」に設定します。

```
{
  "pipeline": {
    "roleArn": "arn:aws:iam::account-id:role/my-AWS-CodePipeline-service-role-name",
    "stages": [
      {
        "name": "Source",
        "actions": [
          {
            "inputArtifacts": [],
            "name": "Source1",
            "actionTypeId": {
              "category": "Source",
              "owner": "AWS",
              "version": "1",
              "provider": "S3"
            },
            "outputArtifacts": [
              {
                "name": "source1"
              }
            ],
            "configuration": {
              "S3Bucket": "<my-input-bucket-name>",
              "S3ObjectKey": "my-source-code-file-name.zip"
            },
            "runOrder": 1
          },
          {
            "inputArtifacts": [],
            "name": "Source2",
            "actionTypeId": {
              "category": "Source",
              "owner": "AWS",
              "version": "1",
              "provider": "S3"
            },
            "outputArtifacts": [
              {
                "name": "source2"
              }
            ],
            "configuration": {
              "S3Bucket": "<my-other-input-bucket-name>",
              "S3ObjectKey": "my-other-source-code-file-name.zip"
            },
            "runOrder": 1
          }
        ]
      },
      {
        "name": "Build",
        "actions": [
          {
            "inputArtifacts": [
              {
                "name": "source1"
              },
              {
                "name": "source2"
              }
            ],
            "name": "Build",
            "actionTypeId": {
              "category": "Build",
              "owner": "AWS",
              "version": "1",
              "provider": "CodeBuild"
            },
            "outputArtifacts": [
              {
                "name": "build1"
              },
              {
                "name": "build1_artifact1"
              },
              {
                "name": "build1_artifact2"
              },
              {
                "name": "build2_artifact1"
              },
              {
                "name": "build2_artifact2"
              }
            ],
            "configuration": {
              "ProjectName": "my-build-project-name",
              "PrimarySource": "source1",
              "BatchEnabled": "true"
            },
            "runOrder": 1
          }
        ]
      }
    ],
    "artifactStore": {
      "type": "S3",
      "location": "<AWS-CodePipeline-internal-bucket-name>"
    },
    "name": "my-pipeline-name",
    "version": 1
  }
}
```

次の例は、このパイプライン設定で動作する CodeBuild buildspec ビルドファイルです。

```
version: 0.2
batch:
  build-list:
    - identifier: build1
      env:
        compute-type: BUILD_GENERAL1_SMALL
    - identifier: build2
      env:
        compute-type: BUILD_GENERAL1_MEDIUM

phases:
  build:
    commands:
      - echo 'file' > output_file

artifacts:
  files:
    - output_file
  secondary-artifacts:
    artifact1:
      files:
        - output_file
    artifact2:
      files:
        - output_file
```

パイプラインの JSON ファイルで指定されている出力成果物の名前は、buildspec ファイルで定義されているビルドおよびアーティファクトの識別子と一致していなければなりません。構文は、プライマリアーティファクトの場合は *buildIdentifier* で、セカンダリアーティファクトの場合は *buildIdentifier*\$1*artifactIdentifier* です。

たとえば、出力アーティファクト名 `build1` の場合、CodeBuild は `build1` の場所に「`build1`」を出力します。出力名は「`build1_artifact1`」であり、CodeBuild はセカンダリアーティファクトを `artifact1` の `build1`、`build1_artifact1` の場所にアップロードします。出力場所が 1 つだけ指定されている場合、名前は *buildIdentifier* のみにします。

JSON ファイルを作成したら、パイプラインを作成することができます。を使用して **create-pipeline** コマンド AWS CLI を実行し、 ファイルを `--cli-input-json`パラメータに渡します。詳細については、『*AWS CodePipeline ユーザーガイド*』の「[パイプラインの作成 (CLI)](https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-create.html#pipelines-create-cli)」を参照してください。

### 複合アーチファクトを使用したBatch ビルド
<a name="sample-pipeline-batch.combined-artifacts"></a>

結合アーティファクトを含むバッチビルドを作成するパイプライン構造の例として、次の JSON ファイルを使用してください。CodePipeline でバッチビルドを有効にするには、「`BatchEnabled`」パラメータのパラメータ「`configuration`」オブジェクトを「`true`」に設定します。ビルド成果物を同じ場所に結合するには、「`CombineArtifacts`」オブジェクトの「`configuration`」パラメータのパラメータを「`true`」に設置します。

```
{
 "pipeline": {
  "roleArn": "arn:aws:iam::account-id:role/my-AWS-CodePipeline-service-role-name",
  "stages": [
    {
      "name": "Source",
      "actions": [
        {
          "inputArtifacts": [],
          "name": "Source1",
          "actionTypeId": {
            "category": "Source",
            "owner": "AWS",
            "version": "1",
            "provider": "S3"
          },
          "outputArtifacts": [
            {
              "name": "source1"
            }
          ],
          "configuration": {
            "S3Bucket": "<my-input-bucket-name>",
            "S3ObjectKey": "my-source-code-file-name.zip"
          },
          "runOrder": 1
        },
        {
          "inputArtifacts": [],
          "name": "Source2",
          "actionTypeId": {
            "category": "Source",
            "owner": "AWS",
            "version": "1",
            "provider": "S3"
          },
          "outputArtifacts": [
            {
              "name": "source2"
            }
          ],
          "configuration": {
            "S3Bucket": "<my-other-input-bucket-name>",
            "S3ObjectKey": "my-other-source-code-file-name.zip"
          },
          "runOrder": 1
        }
      ]
    },
    {
      "name": "Build",
      "actions": [
        {
          "inputArtifacts": [
            {
              "name": "source1"
            },
            {
              "name": "source2"
            }
          ],
          "name": "Build",
          "actionTypeId": {
            "category": "Build",
            "owner": "AWS",
            "version": "1",
            "provider": "CodeBuild"
          },
          "outputArtifacts": [
            {
              "name": "output1 "
            }
          ],
          "configuration": {
            "ProjectName": "my-build-project-name",
            "PrimarySource": "source1",
             "BatchEnabled": "true",
             "CombineArtifacts": "true"
          },
          "runOrder": 1
        }
      ]
    }
  ],
  "artifactStore": {
    "type": "S3",
    "location": "<AWS-CodePipeline-internal-bucket-name>"
  },
  "name": "my-pipeline-name",
  "version": 1
 }
}
```

次の例は、このパイプライン設定で動作する CodeBuild buildspec ビルドファイルです。

```
version: 0.2
batch:
  build-list:
    - identifier: build1
      env:
        compute-type: BUILD_GENERAL1_SMALL
    - identifier: build2
      env:
        compute-type: BUILD_GENERAL1_MEDIUM

phases:
  build:
    commands:
      - echo 'file' > output_file

artifacts:
  files:
    - output_file
```

結合アーチファクトがバッチ構築で有効になっている場合、出力は 1 つだけです。CodeBuild は、すべてのビルドの主要なアーティファクトを 1 つのZIPファイルに結合します。

JSON ファイルを作成したら、パイプラインを作成することができます。を使用して **create-pipeline** コマンド AWS CLI を実行し、 ファイルを `--cli-input-json`パラメータに渡します。詳細については、『*AWS CodePipeline ユーザーガイド*』の「[パイプラインの作成 (CLI)](https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-create.html#pipelines-create-cli)」を参照してください。

## 複数の入力ソースおよび出力アーティファクトを持つ CodePipeline/CodeBuild の統合のサンプル
<a name="sample-pipeline-multi-input-output"></a>

 AWS CodeBuild プロジェクトは複数の入力ソースを取ることができます。また、複数の出力アーティファクトを作成することもできます。このサンプルでは、 AWS CodePipeline を使用して、複数の入力ソースを使用して複数の出力アーティファクトを作成するビルドプロジェクトを作成する方法を示します。詳細については、「[複数の入力ソースと出力アーティファクトのサンプル](sample-multi-in-out.md)」を参照してください。

パイプラインの構造を定義する JSON 形式のファイルを使用し、それを で使用 AWS CLI してパイプラインを作成できます。複数の入力ソースと複数の出力アーティファクトを含むビルドを作成するパイプライン構造の例として、次の JSON ファイルを使用してください。このサンプルの後半では、このファイルが複数の入力と出力をどのように指定しているかが分かります。詳細については、『*AWS CodePipeline ユーザーガイド*』の「[CodePipeline パイプライン構造リファレンス](https://docs.aws.amazon.com/codepipeline/latest/userguide/reference-pipeline-structure.html)」を参照してください。

```
{
 "pipeline": {
  "roleArn": "arn:aws:iam::account-id:role/my-AWS-CodePipeline-service-role-name",
  "stages": [
    {
      "name": "Source",
      "actions": [
        {
          "inputArtifacts": [],
          "name": "Source1",
          "actionTypeId": {
            "category": "Source",
            "owner": "AWS",
            "version": "1",
            "provider": "S3"
          },
          "outputArtifacts": [
            {
              "name": "source1"
            }
          ],
          "configuration": {
            "S3Bucket": "my-input-bucket-name",
            "S3ObjectKey": "my-source-code-file-name.zip"
          },
          "runOrder": 1
        },
        {
          "inputArtifacts": [],
          "name": "Source2",
          "actionTypeId": {
            "category": "Source",
            "owner": "AWS",
            "version": "1",
            "provider": "S3"
          },
          "outputArtifacts": [
            {
              "name": "source2"
            }
          ],
          "configuration": {
            "S3Bucket": "my-other-input-bucket-name",
            "S3ObjectKey": "my-other-source-code-file-name.zip"
          },
          "runOrder": 1
        }
      ]
    },
    {
      "name": "Build",
      "actions": [
        {
          "inputArtifacts": [
            {
              "name": "source1"
            },
            {
              "name": "source2"
            }
          ],
          "name": "Build",
          "actionTypeId": {
            "category": "Build",
            "owner": "AWS",
            "version": "1",
            "provider": "AWS CodeBuild"
          },
          "outputArtifacts": [
            {
              "name": "artifact1"
            },
            {
              "name": "artifact2"
            }
          ],
          "configuration": {
            "ProjectName": "my-build-project-name",
            "PrimarySource": "source1"
          },
          "runOrder": 1
        }
      ]
    }
  ],
  "artifactStore": {
    "type": "S3",
    "location": "AWS-CodePipeline-internal-bucket-name"
  },
  "name": "my-pipeline-name",
  "version": 1
 }
}
```

 この JSON ファイルの制約事項: 
+ 入力ソースの 1 つを `PrimarySource`に指定する必要があります。このソースは、CodeBuild が buildspec ファイルを探して実行するディレクトリです。キーワード `PrimarySource` は、JSON ファイルの CodeBuild ステージの`configuration` セクションにプライマリソースを指定するのに使用されます。
+ 各入力ソースは、それぞれのディレクトリにインストールされます。このディレクトリは、組み込み環境変数 `$CODEBUILD_SRC_DIR` (プライマリソースの場合) と `$CODEBUILD_SRC_DIR_yourInputArtifactName` (他のすべてのソースの場合) に保存されます。このサンプルのパイプラインでは、2 つの入力ソースディレクトリは `$CODEBUILD_SRC_DIR` と `$CODEBUILD_SRC_DIR_source2` です。詳細については、「[ビルド環境の環境変数](build-env-ref-env-vars.md)」を参照してください。
+ パイプラインの JSON ファイルで指定されている出力成果物の名前は、buildspec ファイルで定義されているセカンダリアーティファクトの名前と一致していなければなりません。このパイプラインは、次の buildspec ファイルを使用します。詳細については、「[buildspec の構文](build-spec-ref.md#build-spec-ref-syntax)」を参照してください。

  ```
  version: 0.2
  
  phases:
    build:
      commands:
        - touch source1_file
        - cd $CODEBUILD_SRC_DIR_source2
        - touch source2_file
  
  artifacts:
    files:
      - '**/*'
    secondary-artifacts:
      artifact1:
        base-directory: $CODEBUILD_SRC_DIR
        files:
          - source1_file
      artifact2:
        base-directory: $CODEBUILD_SRC_DIR_source2
        files:
          - source2_file
  ```

 JSON ファイルを作成したら、パイプラインを作成することができます。を使用して **create-pipeline** コマンド AWS CLI を実行し、 ファイルを `--cli-input-json`パラメータに渡します。詳細については、『*AWS CodePipeline ユーザーガイド*』の「[パイプラインの作成 (CLI)](https://docs.aws.amazon.com/codepipeline/latest/userguide/pipelines-create.html#pipelines-create-cli)」を参照してください。