

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

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



您可以在 EventBridge 中監控 CodePipeline 事件，從您自己的應用程式、software-as-a-service (SaaS) 應用程式和 提供即時資料串流 AWS 服務。EventBridge 會將該資料路由到目標，例如 AWS Lambda 和 Amazon Simple Notification Service。這些事件與 Amazon CloudWatch Events 中出現的事件相同，可提供近乎即時的系統事件串流，說明 AWS 資源的變更。如需詳細資訊，請參閱《Amazon EventBridge 使用者指南》中的[什麼是 Amazon EventBridge？](https://docs.aws.amazon.com/eventbridge/latest/userguide/)。

**注意**  
Amazon EventBridge 是管理活動的首選方式。Amazon CloudWatch Events 和 EventBridge 是相同的基礎服務和 API，但 EventBridge 提供了更多功能。您在 CloudWatch Events 或 EventBridge 中所做的變更將會出現在每個主控台中。

事件由規則組成。規則的設定方式是選擇下列項目：
+ **事件模式。**每個規則都以事件模式表示，其中包含要監控的事件來源和類型，以及事件目標。若要監控事件，您可以使用要監控為事件來源的服務建立規則，例如 CodePipeline。例如，您可以建立具有使用 CodePipeline 作為事件來源的事件模式的規則，以在管道、階段或動作的狀態發生變更時觸發規則。
+ **目標。**新的規則收到選取的服務做為事件目標。您可能想要設定目標服務來傳送通知、擷取狀態資訊、採取修正動作、啟動事件或採取其他動作。新增目標時，您還必須將許可授予 EventBridge，以允許它叫用選取的目標服務。

每種類型的執行狀態變更事件都會發出具有特定訊息內容的通知，其中：
+ 初始`version`項目會顯示事件的版本編號。
+ `detail` 管道下的 `version` 項目會顯示管道結構版本號碼。
+ `detail` 管道下的 `execution-id` 項目會顯示導致狀態變更之管道執行的執行 ID。請參閱 **GetPipelineExecution** API [AWS CodePipeline 參考中的 API](https://docs.aws.amazon.com/codepipeline/latest/APIReference/) 呼叫。
+ `pipeline-execution-attempt` 項目會顯示特定執行 ID 的嘗試次數或重試次數。

每 AWS 帳戶 當您變更資源的狀態時，CodePipeline 都會向 EventBridge 報告事件。系統會針對下列資源，至少at-least-once保證的事件：
+ 管道執行
+ 階段執行
+ 動作執行

EventBridge 會發出事件，其中包含上述事件模式和結構描述。對於已處理的事件，例如您透過在開發人員工具主控台中設定的通知接收的事件，事件訊息包含具有一些變化的事件模式欄位。例如， `detail-type` 欄位會轉換為 `detailType`。如需詳細資訊，請參閱《Amazon EventBridge **PutEvents** API 參考》中的 API 呼叫。 [ EventBridge ](https://docs.aws.amazon.com/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`時轉換為 。


****  


- **CodePipeline 管道執行狀態變更**
  - **State:** CANCELED / **Description:** 已取消管道執行，因為已更新管道結構。
  - **State:** 失敗 / **Description:** 管道執行未成功完成。
  - **State:** RESUMED (繼續) / **Description:** 已重試失敗的管道執行，以回應 RetryStageExecution API 呼叫。
  - **State:** STARTED (已啟動) / **Description:** 管道執行目前正在執行。
  - **State:** 已停止 / **Description:** 停止程序已完成，且管道執行已停止。
  - **State:** 停止中 / **Description:** 由於要求停止並等待或停止並捨棄管道執行，因此管道執行正在停止中。
  - **State:** SUCCEEDED (成功) / **Description:** 管道執行已成功完成。
  - **State:** SUPERSEDED (已取代) / **Description:** 雖然此管道執行等待下一個階段完成，但較新的管道執行已改為透過管道前進並繼續。

- **CodePipeline 階段執行狀態變更**
  - **State:** CANCELED / **Description:** 已取消階段，因為已更新管道結構。
  - **State:** 失敗 / **Description:** 階段未成功完成。
  - **State:** RESUMED (繼續) / **Description:** 已重試失敗的階段，以回應 RetryStageExecution API 呼叫。
  - **State:** STARTED (已啟動) / **Description:** 階段目前正在執行。
  - **State:** 已停止 / **Description:** 停止程序已完成，且階段執行已停止。
  - **State:** 停止中 / **Description:** 由於要求停止並等待或停止並捨棄管道執行，因此階段執行正在停止中。
  - **State:** SUCCEEDED (成功) / **Description:** 階段已成功完成。

- **CodePipeline 動作執行狀態變更**
  - **State:** ABANDONED / **Description:** 由於要求停止並捨棄管道執行，因而捨棄動作。
  - **State:** CANCELED / **Description:** 已取消動作，因為已更新管道結構。
  - **State:** 失敗 / **Description:** 對於核准動作，FAILED (失敗) 狀態表示檢閱者拒絕動作，或因動作組態不正確而失敗。
  - **State:** STARTED (已啟動) / **Description:** 動作目前正在執行。
  - **State:** SUCCEEDED (成功) / **Description:** 動作已成功完成。



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

當管道執行的狀態變更時，就會發出管道層級事件。

**Topics**
+ [管道 STARTED 事件](#detect-state-events-pipeline-started)
+ [管道停止事件](#detect-state-events-pipeline-stopping)
+ [管道 SUCCEEDED 事件](#detect-state-events-pipeline-succeeded)
+ [管道 SUCCEEDED （使用 Git 標籤的範例）](#w2aac42c13c29c15)
+ [管道失敗事件](#detect-state-events-pipeline-failed)
+ [管道 FAILED （使用 Git 標籤的範例）](#w2aac42c13c29c23)

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

 當管道執行開始時，它會發出事件，以傳送包含下列內容的通知。此範例適用於 `"myPipeline"` `us-east-1`區域中名為 的管道。`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": {}
}
```

------

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

當管道執行停止時，它會發出事件，以傳送包含下列內容的通知。此範例適用於 `myPipeline` `us-west-2`區域中名為 的管道。

```
{
    "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>

 當管道執行成功時，它會發出事件，以傳送包含下列內容的通知。此範例適用於 `myPipeline` `us-east-1`區域中名為 的管道。

------
#### [ 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>

當管道執行的階段已重試並成功時，它會發出事件，以傳送包含下列內容的通知。此範例適用於 `myPipeline` 為 Git 標籤`execution-trigger`設定的`eu-central-1`區域中名為 的管道。

**注意**  
`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
    }
}
```

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

當管道執行失敗時，它會發出事件，以傳送包含下列內容的通知。此範例適用於 `"myPipeline"` `us-west-2`區域中名為 的管道。

------
#### [ 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>

除非在來源階段失敗，否則對於使用觸發條件設定的管道，它會發出事件來傳送具有下列內容的通知。此範例適用於 `myPipeline` 為 Git 標籤`execution-trigger`設定的`eu-central-1`區域中名為 的管道。

**注意**  
`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)
+ [階段停止事件](#detect-state-events-stage-stopping)
+ [階段已停止事件](#detect-state-events-stage-stopped)
+ [階段重試事件之後的階段 RESUMED](#w2aac42c13c31c15)

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

當階段執行開始時，會發出事件，以傳送包含下列內容的通知。此範例適用於`"myPipeline"`在 `us-east-1` 區域中名為 的管道，適用於階段 `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"
                }
            }
        ]
    }
}
```

------

### 階段停止事件
<a name="detect-state-events-stage-stopping"></a>

當階段執行停止時，會發出事件，以傳送包含下列內容的通知。此範例適用於`myPipeline`在 `us-west-2` 區域中名為 的管道，適用於階段 `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
    }
}
```

### 階段已停止事件
<a name="detect-state-events-stage-stopped"></a>

當階段執行停止時，會發出事件，以傳送包含下列內容的通知。此範例適用於`myPipeline`在 `us-west-2` 區域中名為 的管道，適用於階段 `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)
+ [動作失敗事件](#detect-state-events-action-failed)
+ [動作 ABANDONED 事件](#detect-state-events-action-abandoned)

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

動作執行開始時，會發出事件，以傳送包含下列內容的通知。此範例適用於 `myPipeline` `us-east-1`區域中名為 的管道，適用於部署動作 `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>

當動作執行成功時，會發出事件，以傳送包含下列內容的通知。此範例適用於 `"myPipeline"` `us-west-2`區域中名為 的管道，適用於來源動作 `"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": {}
}
```

------

### 動作失敗事件
<a name="detect-state-events-action-failed"></a>

當動作執行失敗時，會發出事件，以傳送包含下列內容的通知。此範例適用於 `"myPipeline"` `us-west-2`區域中名為 的管道，適用於動作 `"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>

捨棄動作執行時，會發出事件，以傳送包含下列內容的通知。此範例適用於 `"myPipeline"` `us-west-2`區域中名為 的管道，適用於動作 `"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>

規則會監看特定事件，然後將它們路由到您選擇的 AWS 目標。您可以建立規則，在發生其他 AWS 動作時自動執行 AWS 動作，或建立規則，以設定排程定期執行 AWS 動作。

**Topics**
+ [管道狀態變更時傳送通知 （主控台）](#monitoring-cloudwatch-events-console)
+ [管道狀態變更時傳送通知 (CLI)](#monitoring-cloudwatch-events-cli)

### 管道狀態變更時傳送通知 （主控台）
<a name="monitoring-cloudwatch-events-console"></a>

這些步驟說明如何使用 EventBridge 主控台建立規則，以傳送 CodePipeline 中變更的通知。

**使用 Amazon S3 來源建立以管道為目標的 EventBridge 規則**

1. 前往 [https://console.aws.amazon.com/events/](https://console.aws.amazon.com/events/) 開啟 Amazon EventBridge 主控台。

1. 在導覽窗格中，選擇**規則**。保持選取預設匯流排，或選擇事件匯流排。選擇**建立規則**。

1. 在**名稱**中，輸入規則的名稱。

1. 在**規則類型**下，選擇**具有事件模式的規則**。選擇**下一步**。

1. 在**事件模式**下，選擇 **AWS 服務**。

1. 從 **Event Type (事件類型)** 下拉式清單中，選擇通知的狀態變更層級。
   + 針對套用至管道層級事件的規則，選擇 **CodePipeline Pipeline Execution State Change (CodePipeline 管道執行狀態變更)**。
   + 針對套用至階段層級事件的規則，選擇 **CodePipeline Stage Execution State Change (CodePipeline 階段執行狀態變更)**。
   + 針對套用至動作層級事件的規則，選擇 **CodePipeline Action Execution State Change (CodePipeline 動作執行狀態變更)**。

1. 指定規則套用至的狀態變更：
   + 針對套用至所有狀態變更的規則，選擇 **Any state (任何狀態)**。
   + 針對僅套用到部分狀態變更的規則，選擇 **Specific state(s) (特定狀態)**，然後從清單中選擇一或多個狀態值。

1. 對於超出選取器允許範圍的事件模式，您也可以在**事件模式**視窗中使用**編輯**模式選項，以 JSON 格式指定事件模式。
**注意**  
如果未指定，則會針對所有管道/階段/動作和狀態建立事件模式。

   如需更詳細的事件模式，您可以將下列範例事件模式複製並貼到**事件模式**視窗中。
   +   
**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. 在**目標類型**中，選擇 **AWS 服務**。

1. 在**選取目標中**，選擇 **CodePipeline**。在**管道 ARN** 中，輸入管道 ARN，以便此規則啟動管道。
**注意**  
若要取得管道 ARN，請執行 **get-pipeline** 命令。管道 ARN 會出現在輸出中。其建構格式如下：  
arn：aws：codepipeline：{{region}}：{{account}}：{{pipeline-name}}  
範例管道 ARN：  
arn:aws:codepipeline:us-east-2:80398EXAMPLE:MyFirstPipeline 

1. 若要建立或指定 IAM 服務角色，以授予 EventBridge 調用與 EventBridge 規則相關聯目標的許可 （在此情況下，目標是 CodePipeline)：
   + 選擇**為此特定資源建立新角色**，以建立將 EventBridge 許可授予您啟動管道執行的服務角色。
   + 選擇**使用現有角色**來輸入服務角色，為 EventBridge 授予啟動管道執行的許可。

1. 選擇**下一步**。

1. 在**標籤**頁面上，選擇**下一步**。

1. 在**檢閱和建立**頁面上，檢閱規則組態。如果您對此規則感到滿意，請選擇 **Create rule (建立規則)**。

### 管道狀態變更時傳送通知 (CLI)
<a name="monitoring-cloudwatch-events-cli"></a>

這些步驟說明如何使用 CLI 來建立 CloudWatch Events 規則，以傳送 CodePipeline 中變更的通知。

若要使用 AWS CLI 建立規則，請呼叫 **put-rule**命令，並指定：
+ 可唯一識別您所建立規則的名稱。此名稱在您使用與 AWS 帳戶相關聯的 CodePipeline 建立的所有管道中必須是唯一的。
+ 規則所使用來源和詳細資訊欄位的事件模式。如需詳細資訊，請參閱 [Amazon EventBridge 和事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)。

**以 CodePipeline 做為事件來源來建立 EventBridge 規則**

1. 呼叫 **put-rule**​ 命令，以建立規則並指定事件模式 (如需有效狀態，請參閱上述各表)。

   下列範例命令使用 **--event-pattern**建立稱為 的規則`“MyPipelineStateChanges”`，當名為 "myPipeline" 的管道執行失敗時， 會發出 CloudWatch 事件。myPipeline."

   ```
   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.aws.amazon.com/eventbridge/latest/userguide/eb-use-resource-based.html)。