AWS CodeStar 使用 的通知範例 AWS CLI - AWS SDK 程式碼範例

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例

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

AWS CodeStar 使用 的通知範例 AWS CLI

下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 AWS CodeStar Notifications 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 create-notification-rule

AWS CLI

建立通知規則

下列create-notification-rule範例使用名為 的 JSON 檔案rule.json,為指定 AWS 帳戶中名為 MyNotificationRule 的儲存庫建立名為 MyDemoRepo 的通知規則。建立分支和標籤時,具有FULL詳細資訊類型的通知會傳送至指定的目標 Amazon SNS 主題。

aws codestar-notifications create-notification-rule \ --cli-input-json file://rule.json

rule.json 的內容:

{ "Name": "MyNotificationRule", "EventTypeIds": [ "codecommit-repository-branches-and-tags-created" ], "Resource": "arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo", "Targets": [ { "TargetType": "SNS", "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic" } ], "Status": "ENABLED", "DetailType": "FULL" }

輸出:

{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的建立通知規則

下列程式碼範例示範如何使用 delete-notification-rule

AWS CLI

刪除通知規則

下列delete-notification-rule範例會刪除指定的通知規則。

aws codestar-notifications delete-notification-rule \ --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE

輸出:

{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的刪除通知規則

下列程式碼範例示範如何使用 delete-target

AWS CLI

刪除通知規則目標

下列delete-target範例會從設定為使用它做為目標的所有通知規則中移除指定的目標,然後刪除目標。

aws codestar-notifications delete-target \ --target-address arn:aws:sns:us-east-1:123456789012:MyNotificationTopic \ --force-unsubscribe-all

此命令不會產生輸出。

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的刪除通知規則目標

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteTarget

下列程式碼範例示範如何使用 describe-notification-rule

AWS CLI

擷取通知規則的詳細資訊

下列describe-notification-rule範例會擷取指定通知規則的詳細資訊。

aws codestar-notifications describe-notification-rule \ --arn arn:aws:codestar-notifications:us-west-2:123456789012:notificationrule/dc82df7a-EXAMPLE

輸出:

{ "LastModifiedTimestamp": 1569199844.857, "EventTypes": [ { "ServiceName": "CodeCommit", "EventTypeName": "Branches and tags: Created", "ResourceType": "Repository", "EventTypeId": "codecommit-repository-branches-and-tags-created" } ], "Status": "ENABLED", "DetailType": "FULL", "Resource": "arn:aws:codecommit:us-west-2:123456789012:MyDemoRepo", "Arn": "arn:aws:codestar-notifications:us-west-w:123456789012:notificationrule/dc82df7a-EXAMPLE", "Targets": [ { "TargetStatus": "ACTIVE", "TargetAddress": "arn:aws:sns:us-west-2:123456789012:MyNotificationTopic", "TargetType": "SNS" } ], "Name": "MyNotificationRule", "CreatedTimestamp": 1569199844.857, "CreatedBy": "arn:aws:iam::123456789012:user/Mary_Major" }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的檢視通知規則

下列程式碼範例示範如何使用 list-event-types

AWS CLI

取得通知規則的事件類型清單

下列list-event-types範例會擷取 CodeDeploy 應用程式所有可用通知事件類型的篩選清單。如果不使用篩選條件,命令會傳回所有資源類型的所有通知事件類型。

aws codestar-notifications list-event-types \ --filters Name=SERVICE_NAME,Value=CodeDeploy

輸出:

{ "EventTypes": [ { "EventTypeId": "codedeploy-application-deployment-succeeded", "ServiceName": "CodeDeploy", "EventTypeName": "Deployment: Succeeded", "ResourceType": "Application" }, { "EventTypeId": "codedeploy-application-deployment-failed", "ServiceName": "CodeDeploy", "EventTypeName": "Deployment: Failed", "ResourceType": "Application" }, { "EventTypeId": "codedeploy-application-deployment-started", "ServiceName": "CodeDeploy", "EventTypeName": "Deployment: Started", "ResourceType": "Application" } ] }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的建立通知規則

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListEventTypes

下列程式碼範例示範如何使用 list-notification-rules

AWS CLI

擷取通知規則清單

下列list-notification-rules範例會擷取指定 AWS 區域中所有通知規則的清單。

aws codestar-notifications list-notification-rules --region us-east-1

輸出:

{ "NotificationRules": [ { "Id": "dc82df7a-EXAMPLE", "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }, { "Id": "8d1f0983-EXAMPLE", "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/8d1f0983-EXAMPLE" } ] }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的檢視通知規則

下列程式碼範例示範如何使用 list-tags-for-resource

AWS CLI

取得連接至通知規則的標籤清單

下列list-tags-for-resource範例會擷取連接至指定通知規則的所有標籤清單。在此範例中,通知規則目前沒有與其相關聯的標籤。

aws codestar-notifications list-tags-for-resource \ --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE

輸出:

{ "Tags": {} }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的建立通知規則

下列程式碼範例示範如何使用 list-targets

AWS CLI

擷取通知規則目標的清單

下列list-targets範例會擷取指定 AWS 區域中所有通知規則目標的清單。

aws codestar-notifications list-targets \ --region us-east-1

輸出:

{ "Targets": [ { "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MySNSTopicForNotificationRules", "TargetType": "SNS", "TargetStatus": "ACTIVE" }, { "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MySNSTopicForNotificationsAboutMyDemoRepo", "TargetType": "SNS", "TargetStatus": "ACTIVE" } ] }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的檢視通知規則目標

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListTargets

下列程式碼範例示範如何使用 subscribe

AWS CLI

將目標新增至通知規則

下列subscribe範例會將 Amazon SNS 主題新增為指定通知規則的目標。

aws codestar-notifications subscribe \ --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE \ --target TargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopic

輸出:

{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的新增或移除 Amazon SNS 主題做為通知規則的目標

  • 如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 Subscribe

下列程式碼範例示範如何使用 tag-resource

AWS CLI

將標籤新增至通知規則

下列tag-resource範例會將索引鍵名稱為 Team且值為 的標籤新增至Li_Juan指定的通知規則。

aws codestar-notifications tag-resource \ --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE \ --tags Team=Li_Juan

輸出:

{ "Tags": { "Team": "Li_Juan" } }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的建立通知規則

  • 如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 TagResource

下列程式碼範例示範如何使用 unsubscribe

AWS CLI

從通知規則中移除目標

下列unsubscribe範例會從指定的通知規則中移除 Amazon SNS 主題做為目標。

aws codestar-notifications unsubscribe \ --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE \ --target TargetType=SNS,TargetAddress=arn:aws:sns:us-east-1:123456789012:MyNotificationTopic

輸出:

{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic" }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的新增或移除 Amazon SNS 主題做為通知規則的目標

  • 如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 Unsubscribe

下列程式碼範例示範如何使用 untag-resource

AWS CLI

從通知規則移除標籤

下列untag-resource範例Team會從指定的通知規則中移除具有金鑰名稱的標籤。

aws codestar-notifications untag-resource \ --arn arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE \ --tag-keys Team

此命令不會產生輸出。

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的編輯通知規則

  • 如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 UntagResource

下列程式碼範例示範如何使用 update-notification-rule

AWS CLI

更新通知規則

下列update-notification-rule範例123456789012使用名為 MyNotificationRule的 JSON 檔案更新 AWS 帳戶中名為 的通知規則update.json

aws codestar-notifications update-notification-rule \ --cli-input-json file://update.json

update.json 的內容:

{ "Name": "MyUpdatedNotificationRule", "EventTypeIds": [ "codecommit-repository-branches-and-tags-created" ], "Resource": "arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo", "Targets": [ { "TargetType": "SNS", "TargetAddress": "arn:aws:sns:us-east-1:123456789012:MyNotificationTopic" } ], "Status": "ENABLED", "DetailType": "FULL" }

輸出:

{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }

如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的編輯通知規則