CodePipeline 样品 CodeBuild - Amazon CodeBuild
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

CodePipeline 样品 CodeBuild

Amazon CodePipeline 与集成 CodeBuild 和批量构建

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

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

管道的 JSON 文件中指定的输出构件的名称必须与 buildspec 文件中定义的构建和构件的标识符相匹配。主要构件的语法是 buildIdentifier,辅助构件的语法是 buildIdentifier_artifactIdentifier

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

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

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

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

您可以使用 JSON 格式的文件来定义管道的结构,然后将其与一起使用 Amazon CLI 来创建管道。使用以下 JSON 文件作为管道结构的示例,此管道结构可以创建一个具有多输入源和多输出构件的构建。稍后,此示例会介绍该文件如何指定多个输入和输出。有关更多信息,请参阅《Amazon 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。有关更多信息,请参阅构建环境中的环境变量

  • 管道的 JSON 文件中指定的输出构件的名称必须与 buildspec 文件中定义的辅助构件的名称相匹配。此管道使用以下 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 文件后,可以创建管道。 Amazon CLI 使用运行创建管道命令并将文件传递给参数。--cli-input-json有关更多信息,请参阅《Amazon CodePipeline 用户指南》中的创建管道 (CLI)