接入教程
1. 登录AWS控制台并进入CodeStar Notifications服务
2. 创建通知规则并选择触发事件类型(如代码提交或部署)
3. 配置通知目标(如SNS主题或Slack频道)
4. 设置筛选条件以过滤特定事件
5. 测试通知规则并启用监控
6. 通过API或控制台管理规则生命周期
使用Python创建通知规则
import boto3
from botocore.config import Config
# 配置客户端
config = Config(region_name='us-east-1')
client = boto3.client('codestar-notifications',
config=config,
aws_access_key_id='YOUR_API_KEY',
aws_secret_access_key='YOUR_SECRET_KEY')
# 创建通知规则
response = client.create_notification_rule(
Name='MyNotificationRule',
EventTypeIds=[
'codecommit-repository-comments'
],
Resource='arn:aws:codecommit:us-east-1:123456789012:MyDemoRepo',
Targets=[
{
'TargetType': 'SNS',
'TargetAddress': 'arn:aws:sns:us-east-1:123456789012:MyTopic'
}
],
DetailType='BASIC',
Status='ENABLED'
)
print(f"规则ARN: {response['Arn']}")
使用PHP列出通知规则
<?php
require 'vendor/autoload.php';
use Aws\CodeStarNotifications\CodeStarNotificationsClient;
use Aws\Exception\AwsException;
// 创建客户端
$client = new CodeStarNotificationsClient([
'region' => 'us-east-1',
'version' => 'latest',
'credentials' => [
'key' => 'YOUR_API_KEY',
'secret' => 'YOUR_SECRET_KEY'
]
]);
try {
// 列出通知规则
$result = $client->listNotificationRules([
'Filters' => [
[
'Name' => 'EventTypeId',
'Value' => 'codecommit-repository-comments'
]
],
'MaxResults' => 10
]);
echo "找到规则: " . count($result['NotificationRules']) . "\n";
foreach ($result['NotificationRules'] as $rule) {
echo "规则ARN: " . $rule['Arn'] . "\n";
}
} catch (AwsException $e) {
echo "错误: " . $e->getMessage() . "\n";
}
?>
使用JavaScript删除通知规则
const AWS = require('aws-sdk');
// 配置AWS SDK
AWS.config.update({
region: 'us-east-1',
accessKeyId: 'YOUR_API_KEY',
secretAccessKey: 'YOUR_SECRET_KEY'
});
const codestarNotifications = new AWS.CodeStarNotifications({apiVersion: '2019-10-15'});
// 删除通知规则
async function deleteNotificationRule(ruleArn) {
try {
const params = {
Arn: ruleArn
};
const response = await codestarNotifications.deleteNotificationRule(params).promise();
console.log(`成功删除规则: ${ruleArn}`);
return response;
} catch (error) {
console.error('删除失败:', error);
throw error;
}
}
// 使用示例
const ruleArn = 'arn:aws:codestar-notifications:us-east-1:123456789012:notification-rule/abc123';
deleteNotificationRule(ruleArn);
常见问题
AWS CodeStar Notifications支持哪些事件类型?
该服务支持多种AWS开发工具的事件,包括CodeCommit代码提交、CodeBuild构建状态、CodePipeline流水线执行等事件类型。具体事件类型ID可在AWS官方文档中查看。
通知规则可以发送到哪些目标?
通知规则支持发送到多种目标,包括Amazon SNS主题、AWS Chatbot聊天频道、以及通过webhook集成到第三方服务。每个规则最多可以配置10个目标。
如何监控通知规则的送达状态?
可以通过AWS CloudTrail日志查看API调用记录,或使用Amazon CloudWatch监控通知发送指标。每个通知规则的状态变更和发送尝试都会被记录。
Aitishiku.com