AWS CodeBuild
用户指南 (API 版本 2016-10-06)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

适用于 CodeBuild 的构建通知示例

Amazon CloudWatch Events 具有对 AWS CodeBuild 的内置支持。CloudWatch Events 是描述您的 AWS 资源中的变化的系统事件流。利用 CloudWatch Events,您可以写入声明性规则以将相关事件与要执行的自动操作关联。每当构建成功、失败、从一个构建阶段转到另一个构建阶段或出现这些事件的任意组合时,本示例都会使用 Amazon CloudWatch Events 和 Amazon Simple Notification Service (Amazon SNS) 向订阅者发送构建通知。

重要

运行此示例可能会导致您的 AWS 账户产生相关费用。这些费用包括可能针对 CodeBuild 和 AWS 资源以及与 Amazon CloudWatch 和 Amazon SNS 相关的操作收取的费用。有关更多信息,请参阅 CodeBuild 定价Amazon CloudWatch 定价Amazon SNS 定价

运行示例

要运行此示例,请:

  1. 如果您已在 Amazon SNS 中设置并订阅用于此示例的主题,请跳至第 4 步。或者,如果您通过 IAM 用户而不是 AWS 根账户或 IAM 管理员用户来使用 Amazon SNS,请向用户(或与用户关联的 IAM 组)添加以下语句(在 ### BEGIN ADDING STATEMENT HERE ###### END ADDING STATEMENT HERE ### 之间)。(不建议使用 AWS 根账户。) 此语句可用于查看、创建、订阅和测试向 Amazon SNS 中的主题发送通知的情况。为了简洁起见,也为了帮您查找添加语句的位置,此处使用了省略号 (...)。请勿删除任何语句,也不要将这些省略号键入现有策略中。

    { "Statement": [ ### BEGIN ADDING STATEMENT HERE ### { "Action": [ "sns:CreateTopic", "sns:GetTopicAttributes", "sns:List*", "sns:Publish", "sns:SetTopicAttributes", "sns:Subscribe" ], "Resource": "*", "Effect": "Allow" }, ### END ADDING STATEMENT HERE ### ... ], "Version": "2012-10-17" }

    注意

    修改该策略的 IAM 实体必须拥有在 IAM 中修改策略的权限。

    有关更多信息,请参阅编辑客户托管策略IAM User Guide使用内联策略(控制台)中的“编辑或删除组、用户或角色的内联策略”部分。

  2. 在 Amazon SNS 中创建或标识主题。AWS CodeBuild 将使用 CloudWatch Events 通过 Amazon SNS 向该主题发送构建通知。要创建主题,请执行以下操作:

    1. https://console.amazonaws.cn/sns 中打开 Amazon SNS 控制台。

    2. 选择 Create topic

    3. Create new topic (创建新主题) 对话框中,对于 Topic name (主题名称),键入主题的名称(例如 CodeBuildDemoTopic)。(如果您选择了其他名称,请用该名称替换掉本示例中对应的名称。)

    4. 选择 Create topic

    5. Topic details: CodeBuildDemoTopic (主题详细信息: CodeBuildDemoTopic) 页面上,复制 Topic ARN (主题 ARN) 值,如以下屏幕截图所示。在下一个步骤中,您需要用到此值。

    有关更多信息,请参阅 Amazon SNS 开发人员指南 中的创建主题

  3. 为一个或多个收件人订阅主题以接收电子邮件通知。为收件人订阅主题:

    1. 使用上一步中打开的 Amazon SNS 控制台,在导航窗格中,选择 Subscriptions (订阅),然后选择 Create subscription (创建订阅)

    2. Create subscription 对话框中,对于 Topic ARN,粘贴您在上一步中复制的主题 ARN。

    3. 对于 Protocol,选择 Email

    4. 对于终端节点,请键入收件人的完整电子邮件地址。将您的结果与以下屏幕截图进行比较。

    5. 选择 Create Subscription

    6. Amazon SNS 向收件人发送订阅确认电子邮件。要开始接收电子邮件通知,收件人必须在订阅确认电子邮件中选择 Confirm subscription 链接。在收件人单击该链接后,如果成功订阅,Amazon SNS 将在收件人的 Web 浏览器中显示一条确认消息。

    有关更多信息,请参阅 Amazon SNS 开发人员指南 中的订阅主题

  4. 如果您通过 IAM 用户而不是 AWS 根账户或 IAM 管理员用户来使用 CloudWatch Events,请向用户(或与用户关联的 IAM 组)添加以下语句(在 ### BEGIN ADDING STATEMENT HERE ###### END ADDING STATEMENT HERE ### 之间)。(不建议使用 AWS 根账户。) 此语句使您可以使用 CloudWatch Events。为了简洁起见,也为了帮您查找添加语句的位置,此处使用了省略号 (...)。请勿删除任何语句,也不要将这些省略号键入现有策略中。

    { "Statement": [ ### BEGIN ADDING STATEMENT HERE ### { "Action": [ "events:*", "iam:PassRole" ], "Resource": "*", "Effect": "Allow" }, ### END ADDING STATEMENT HERE ### ... ], "Version": "2012-10-17" }

    注意

    修改该策略的 IAM 实体必须拥有在 IAM 中修改策略的权限。

    有关更多信息,请参阅编辑客户托管策略IAM User Guide使用内联策略(控制台)中的“编辑或删除组、用户或角色的内联策略”部分。

  5. 在 CloudWatch Events中创建规则。为此,请通过以下网址打开 CloudWatch 控制台:https://console.amazonaws.cn/cloudwatch

  6. 在导航窗格中的 Events 下,选择 Rules,然后选择 Create rule

  7. Step 1: Create rule page 上,以下项应该已被选定:Event PatternBuild event pattern to match events by service

  8. 对于 Service Name (服务名称),选择 CodeBuild。对于 Event TypeAll Events 应该已被选定。

  9. Event Pattern Preview 应显示以下代码。

    { "source": [ "aws.codebuild" ] }

    将您迄今为止的结果与以下屏幕截图进行比较:

  10. 通过选择 Edit,将 Event Pattern Preview 中的代码替换为以下两个规则模式之一。

    每当一个构建开始或完成时,第一个规则模式就会为 AWS CodeBuild 中的指定构建项目触发一个事件。

    { "source": [ "aws.codebuild" ], "detail-type": [ "CodeBuild Build State Change" ], "detail": { "build-status": [ "IN_PROGRESS", "SUCCEEDED", "FAILED", "STOPPED" ], "project-name": [ "my-demo-project-1", "my-demo-project-2" ] } }

    在前面的规则中,根据需要更改以下代码。

    • 要在每次构建开始或完成时触发事件,请保留 build-status 阵列中显示的所有值,或删除整个 build-status 阵列。

    • 要仅在构建完成时触发事件,请从 build-status 阵列中删除 IN_PROGRESS

    • 要仅在构建开始时触发事件,请从 build-status 阵列中删除除 IN_PROGRESS 以外的所有值。

    • 要为所有构建项目触发事件,请删除整个 project-name 阵列。

    • 要仅为单个构建项目触发事件,请在 project-name 阵列中指定每个构建项目的名称。

    每当构建从一个构建阶段转到另一个构建阶段时,第二个规则模式将为 AWS CodeBuild 中的指定构建项目触发一个事件。

    { "source": [ "aws.codebuild" ], "detail-type": [ "CodeBuild Build Phase Change" ], "detail": { "completed-phase": [ "SUBMITTED", "PROVISIONING", "DOWNLOAD_SOURCE", "INSTALL", "PRE_BUILD", "BUILD", "POST_BUILD", "UPLOAD_ARTIFACTS", "FINALIZING" ], "completed-phase-status": [ "TIMED_OUT", "STOPPED", "FAILED", "SUCCEEDED", "FAULT", "CLIENT_ERROR" ], "project-name": [ "my-demo-project-1", "my-demo-project-2" ] } }

    在前面的规则中,根据需要更改以下代码。

    • 要针对所有构建阶段更改触发事件 (这可以为每个构建发送最多 9 条通知),请保留 completed-phase 数组中显示的所有值,或删除整个 completed-phase 阵列。

    • 要仅针对单个构建阶段更改触发事件,请删除 completed-phase 阵列中您不希望为其触发事件的每个构建阶段的名称。

    • 要针对所有构建阶段状态更改触发事件,请保留 completed-phase-status 阵列中显示的所有值,或删除整个 completed-phase-status 阵列。

    • 要仅针对单个构建阶段状态更改触发事件,请删除 completed-phase-status 阵列中您不希望对其触发事件的每个构建阶段状态的名称。

    • 要为所有构建项目触发事件,请删除 project-name 阵列。

    • 要为单个构建项目触发事件,请在 project-name 阵列中指定每个构建项目的名称。

    注意

    如果要同时为构建状态更改和构建阶段更改触发事件,则必须创建两个单独的规则,一个针对构建状态更改,另一个针对构建阶段更改。如果您尝试将两个规则合并为一个规则,则合并后的规则可能产生意外结果或停止协作。

    替换完代码后,选择 Save

  11. 对于 Targets,选择 Add target

  12. 在目标列表中,选择 SNS 主题

  13. 对于 Topic,选择您之前标识或创建的主题。

  14. 展开配置输入,然后选择输入转换器

  15. Input Path 框中,键入以下输入路径之一。

    对于 detail-type 值为 CodeBuild Build State Change 的规则,键入以下内容。

    {"build-id":"$.detail.build-id","project-name":"$.detail.project-name","build-status":"$.detail.build-status"}

    对于 detail-type 值为 CodeBuild Build Phase Change 的规则,键入以下内容。

    {"build-id":"$.detail.build-id","project-name":"$.detail.project-name","completed-phase":"$.detail.completed-phase","completed-phase-status":"$.detail.completed-phase-status"}

    注意

    要获取其他类型的信息,请参阅构建通知输入格式参考

  16. Input Template 框中,键入以下输入模板之一。

    对于 detail-type 值为 CodeBuild Build State Change 的规则,键入以下内容。

    "Build '<build-id>' for build project '<project-name>' has reached the build status of '<build-status>'."

    对于 detail-type 值为 CodeBuild Build Phase Change 的规则,键入以下内容。

    "Build '<build-id>' for build project '<project-name>' has completed the build phase of '<completed-phase>' with a status of '<completed-phase-status>'."

    将您迄今为止的结果与以下屏幕截图进行比较,该截图显示了一条 detail-type 值为 CodeBuild Build State Change 的规则:

  17. 选择 Configure details

  18. Step 2: Configure rule details 页面上,键入一个 Name 和可选的 Description。对于 State,将 Enabled 框保持选中状态。

    将您迄今为止的结果与以下屏幕截图进行比较:

  19. 选择 Create rule

  20. 按照直接运行 AWS CodeBuild 中的步骤 (举例) 创建构建项目、运行构建并查看构建信息。

  21. 确认 CodeBuild 立即成功发送构建通知。例如,检查您的收件箱中现在是否有构建通知电子邮件。

要更改规则的行为,请在 CloudWatch 控制台,选择要更改的规则,然后依次选择 Actions (操作)Edit (编辑)。对规则进行更改,然后选择 Configure details,接下来选择 Update rule

要停止使用规则发送构建通知,请在 CloudWatch 控制台中,选择要停止使用的规则,然后依次选择 Actions (操作)Disable (禁用)

要删除整个规则,请在 CloudWatch 控制台中,选择要删除的规则,然后依次选择 Actions (操作)Delete (删除)

相关资源

构建通知输入格式参考

CloudWatch 以 JSON 格式发送通知。

构建状态更改通知使用以下格式:

{ "version": "0", "id": "c030038d-8c4d-6141-9545-00ff7b7153EX", "detail-type": "CodeBuild Build State Change", "source": "aws.codebuild", "account": "123456789012", "time": "2017-09-01T16:14:28Z", "region": "us-west-2", "resources":[ "arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX" ], "detail":{ "build-status": "SUCCEEDED", "project-name": "my-sample-project", "build-id": "arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX", "additional-information": { "artifact": { "md5sum": "da9c44c8a9a3cd4b443126e823168fEX", "sha256sum": "6ccc2ae1df9d155ba83c597051611c42d60e09c6329dcb14a312cecc0a8e39EX", "location": "arn:aws:s3:::codebuild-123456789012-output-bucket/my-output-artifact.zip" }, "environment": { "image": "aws/codebuild/standard:2.0", "privileged-mode": false, "compute-type": "BUILD_GENERAL1_SMALL", "type": "LINUX_CONTAINER", "environment-variables": [] }, "timeout-in-minutes": 60, "build-complete": true, "initiator": "MyCodeBuildDemoUser", "build-start-time": "Sep 1, 2017 4:12:29 PM", "source": { "location": "codebuild-123456789012-input-bucket/my-input-artifact.zip", "type": "S3" }, "logs": { "group-name": "/aws/codebuild/my-sample-project", "stream-name": "8745a7a9-c340-456a-9166-edf953571bEX", "deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEvent:group=/aws/codebuild/my-sample-project;stream=8745a7a9-c340-456a-9166-edf953571bEX" }, "phases": [ { "phase-context": [], "start-time": "Sep 1, 2017 4:12:29 PM", "end-time": "Sep 1, 2017 4:12:29 PM", "duration-in-seconds": 0, "phase-type": "SUBMITTED", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:12:29 PM", "end-time": "Sep 1, 2017 4:13:05 PM", "duration-in-seconds": 36, "phase-type": "PROVISIONING", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:05 PM", "end-time": "Sep 1, 2017 4:13:10 PM", "duration-in-seconds": 4, "phase-type": "DOWNLOAD_SOURCE", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:10 PM", "end-time": "Sep 1, 2017 4:13:10 PM", "duration-in-seconds": 0, "phase-type": "INSTALL", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:10 PM", "end-time": "Sep 1, 2017 4:13:10 PM", "duration-in-seconds": 0, "phase-type": "PRE_BUILD", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:10 PM", "end-time": "Sep 1, 2017 4:14:21 PM", "duration-in-seconds": 70, "phase-type": "BUILD", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:14:21 PM", "end-time": "Sep 1, 2017 4:14:21 PM", "duration-in-seconds": 0, "phase-type": "POST_BUILD", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:14:21 PM", "end-time": "Sep 1, 2017 4:14:21 PM", "duration-in-seconds": 0, "phase-type": "UPLOAD_ARTIFACTS", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:14:21 PM", "end-time": "Sep 1, 2017 4:14:26 PM", "duration-in-seconds": 4, "phase-type": "FINALIZING", "phase-status": "SUCCEEDED" }, { "start-time": "Sep 1, 2017 4:14:26 PM", "phase-type": "COMPLETED" } ] }, "current-phase": "COMPLETED", "current-phase-context": "[]", "version": "1" } }

构建阶段更改通知使用以下格式:

{ "version": "0", "id": "43ddc2bd-af76-9ca5-2dc7-b695e15adeEX", "detail-type": "CodeBuild Build Phase Change", "source": "aws.codebuild", "account": "123456789012", "time": "2017-09-01T16:14:21Z", "region": "us-west-2", "resources":[ "arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX" ], "detail":{ "completed-phase": "COMPLETED", "project-name": "my-sample-project", "build-id": "arn:aws:codebuild:us-west-2:123456789012:build/my-sample-project:8745a7a9-c340-456a-9166-edf953571bEX", "completed-phase-context": "[]", "additional-information": { "artifact": { "md5sum": "da9c44c8a9a3cd4b443126e823168fEX", "sha256sum": "6ccc2ae1df9d155ba83c597051611c42d60e09c6329dcb14a312cecc0a8e39EX", "location": "arn:aws:s3:::codebuild-123456789012-output-bucket/my-output-artifact.zip" }, "environment": { "image": "aws/codebuild/standard:2.0", "privileged-mode": false, "compute-type": "BUILD_GENERAL1_SMALL", "type": "LINUX_CONTAINER", "environment-variables": [] }, "timeout-in-minutes": 60, "build-complete": true, "initiator": "MyCodeBuildDemoUser", "build-start-time": "Sep 1, 2017 4:12:29 PM", "source": { "location": "codebuild-123456789012-input-bucket/my-input-artifact.zip", "type": "S3" }, "logs": { "group-name": "/aws/codebuild/my-sample-project", "stream-name": "8745a7a9-c340-456a-9166-edf953571bEX", "deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEvent:group=/aws/codebuild/my-sample-project;stream=8745a7a9-c340-456a-9166-edf953571bEX" }, "phases": [ { "phase-context": [], "start-time": "Sep 1, 2017 4:12:29 PM", "end-time": "Sep 1, 2017 4:12:29 PM", "duration-in-seconds": 0, "phase-type": "SUBMITTED", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:12:29 PM", "end-time": "Sep 1, 2017 4:13:05 PM", "duration-in-seconds": 36, "phase-type": "PROVISIONING", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:05 PM", "end-time": "Sep 1, 2017 4:13:10 PM", "duration-in-seconds": 4, "phase-type": "DOWNLOAD_SOURCE", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:10 PM", "end-time": "Sep 1, 2017 4:13:10 PM", "duration-in-seconds": 0, "phase-type": "INSTALL", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:10 PM", "end-time": "Sep 1, 2017 4:13:10 PM", "duration-in-seconds": 0, "phase-type": "PRE_BUILD", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:13:10 PM", "end-time": "Sep 1, 2017 4:14:21 PM", "duration-in-seconds": 70, "phase-type": "BUILD", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:14:21 PM", "end-time": "Sep 1, 2017 4:14:21 PM", "duration-in-seconds": 0, "phase-type": "POST_BUILD", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:14:21 PM", "end-time": "Sep 1, 2017 4:14:21 PM", "duration-in-seconds": 0, "phase-type": "UPLOAD_ARTIFACTS", "phase-status": "SUCCEEDED" }, { "phase-context": [], "start-time": "Sep 1, 2017 4:14:21 PM", "end-time": "Sep 1, 2017 4:14:26 PM", "duration-in-seconds": 4, "phase-type": "FINALIZING", "phase-status": "SUCCEEDED" }, { "start-time": "Sep 1, 2017 4:14:26 PM", "phase-type": "COMPLETED" } ] }, "completed-phase-status": "SUCCEEDED", "completed-phase-duration-seconds": 4, "version": "1", "completed-phase-start": "Sep 1, 2017 4:14:21 PM", "completed-phase-end": "Sep 1, 2017 4:14:26 PM" } }