

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

# 监控 CodePipeline 事件
<a name="detect-state-changes-cloudwatch-events"></a>



您可以在中监控 CodePipeline 事件 EventBridge，它会提供来自您自己的应用程序、 software-as-a-service (SaaS) 应用程序和的实时数据流 Amazon Web Services 服务。 EventBridge 将该数据路由到目标，例如 Amazon Lambda 和 Amazon 简单通知服务。这些事件与 Amazon Events 中显示 CloudWatch 的事件相同，后者提供描述 Amazon 资源变化的近乎实时的系统事件流。有关更多信息，请参阅[什么是亚马逊 EventBridge？](https://docs.amazonaws.cn/eventbridge/latest/userguide/) 在《*亚马逊 EventBridge 用户指南》*中。

**注意**  
亚马逊 EventBridge 是管理您的活动的首选方式。Amazon Ev CloudWatch ent EventBridge s 和 Amazon Events 是相同的底层服务和 API，但 EventBridge 提供了更多功能。您在 CloudWatch 活动中所做的更改或 EventBridge 将显示在每个控制台中。

事件由规则组成。通过选择以下选项来配置规则：
+ **事件模式。**每条规则都以事件模式表示，其中包含要监控的事件的来源和类型以及事件目标。要监控事件，您需要创建一条规则，将所监视的服务作为事件源，例如 CodePipeline。例如，您可以创建带有事件模式的规则，当管道、阶段或操作的状态发生变化时，该事件模式 CodePipeline 用作事件源来触发规则。
+ **目标。**新规则将选定的服务作为事件目标。您可能想设置目标服务以发送通知，捕获状态信息，采取纠正措施，启动事件或采取其他操作。添加目标时，还必须向授予权限 EventBridge 以允许其调用所选目标服务。

每种类型的执行状态更改事件发出包含特定消息内容的通知，其中：
+ 初始 `version` 条目显示事件的版本号。
+ `detail` 管道下面的 `version` 条目显示管道结构版本号。
+ `detail` 管道下面的 `execution-id` 条目显示导致状态更改的管道执行的执行 ID。请参阅 [Amazon CodePipeline API 参考](https://docs.amazonaws.cn/codepipeline/latest/APIReference/)中的 **GetPipelineExecution** API 调用。
+ 该 `pipeline-execution-attempt` 条目显示特定执行 ID 的尝试次数或重试次数。

CodePipeline EventBridge 每当您的资源状态发生 Amazon Web Services 账户 变化时，都会向其报告事件。以下资源在保证 at-least-once的基础上发出事件：
+ 管道执行
+ 阶段执行
+ 操作执行

事件由 EventBridge 触发，事件模式和架构详述如上所述。对于已处理的事件，例如通过在开发人员工具控制台中配置的通知收到的事件，事件消息包含的事件模式字段会有些变化。例如，`detail-type` 字段转换为 `detailType`。有关更多信息，请参阅《[亚马逊 **PutEvents** API 参考》中的 EventBridge API 调用](https://docs.amazonaws.cn/eventbridge/latest/APIReference/)。

以下示例显示了的事件 CodePipeline。在可能的情况下，每个示例都显示已发出事件的架构以及已处理事件的架构。

**Topics**
+ [详细信息类型](#detect-state-events-types)
+ [管道级事件](#detect-state-events-pipeline)
+ [阶段级事件](#detect-state-events-stage)
+ [操作级事件](#detect-state-events-action)
+ [创建发送管道事件通知的规则](#create-cloudwatch-notifications)

## 详细信息类型
<a name="detect-state-events-types"></a>

在设置要监控的事件时，可以选择事件的详细信息类型。

您可以配置在以下状态发生更改时发送的通知：
+ 指定的管道或所有管道。您可以使用 `"detail-type":` `"CodePipeline Pipeline Execution State Change"` 控制该行为。
+ 指定管道或所有管道中的指定阶段或所有阶段。您可以使用 `"detail-type":` `"CodePipeline Stage Execution State Change"` 控制该行为。
+ 指定管道或所有管道的指定阶段或所有阶段中的指定操作或所有操作。您可以使用 `"detail-type":` `"CodePipeline Action Execution State Change"` 控制该行为。

**注意**  
由发出的事件 EventBridge 包含`detail-type`参数，该参数将在处理事件`detailType`时转换为该参数。


****  
[\[See the AWS documentation website for more details\]](http://docs.amazonaws.cn/codepipeline/latest/userguide/detect-state-changes-cloudwatch-events.html)

## 管道级事件
<a name="detect-state-events-pipeline"></a>

当管道执行状态发生变化时，就会发出管道级事件。

**Topics**
+ [管道 STARTED 事件](#detect-state-events-pipeline-started)
+ [管道 STOPPING 事件](#detect-state-events-pipeline-stopping)
+ [管道 SUCCEEDED 事件](#detect-state-events-pipeline-succeeded)
+ [管道 SUCCEEDED（含 Git 标签的示例）](#w2aac42c13c29c15)
+ [管道 FAILED 事件](#detect-state-events-pipeline-failed)
+ [管道 FAILED（含 Git 标签的示例）](#w2aac42c13c29c23)

### 管道 STARTED 事件
<a name="detect-state-events-pipeline-started"></a>

 在启动管道执行时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-east-1` 区域中名为 `"myPipeline"` 的管道。`id` 字段表示事件 ID，`account` 字段表示创建管道的账户 ID。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Pipeline Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-24T22:03:07Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:codepipeline:us-east-1:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "execution-trigger": {
            "trigger-type": "StartPipelineExecution",
            "trigger-detail": "arn:aws:sts::123456789012:assumed-role/Admin/my-user"
        },
        "state": "STARTED",
        "version": 1.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Pipeline Execution State Change",
    "region": "us-east-1",
    "source": "aws.codepipeline",
    "time": "2021-06-24T00:44:50Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "execution-trigger": {
            "trigger-type": "StartPipelineExecution",
            "trigger-detail": "arn:aws:sts::123456789012:assumed-role/Admin/my-user"
        },
        "state": "STARTED",
        "version": 1.0,
        "pipeline-execution-attempt": 1.0
    },
    "resources": [
        "arn:aws:codepipeline:us-east-1:123456789012:myPipeline"
    ],
    "additionalAttributes": {}
}
```

------

### 管道 STOPPING 事件
<a name="detect-state-events-pipeline-stopping"></a>

在停止管道执行时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-west-2` 区域中名为 `myPipeline` 的管道。

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Pipeline Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-24T22:02:20Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "state": "STOPPING",
        "version": 3.0,
        "pipeline-execution-attempt": 1.0
        "stop-execution-comments": "Stopping the pipeline for an update"
    }
}
```

### 管道 SUCCEEDED 事件
<a name="detect-state-events-pipeline-succeeded"></a>

 在管道执行成功时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-east-1` 区域中名为 `myPipeline` 的管道。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Pipeline Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-24T22:03:44Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:codepipeline:us-east-1:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "state": "SUCCEEDED",
        "version": 3.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Pipeline Execution State Change",
    "region": "us-east-1",
    "source": "aws.codepipeline",
    "time": "2021-06-30T22:13:51Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "state": "SUCCEEDED",
        "version": 1.0,
        "pipeline-execution-attempt": 1.0
    },
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "additionalAttributes": {}
}
```

------

### 管道 SUCCEEDED（含 Git 标签的示例）
<a name="w2aac42c13c29c15"></a>

在管道执行有一个阶段已经重试并成功时，它发出一个事件以发送包含以下内容的通知。此示例适用于为 Git 标签配置了 `execution-trigger` 的 `eu-central-1` 区域中名为 `myPipeline` 的管道。

**注意**  
`execution-trigger` 字段的值是 `tag-name` 或 `branch-name`，具体取决于触发管道的事件类型。

```
{
    "version": "0",
    "id": "b128b002-09fd-4574-4eba-27152726c777",
    "detail-type": "CodePipeline Pipeline Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2023-10-26T13:50:53Z",
    "region": "eu-central-1",
    "resources": [
        "arn:aws:codepipeline:eu-central-1:123456789012:BuildFromTag"
    ],
    "detail": {
        "pipeline": "BuildFromTag",
        "execution-id": "e17b5773-cc0d-4db2-9ad7-594c73888de8",
        "start-time": "2023-10-26T13:49:39.208Z",
        "execution-trigger": {
            "author-display-name": "Mary Major",
            "full-repository-name": "mmajor/sample-project",
            "provider-type": "GitLab",
            "author-email": "email_address",
            "commit-message": "Update file README.md",
            "author-date": "2023-08-16T21:08:08Z",
            "tag-name": "gitlab-v4.2.1",
            "commit-id": "commit_ID",
            "connection-arn": "arn:aws:codestar-connections:eu-central-1:123456789012:connection/0f5b706a-1a1d-46c5-86b6-f177321bcfb2",
            "author-id": "Mary Major"
        },
        "state": "SUCCEEDED",
        "version": 32.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

### 管道 FAILED 事件
<a name="detect-state-events-pipeline-failed"></a>

在管道执行失败时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-west-2` 区域中名为 `"myPipeline"` 的管道。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Pipeline Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-31T18:55:43Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "state": "FAILED",
        "version": 4.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Pipeline Execution State Change",
    "region": "us-west-2",
    "source": "aws.codepipeline",
    "time": "2021-06-24T00:46:16Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "state": "FAILED",
        "version": 1.0,
        "pipeline-execution-attempt": 1.0
    },
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "additionalAttributes": {
        "failedActionCount": 1,
        "failedActions": [
            {
                "action": "Deploy",
                "additionalInformation": "Deployment <ID> failed"
            }
        ],
        "failedStage": "Deploy"
    }
```

------

### 管道 FAILED（含 Git 标签的示例）
<a name="w2aac42c13c29c23"></a>

除非它在源阶段失败，否则对于配置了触发器的管道，它会发出一个事件，发送包含以下内容的通知。此示例适用于为 Git 标签配置了 `execution-trigger` 的 `eu-central-1` 区域中名为 `myPipeline` 的管道。

**注意**  
`execution-trigger` 字段的值是 `tag-name` 或 `branch-name`，具体取决于触发管道的事件类型。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Pipeline Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-31T18:55:43Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "execution-trigger": {
            "author-display-name": "Mary Major",
            "full-repository-name": "mmajor/sample-project",
            "provider-type": "GitLab",
            "author-email": "email_address",
            "commit-message": "Update file README.md",
            "author-date": "2023-08-16T21:08:08Z",
            "tag-name": "gitlab-v4.2.1",
            "commit-id": "commit_ID",
            "connection-arn": "arn:aws:codestar-connections:eu-central-1:123456789012:connection/0f5b706a-1a1d-46c5-86b6-f177321bcfb2",
            "author-id": "Mary Major"
        },
        "state": "FAILED",
        "version": 4.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Pipeline Execution State Change",
    "region": "us-west-2",
    "source": "aws.codepipeline",
    "time": "2021-06-24T00:46:16Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "execution-trigger": {
            "author-display-name": "Mary Major",
            "full-repository-name": "mmajor/sample-project",
            "provider-type": "GitLab",
            "author-email": "email_address",
            "commit-message": "Update file README.md",
            "author-date": "2023-08-16T21:08:08Z",
            "tag-name": "gitlab-v4.2.1",
            "commit-id": "commit_ID",
            "connection-arn": "arn:aws:codestar-connections:eu-central-1:123456789012:connection/0f5b706a-1a1d-46c5-86b6-f177321bcfb2",
            "author-id": "Mary Major"
        },
        "state": "FAILED",
        "version": 1.0,
        "pipeline-execution-attempt": 1.0
    },
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "additionalAttributes": {
        "failedActionCount": 1,
        "failedActions": [
            {
                "action": "Deploy",
                "additionalInformation": "Deployment <ID> failed"
            }
        ],
        "failedStage": "Deploy"
    }
```

------

## 阶段级事件
<a name="detect-state-events-stage"></a>

当阶段执行状态发生变化时，就会发出阶段级事件。

**Topics**
+ [阶段 STARTED 事件](#detect-state-events-stage-started)
+ [阶段 STOPPING 事件](#detect-state-events-stage-stopping)
+ [阶段 STOPPED 事件](#detect-state-events-stage-stopped)
+ [阶段重试后的阶段 RESUMED 事件](#w2aac42c13c31c15)

### 阶段 STARTED 事件
<a name="detect-state-events-stage-started"></a>

在启动阶段执行时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-east-1` 区域中名为 `"myPipeline"` 的管道的 `Prod` 阶段。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": 01234567-EXAMPLE,
    "detail-type": "CodePipeline Stage Execution State Change",
    "source": "aws.codepipeline",
    "account": 123456789012,
    "time": "2020-01-24T22:03:07Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:codepipeline:us-east-1:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "version": 1.0,
        "execution-id": 12345678-1234-5678-abcd-12345678abcd,
        "start-time": "2023-10-26T13:49:39.208Z",
        "stage": "Prod",
        "state": "STARTED",
        "pipeline-execution-attempt": 1.0
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Stage Execution State Change",
    "region": "us-east-1",
    "source": "aws.codepipeline",
    "time": "2021-06-24T00:45:40Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "stage": "Source",
        "state": "STARTED",
        "version": 1.0,
        "pipeline-execution-attempt": 0.0
    },
    "resources": [
        "arn:aws:codepipeline:us-east-1:123456789012:myPipeline"
    ],
    "additionalAttributes": {
        "sourceActions": [
            {
                "sourceActionName": "Source",
                "sourceActionProvider": "CodeCommit",
                "sourceActionVariables": {
                    "BranchName": "main",
                    "CommitId": "<ID>",
                    "RepositoryName": "my-repo"
                }
            }
        ]
    }
}
```

------

### 阶段 STOPPING 事件
<a name="detect-state-events-stage-stopping"></a>

在停止阶段执行时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-west-2` 区域中名为 `myPipeline` 的管道的 `Deploy` 阶段。

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Stage Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-24T22:02:20Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "stage": "Deploy",
        "state": "STOPPING",
        "version": 3.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

### 阶段 STOPPED 事件
<a name="detect-state-events-stage-stopped"></a>

在阶段执行已停止时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-west-2` 区域中名为 `myPipeline` 的管道的 `Deploy` 阶段。

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Stage Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-31T18:21:39Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:49:39.208Z",
        "stage": "Deploy",
        "state": "STOPPED",
        "version": 3.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

### 阶段重试后的阶段 RESUMED 事件
<a name="w2aac42c13c31c15"></a>

在阶段恢复执行而且有一个阶段已经重试时，它发出一个事件以发送包含以下内容的通知。

当阶段已重试时，将显示 `stage-last-retry-attempt-time` 字段，如本例所示。如果执行了重试，则该字段将显示在所有阶段事件上。

**注意**  
阶段重试后，`stage-last-retry-attempt-time` 字段将显示在所有后续阶段事件中。

```
{
    "version": "0",
    "id": "38656bcd-a798-5f92-c738-02a71be484e1",
    "detail-type": "CodePipeline Stage Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2023-10-26T14:14:56Z",
    "region": "eu-central-1",
    "resources": [
        "arn:aws:codepipeline:eu-central-1:123456789012:BuildFromTag"
    ],
    "detail": {
        "pipeline": "BuildFromTag",
        "execution-id": "05dafb6a-5a56-4951-a858-968795364846",
        "stage-last-retry-attempt-time": "2023-10-26T14:14:56.305Z",
        "stage": "Build",
        "state": "RESUMED",
        "version": 32.0,
        "pipeline-execution-attempt": 2.0
    }
}
```

## 操作级事件
<a name="detect-state-events-action"></a>

当操作执行状态发生变化时，就会发出操作级事件。

**Topics**
+ [操作 STARTED 事件](#detect-state-events-action-started)
+ [操作 SUCCEEDED 事件](#detect-state-events-action-succeeded)
+ [操作 FAILED 事件](#detect-state-events-action-failed)
+ [操作 ABANDONED 事件](#detect-state-events-action-abandoned)

### 操作 STARTED 事件
<a name="detect-state-events-action-started"></a>

在启动操作执行时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-east-1` 区域中名为 `myPipeline` 的管道的部署操作 `myAction`。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": 01234567-EXAMPLE,
    "detail-type": "CodePipeline Action Execution State Change",
    "source": "aws.codepipeline",
    "account": 123456789012,
    "time": "2020-01-24T22:03:07Z",
    "region": "us-east-1",
    "resources": [
        "arn:aws:codepipeline:us-east-1:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": 12345678-1234-5678-abcd-12345678abcd,
        "start-time": "2023-10-26T13:51:09.981Z",
        "stage": "Prod",
        "action-execution-id": "47f821c5-a902-44b2-ae61-b878d31ecd21",
        "action": "myAction",
        "state": "STARTED",
        "type": {
            "owner": "AWS",
            "category": "Deploy",
            "provider": "CodeDeploy",
            "version": "1"
        },
        "version": 2.0
        "pipeline-execution-attempt": 1.0
        "input-artifacts": [
            {
                "name": "SourceArtifact",
                "s3location": {
                    "bucket": "codepipeline-us-east-1-BUCKETEXAMPLE",
                    "key": "myPipeline/SourceArti/KEYEXAMPLE"
                }
            }
        ]
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Action Execution State Change",
    "region": "us-west-2",
    "source": "aws.codepipeline",
    "time": "2021-06-24T00:45:44Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:51:09.981Z",
        "stage": "Deploy",
        "action-execution-id": "47f821c5-a902-44b2-ae61-b878d31ecd21",
        "action": "Deploy",
        "input-artifacts": [
            {
                "name": "SourceArtifact",
                "s3location": {
                    "bucket": "codepipeline-us-east-1-EXAMPLE",
                    "key": "myPipeline/SourceArti/EXAMPLE"
                }
            }
        ],
        "state": "STARTED",
        "region": "us-east-1",
        "type": {
            "owner": "AWS",
            "provider": "CodeDeploy",
            "category": "Deploy",
            "version": "1"
        },
        "version": 1.0,
        "pipeline-execution-attempt": 1.0
    },
    "resources": [
        "arn:aws:codepipeline:us-east-1:123456789012:myPipeline"
    ],
    "additionalAttributes": {}
}
```

------

### 操作 SUCCEEDED 事件
<a name="detect-state-events-action-succeeded"></a>

在操作执行成功时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-west-2` 区域中名为 `"myPipeline"` 的管道的源操作 `"Source"`。对于这种事件类型，有两个不同的 `region` 字段。事件 `region` 字段指定管道事件的区域。`detail` 部分下的 `region` 字段指定操作的区域。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Action Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-24T22:03:11Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "start-time": "2023-10-26T13:51:09.981Z",
        "stage": "Source",
        "execution-result": {
            "external-execution-url": "https://us-west-2.console.aws.amazon.com/codecommit/home#/repository/my-repo/commit/8cf40f2EXAMPLE",
            "external-execution-summary": "Added LICENSE.txt",
            "external-execution-id": "8cf40fEXAMPLE"
        },
        "output-artifacts": [
            {
                "name": "SourceArtifact",
                "s3location": {
                    "bucket": "codepipeline-us-west-2-BUCKETEXAMPLE",
                    "key": "myPipeline/SourceArti/KEYEXAMPLE"
                }
            }
        ],
        "action-execution-id": "47f821c5-a902-44b2-ae61-b878d31ecd21",
        "action": "Source",
        "state": "SUCCEEDED",
        "region": "us-west-2",
        "type": {
            "owner": "AWS",
            "provider": "CodeCommit",
            "category": "Source",
            "version": "1"
        },
        "version": 3.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Action Execution State Change",
    "region": "us-west-2",
    "source": "aws.codepipeline",
    "time": "2021-06-24T00:45:44Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-west-2:ACCOUNT:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "arn:aws:codepipeline:us-west-2:123456789012:myPipeline",
        "start-time": "2023-10-26T13:51:09.981Z",
        "stage": "Source",
        "execution-result": {
            "external-execution-url": "https://us-west-2.console.aws.amazon.com/codecommit/home#/repository/my-repo/commit/8cf40f2EXAMPLE",
            "external-execution-summary": "Edited index.html",
            "external-execution-id": "36ab3ab7EXAMPLE"
        },
        "output-artifacts": [
            {
                "name": "SourceArtifact",
                "s3location": {
                    "bucket": "codepipeline-us-west-2-EXAMPLE",
                    "key": "myPipeline/SourceArti/EXAMPLE"
                }
            }
        ],
        "action-execution-id": "47f821c5-a902-44b2-ae61-b878d31ecd21",
        "action": "Source",
        "state": "SUCCEEDED",
        "region": "us-west-2",
        "type": {
            "owner": "AWS",
            "provider": "CodeCommit",
            "category": "Source",
            "version": "1"
        },
        "version": 1.0,
        "pipeline-execution-attempt": 1.0
    },
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "additionalAttributes": {}
}
```

------

### 操作 FAILED 事件
<a name="detect-state-events-action-failed"></a>

在操作执行失败时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-west-2` 区域中名为 `"myPipeline"` 的管道的 `"Deploy"` 操作。

------
#### [ Emitted event ]

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Action Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-31T18:55:43Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd", 
        "start-time": "2023-10-26T13:51:09.981Z",
        "stage": "Deploy",
        "execution-result": {
            "external-execution-url": "https://us-west-2.console.aws.amazon.com/codedeploy/home?#/deployments/<ID>",
            "external-execution-summary": "Deployment <ID> failed",
            "external-execution-id": "<ID>",
            "error-code": "JobFailed"
        },
        "action-execution-id": "47f821c5-a902-44b2-ae61-b878d31ecd21",
        "action": "Deploy",
        "state": "FAILED",
        "region": "us-west-2",
        "type": {
            "owner": "AWS",
            "provider": "CodeDeploy",
            "category": "Deploy",
            "version": "1"
        },
        "version": 4.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

------
#### [ Processed event ]

```
{
    "account": "123456789012",
    "detailType": "CodePipeline Action Execution State Change",
    "region": "us-west-2",
    "source": "aws.codepipeline",
    "time": "2021-06-24T00:46:16Z",
    "notificationRuleArn": "arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/a69c62c21EXAMPLE",
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "stage": "Deploy",
        "execution-result": {
            "external-execution-url": "https://console.aws.amazon.com/codedeploy/home?region=us-west-2#/deployments/<ID>",
            "external-execution-summary": "Deployment <ID> failed",
            "external-execution-id": "<ID>",
            "error-code": "JobFailed"
        },  
        "action-execution-id": "47f821c5-a902-44b2-ae61-b878d31ecd21",
        "action": "Deploy",
        "state": "FAILED",
        "region": "us-west-2",
        "type": {
            "owner": "AWS",
            "provider": "CodeDeploy",
            "category": "Deploy",
            "version": "1"
        },
        "version": 13.0,
        "pipeline-execution-attempt": 1.0
    },
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "additionalAttributes": {
        "additionalInformation": "Deployment <ID> failed"
    }
}
```

------

### 操作 ABANDONED 事件
<a name="detect-state-events-action-abandoned"></a>

在放弃操作执行时，它发出一个事件以发送包含以下内容的通知。此示例适用于 `us-west-2` 区域中名为 `"myPipeline"` 的管道的 `"Deploy"` 操作。

```
{
    "version": "0",
    "id": "01234567-EXAMPLE",
    "detail-type": "CodePipeline Action Execution State Change",
    "source": "aws.codepipeline",
    "account": "123456789012",
    "time": "2020-01-31T18:21:39Z",
    "region": "us-west-2",
    "resources": [
        "arn:aws:codepipeline:us-west-2:123456789012:myPipeline"
    ],
    "detail": {
        "pipeline": "myPipeline",
        "execution-id": "12345678-1234-5678-abcd-12345678abcd",
        "stage": "Deploy",
        "action-execution-id": "47f821c5-a902-44b2-ae61-b878d31ecd21",
        "action": "Deploy",
        "state": "ABANDONED",
        "region": "us-west-2",
        "type": {
            "owner": "AWS",
            "provider": "CodeDeploy",
            "category": "Deploy",
            "version": "1"
        },
        "version": 3.0,
        "pipeline-execution-attempt": 1.0
    }
}
```

## 创建发送管道事件通知的规则
<a name="create-cloudwatch-notifications"></a>

规则监视某些事件，然后将其路由到您选择的 Amazon 目标。您可以创建在发生其他 Amazon 操作时自动执行 Amazon 操作的规则，也可以创建按设定的时间表定期执行 Amazon 操作的规则。

**Topics**
+ [当管道状态发生更改时发送通知（控制台）](#monitoring-cloudwatch-events-console)
+ [当管道状态发生更改时发送通知 (CLI)](#monitoring-cloudwatch-events-cli)

### 当管道状态发生更改时发送通知（控制台）
<a name="monitoring-cloudwatch-events-console"></a>

以下步骤说明如何使用 EventBridge 控制台创建规则，以发送更改通知 CodePipeline。

**使用 Amazon S3 源创建针对您的管道的 EventBridge 规则**

1. 打开 Amazon EventBridge 控制台，网址为[https://console.aws.amazon.com/events/](https://console.amazonaws.cn/events/)。

1. 在导航窗格中，选择**规则**。保留选中的默认总线或选择一个事件总线。选择 **Create rule**（创建规则）。

1. 在**名称**中，输入规则的名称。

1. 对于**规则类型**，选择**具有事件模式的规则**。选择**下一步**。

1. 在**事件模式**下，选择 **Amazon 服务**。

1. 从**事件类型**下拉列表中，选择通知的状态更改级别。
   + 要获取适用于管道级事件的规则，请选择**CodePipeline管道执行**状态更改。
   + 要获取适用于舞台级别事件的规则，请选择**CodePipeline阶段执行状态更改**。
   + 要获取适用于操作级事件的规则，请选择**CodePipeline操作执行状态更改**。

1. 指定规则适用的状态更改：
   + 对于适用于所有状态更改的规则，请选择**任意状态**。
   + 对于仅适用于某些状态更改的规则，请选择**特定状态**，然后从列表中选择一个或多个状态值。

1. 对于比选择器允许的级别更详细的事件模式，您也可以使用**事件模式**窗口中的**编辑模式**选项指定 JSON 格式的事件模式。
**注意**  
如果未另行指定，则会为所有pipelines/stages/actions和状态创建事件模式。

   有关更详细的事件模式，您可以复制以下示例事件模式并粘贴到**事件模式**窗口中。
   +   
**Example**  

     可以使用该示例事件模式在所有管道中捕获失败的部署和生成操作。

     ```
     {
     "source": [
         "aws.codepipeline"
       ],
       "detail-type": [
         "CodePipeline Action Execution State Change"
       ],
       "detail": {
         "state": [
           "FAILED"
         ],
         "type": {
           "category": ["Deploy", "Build"]
         }
       }
     }
     ```
   +   
**Example**  

     可以使用该示例事件模式在所有管道中捕获所有拒绝或失败的审批操作。

     ```
     {
      "source": [
         "aws.codepipeline"
       ],
       "detail-type": [
         "CodePipeline Action Execution State Change"
       ],
       "detail": {
         "state": [
           "FAILED"
         ],
         "type": {
           "category": ["Approval"]
         }
       }
     }
     ```
   +   
**Example**  

     可以使用该示例事件模式从指定的管道中捕获所有事件。

     ```
     {
     "source": [
         "aws.codepipeline"
       ],
       "detail-type": [
         "CodePipeline Pipeline Execution State Change",
         "CodePipeline Action Execution State Change",
         "CodePipeline Stage Execution State Change"
       ],
       "detail": {
         "pipeline": ["myPipeline", "my2ndPipeline"]
       }
     }
     ```

1. 选择**下一步**。

1. 在**目标类型**中，选择 **Amazon 服务**。

1. 在**选择目标中**，选择**CodePipeline**。在**管道 ARN** 中，输入该规则启动的管道的管道 ARN。
**注意**  
要获取管道 ARN，请运行 **get-pipeline** 命令。管道 ARN 将显示在输出中。它是使用以下格式构造的：  
arn: aws: codepiline::: *region* *account* *pipeline-name*  
示例管道 ARN：  
arn: aws: codepipeline: us-east-2:80398 示例：MyFirstPipeline 

1. 要创建或指定一个 IAM 服务角色来授予调用与您的 EventBridge 规则关联的目标的 EventBridge 权限（在本例中，目标是 CodePipeline），请执行以下操作：
   + 选择 “**为此特定资源创建新角色**” 以创建服务角色，该角色 EventBridge 授予您启动管道执行的权限。
   + 选择 “**使用现有角色**” 输入一个服务角色，该角色 EventBridge 授予您启动管道执行的权限。

1. 选择**下一步**。

1. 在**标签**页面上，选择**下一步**。

1. 在**查看和创建**页面上，检查规则配置。如果您对规则满意，请选择 **Create rule**。

### 当管道状态发生更改时发送通知 (CLI)
<a name="monitoring-cloudwatch-events-cli"></a>

以下步骤说明如何使用 CLI 创建 CloudWatch 事件规则以发送更改通知 CodePipeline。

要使用创建规则，请调用**put-rule**命令，指定： Amazon CLI 
+ 唯一地标识创建的规则的名称。在您创建的与 Amazon 账户 CodePipeline 关联的所有管道中，此名称必须是唯一的。
+ 规则使用的源事件模式和详细信息字段。有关更多信息，请参阅 [Amazon EventBridge 和事件模式](https://docs.amazonaws.cn/eventbridge/latest/userguide/eb-events.html)。

**创建以事件源 CodePipeline 为对象的 EventBridge 规则**

1. 调用 **put-rule** 命令以创建一个规则，从而指定事件模式。(请参阅上表以了解有效的状态。)

   以下示例命令用于创建名**--event-pattern**为的规则`“MyPipelineStateChanges”`，当名为 “myPipeline” 的管道执行管道失败时，该规则会发出 CloudWatch 事件。

   ```
   aws events put-rule --name "MyPipelineStateChanges" --event-pattern "{\"source\":[\"aws.codepipeline\"],\"detail-type\":[\"CodePipeline Pipeline Execution State Change\"],\"detail\":{\"pipeline\":[\"myPipeline\"],\"state\":[\"FAILED\"]}}"
   ```

1. 调用 **put-targets** 命令并包含以下参数：
   + `--rule` 参数与您使用 **put-rule** 创建的 `rule_name` 结合使用。
   + `--targets` 参数与目标列表中该目标的列表 `Id` 以及 Amazon SNS 主题的 `ARN` 结合使用。

   以下示例命令为名为 `MyPipelineStateChanges` 的规则指定此内容，目标 `Id` 由数字 1 组成，这指示此内容位于规则的目标列表中，而这是目标 1。示例命令还为 Amazon SNS 主题指定一个示例 `ARN`。

   ```
   aws events put-targets --rule MyPipelineStateChanges --targets Id=1,Arn=arn:aws:sns:us-west-2:11111EXAMPLE:MyNotificationTopic
   ```

1.  EventBridge 添加使用指定目标服务调用通知的权限。有关更多信息，请参阅[使用适用于 Amazon EventBridge 的基于资源的政策。](https://docs.amazonaws.cn/eventbridge/latest/userguide/eb-use-resource-based.html)