AWS CodeStar 使用通知示例 Amazon CLI - Amazon Command Line Interface
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

AWS CodeStar 使用通知示例 Amazon CLI

以下代码示例向您展示了如何使用 with Notifications 来执行操作和实现常见场 AWS CodeStar 景。 Amazon Command Line Interface

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景和跨服务示例的上下文查看操作。

场景是展示如何通过在同一服务中调用多个函数来完成特定任务任务的代码示例。

每个示例都包含一个指向的链接 GitHub,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何使用 create-notification-rule

Amazon CLI

创建通知规则

以下create-notification-rule示例使用名为的 JSON 文件rule.json为指定 Amazon 账户MyDemoRepo中名MyNotificationRule为的存储库创建名为的通知规则。创建分支和标签时,带有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" }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的创建通知规则

以下代码示例演示如何使用 delete-notification-rule

Amazon 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" }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的删除通知规则

以下代码示例演示如何使用 delete-target

Amazon CLI

删除通知规则目标

以下delete-target示例将指定目标从所有配置为用作目标的通知规则中移除,然后删除该目标。

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

此命令不生成任何输出。

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的删除通知规则目标

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考DeleteTarget中的。

以下代码示例演示如何使用 describe-notification-rule

Amazon 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" }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的 “查看通知规则”。

以下代码示例演示如何使用 list-event-types

Amazon 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" } ] }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的创建通知规则

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考ListEventTypes中的。

以下代码示例演示如何使用 list-notification-rules

Amazon CLI

检索通知规则列表

以下list-notification-rules示例检索指定 Amazon 区域中所有通知规则的列表。

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" } ] }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的 “查看通知规则”。

以下代码示例演示如何使用 list-tags-for-resource

Amazon 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": {} }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的创建通知规则

以下代码示例演示如何使用 list-targets

Amazon CLI

检索通知规则目标列表

以下list-targets示例检索指定 Amazon 区域中所有通知规则目标的列表。

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" } ] }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的查看通知规则目标

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考ListTargets中的。

以下代码示例演示如何使用 subscribe

Amazon 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" }

有关更多信息,请参阅Amazon 开发者工具控制台用户指南中的添加或删除作为通知规则目标的 Amazon SNS 主题

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 Subscribe

以下代码示例演示如何使用 tag-resource

Amazon 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" } }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的创建通知规则

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考TagResource中的。

以下代码示例演示如何使用 unsubscribe

Amazon 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" }

有关更多信息,请参阅Amazon 开发者工具控制台用户指南中的添加或删除作为通知规则目标的 Amazon SNS 主题

  • 有关 API 详细信息,请参阅《Amazon CLI Command Reference》中的 Unsubscribe

以下代码示例演示如何使用 untag-resource

Amazon CLI

从通知规则中移除标签

以下untag-resource示例Team从指定的通知规则中删除带有密钥名称的标签。

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

此命令不生成任何输出。

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的编辑通知规则

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考UntagResource中的。

以下代码示例演示如何使用 update-notification-rule

Amazon CLI

更新通知规则

以下update-notification-rule示例123456789012使用名为的 JSON 文件更新 Amazon 账户MyNotificationRule中名为的通知规则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" }

有关更多信息,请参阅《Amazon 开发者工具控制台用户指南》中的编辑通知规则