文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS CodeStar 使用 的通知範例 AWS CLI
下列程式碼範例示範如何使用 AWS Command Line Interface 搭配 AWS CodeStar 通知來執行動作和實作常見案例。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
主題
動作
以下程式碼範例顯示如何使用 create-notification-rule。
- AWS CLI
-
建立通知規則
下列
create-notification-rule範例使用名為 的 JSON 檔案rule.json,MyNotificationRule為指定 AWS 帳戶中名為 的儲存庫建立名為MyDemoRepo的通知規則。建立分支和標籤時,具有FULL詳細資訊類型的通知會傳送至指定的目標 Amazon SNS 主題。aws codestar-notifications create-notification-rule \ --cli-input-jsonfile://rule.jsonrule.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 開發人員工具主控台使用者指南》中的建立通知規則。
-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 CreateNotificationRule
。
-
以下程式碼範例顯示如何使用 delete-notification-rule。
- AWS CLI
-
刪除通知規則
下列
delete-notification-rule範例會刪除指定的通知規則。aws codestar-notifications delete-notification-rule \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE輸出:
{ "Arn": "arn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE" }如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的刪除通知規則。
-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 DeleteNotificationRule
。
-
以下程式碼範例顯示如何使用 delete-target。
- AWS CLI
-
刪除通知規則目標
下列
delete-target範例會從設定為使用它做為目標的所有通知規則中移除指定的目標,然後刪除目標。aws codestar-notifications delete-target \ --target-addressarn: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 \ --arnarn: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 開發人員工具主控台使用者指南》中的檢視通知規則。
-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 DescribeNotificationRule
。
-
以下程式碼範例顯示如何使用 list-event-types。
- AWS CLI
-
取得通知規則的事件類型清單
下列
list-event-types範例會擷取 CodeDeploy 應用程式所有可用通知事件類型的篩選清單。如果您不使用篩選條件,命令會傳回所有資源類型的所有通知事件類型。aws codestar-notifications list-event-types \ --filtersName=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 --regionus-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 開發人員工具主控台使用者指南》中的檢視通知規則。
-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 ListNotificationRules
。
-
以下程式碼範例顯示如何使用 list-tags-for-resource。
- AWS CLI
-
取得連接至通知規則的標籤清單
下列
list-tags-for-resource範例會擷取連接至指定通知規則的所有標籤清單。在此範例中,通知規則目前沒有與其相關聯的標籤。aws codestar-notifications list-tags-for-resource \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE輸出:
{ "Tags": {} }如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的建立通知規則。
-
如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 ListTagsForResource
。
-
以下程式碼範例顯示如何使用 list-targets。
- AWS CLI
-
擷取通知規則目標的清單
下列
list-targets範例會擷取指定區域中所有通知規則目標的清單 AWS 。aws codestar-notifications list-targets \ --regionus-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 \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE\ --targetTargetType=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 \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE\ --tagsTeam=Li_Juan輸出:
{ "Tags": { "Team": "Li_Juan" } }如需詳細資訊,請參閱《 AWS 開發人員工具主控台使用者指南》中的建立通知規則。
-
如需 API 詳細資訊,請參閱《AWS CLI 命令參考》中的 TagResource
。
-
以下程式碼範例顯示如何使用 unsubscribe。
- AWS CLI
-
從通知規則中移除目標
下列
unsubscribe範例會從指定的通知規則中移除 Amazon SNS 主題做為目標。aws codestar-notifications unsubscribe \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/dc82df7a-EXAMPLE\ --targetTargetType=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 \ --arnarn:aws:codestar-notifications:us-east-1:123456789012:notificationrule/fe1efd35-EXAMPLE\ --tag-keysTeam此命令不會產生輸出。
如需詳細資訊,請參閱《 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-jsonfile://update.jsonupdate.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 開發人員工具主控台使用者指南》中的編輯通知規則。
-
如需 API 詳細資訊,請參閱《 AWS CLI 命令參考》中的 UpdateNotificationRule
。
-