AWS CodePipeline 与 CodeBuild 和批处理版本集成 - AWS CodeBuild
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

AWS CodePipeline 与 CodeBuild 和批处理版本集成

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

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

具有各个构件的批处理构建

使用以下 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": "AWS 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_artifactIdentifier (针对辅助构件)。

例如,对于输出构件名称 build1,CodeBuild 会将主构件 build1 上传到 build1 的位置。 对于输出名称 build1_artifact1,CodeBuild 会将辅助构件 artifact1build1 上传到 build1_artifact1 的位置,以此类推。如果只指定一个输出位置,名称应为 buildIdentifier 仅限 。

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

使用组合构件进行批量生成

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