CodePipeline 样品 CodeBuild - AWS CodeBuild

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

CodePipeline 样品 CodeBuild

AWS CodePipeline 与集成 CodeBuild 和批量构建

AWS CodeBuild 现在支持批量构建。此示例演示 AWS CodePipeline 如何使用创建使用批量生成的生成项目。

您可以使用定义管JSON道结构的格式文件,然后将其与一起使用 AWS CLI 来创建管道。有关更多信息,请参阅《AWS CodePipeline 用户指南》中的 AWS CodePipeline 管道结构参考

使用单个构件进行批量构建

使用以下JSON文件作为工作流结构的示例,该结构使用单独的构件创建批量构建。要在中启用批量构建 CodePipeline,请将configuration对象的BatchEnabled参数设置为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

管道文件中指定的输出工件的名称必须与 buildspec JSON 文件中定义的版本和构件的标识符匹配。语法是 buildIdentifier 用于主要工件,以及 buildIdentifier_artifactIdentifier 用于次要工件。

例如,对于输出对象名称build1, CodeBuild 会将的主构件上传build1到的位置build1。对于输出名称build1_artifact1, CodeBuild 会将的次要工件artifact1上传build1到的位置build1_artifact1,依此类推。如果只指定了一个输出位置,则名称应为 buildIdentifier 只有。

创建JSON文件后,您可以创建管道。 AWS CLI 使用运行创建管道命令并将文件传递给参数。--cli-input-json有关更多信息,请参阅《AWS CodePipeline 用户指南》中的创建管道 (CLI)

使用合并的构件进行批量构建

使用以下JSON文件作为工作流结构的示例,该结构使用组合的工件创建批量构建。要在中启用批量构建 CodePipeline,请将configuration对象的BatchEnabled参数设置为true。要将构建构件合并到同一位置,请将 configuration 对象的 CombineArtifacts 参数设置为 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

如果为批量生成启用了组合工件,则只允许一个输出。 CodeBuild 会将所有版本的主要构件合并到一个ZIP文件中。

创建JSON文件后,您可以创建管道。 AWS CLI 使用运行创建管道命令并将文件传递给参数。--cli-input-json有关更多信息,请参阅《AWS CodePipeline 用户指南》中的创建管道 (CLI)

AWS CodePipeline 与多个输入源 CodeBuild 和输出构件的集成示例

一个 AWS CodeBuild 项目可以采用多个输入源。也可以创建多个输出构件。此示例演示 AWS CodePipeline 如何使用创建使用多个输入源来创建多个输出构件的构建项目。有关更多信息,请参阅 多输入源和输出构件示例

您可以使用定义管JSON道结构的格式文件,然后将其与一起使用 AWS CLI 来创建管道。使用以下JSON文件作为工作流结构的示例,该结构创建了一个包含多个输入源和多个输出构件的构建。稍后,此示例会介绍该文件如何指定多个输入和输出。有关更多信息,请参阅《AWS CodePipeline 用户指南》中的CodePipeline 管道结构参考

{ "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文件中:

  • 必须将输入源之一指定为 PrimarySource。这个源代码是 CodeBuild 查找和运行你的 buildspec 文件的目录。关键PrimarySource字用于指定JSON文件中 CodeBuild 舞台configuration部分的主要来源。

  • 每个输入源都安装在各自的目录中。此目录存储在内置环境变量 $CODEBUILD_SRC_DIR(对于主要源)和 $CODEBUILD_SRC_DIR_yourInputArtifactName(对于所有其他源)中。对于此示例中的管道,两个输入源目录为 $CODEBUILD_SRC_DIR$CODEBUILD_SRC_DIR_source2。有关更多信息,请参阅 构建环境中的环境变量

  • 管道文件中指定的输出工件的名称必须与 buildspec JSON 文件中定义的次要工件的名称相匹配。此管道使用以下 buildspec 文件。有关更多信息,请参阅 buildspec 语法

    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文件后,您可以创建管道。 AWS CLI 使用运行创建管道命令并将文件传递给参数。--cli-input-json有关更多信息,请参阅《AWS CodePipeline 用户指南》中的创建管道 (CLI)