gammarers-aws-cdk-resources/aws-codepipeline-state-change-detection-event-rules
GitHub: gammarers-aws-cdk-resources/aws-codepipeline-state-change-detection-event-rules
一个 AWS CDK 构造库,用于定义 EventBridge 规则以检测 CodePipeline 流水线与阶段的执行状态变更。
Stars: 0 | Forks: 0
# AWS CodePipeline 状态变更检测事件规则
[](https://github.com/gammarers-aws-cdk-resources/aws-codepipeline-state-change-detection-event-rules/blob/main/LICENSE)
[](https://www.npmjs.com/package/aws-codepipeline-state-change-detection-event-rules)
[](https://github.com/gammarers-aws-cdk-resources/aws-codepipeline-state-change-detection-event-rules/actions/workflows/release.yml)
[](https://github.com/gammarers-aws-cdk-resources/aws-codepipeline-state-change-detection-event-rules/releases)
[](https://constructs.dev/packages/aws-codepipeline-state-change-detection-event-rules)
一个 AWS CDK 构造库,用于定义 EventBridge 规则,以检测 AWS CodePipeline 中的流水线与阶段执行状态变更。
## 提供的构造
| 构造 | 描述 |
|-----------|-------------|
| `CodePipelinePipelineExecutionStateChangeDetectionEventRule` | 检测流水线级执行状态变更(已开始、成功、失败等)的规则 |
| `CodePipelineStageExecutionStateChangeDetectionEventRule` | 检测每个阶段执行状态变更的规则 |
两者均扩展 `events.Rule`,并允许通过 `targetStates` 按状态过滤(未指定时包含所有状态)。事件模式由每个构造固定;传入 `eventPattern` 到属性中不被支持,运行时将抛出异常。
## 安装
### TypeScript
**npm:**
```
npm install aws-codepipeline-state-change-detection-event-rules
```
**yarn:**
```
yarn add aws-codepipeline-state-change-detection-event-rules
```
## 示例
### 流水线执行状态变更检测
示例:在流水线执行状态(已开始、成功、失败、取消等)变更时调用 Lambda。当指定 `targetStates` 时,仅在这些状态的目标会被调用。
```
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as events_targets from 'aws-cdk-lib/aws-events-targets';
import {
CodePipelinePipelineExecutionState,
CodePipelinePipelineExecutionStateChangeDetectionEventRule,
} from 'aws-codepipeline-state-change-detection-event-rules';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
const fn = new lambda.Function(stack, 'MyFunc', {
runtime: lambda.Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = async () => {}`),
});
const rule = new CodePipelinePipelineExecutionStateChangeDetectionEventRule(
stack,
'PipelineStateChangeRule',
{
ruleName: 'my-pipeline-state-change-rule',
targets: [new events_targets.LambdaFunction(fn)],
// When omitted, all states. When specified, triggers only for those states.
targetStates: [
CodePipelinePipelineExecutionState.FAILED,
CodePipelinePipelineExecutionState.SUCCEEDED,
],
}
);
```
### 阶段执行状态变更检测
示例:检测每个阶段的执行状态变更并调用 Lambda。
```
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as events_targets from 'aws-cdk-lib/aws-events-targets';
import {
CodePipelineStageExecutionState,
CodePipelineStageExecutionStateChangeDetectionEventRule,
} from 'aws-codepipeline-state-change-detection-event-rules';
const app = new cdk.App();
const stack = new cdk.Stack(app, 'MyStack');
const fn = new lambda.Function(stack, 'MyFunc', {
runtime: lambda.Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = async () => {}`),
});
const rule = new CodePipelineStageExecutionStateChangeDetectionEventRule(
stack,
'StageStateChangeRule',
{
ruleName: 'my-stage-state-change-rule',
targets: [new events_targets.LambdaFunction(fn)],
targetStates: [
CodePipelineStageExecutionState.FAILED,
CodePipelineStageExecutionState.SUCCEEDED,
],
}
);
```
## 可检测状态
### 流水线执行状态(CodePipelinePipelineExecutionState)
`CANCELED` / `FAILED` / `RESUMED` / `STARTED` / `STOPPED` / `STOPPING` / `SUCCEEDED` / `SUPERSEDED`
### 阶段执行状态(CodePipelineStageExecutionState)
`CANCELED` / `FAILED` / `RESUMED` / `STARTED` / `STOPPED` / `STOPPING` / `SUCCEEDED`
## API 参考
详见 [API.md](./API.md)。
## 许可证
本项目根据 Apache-2.0 License 授权。
标签:AWS, CDK, Cilium, CloudFormation, CodePipeline, DPI, EventBridge, NPM, 事件桥, 事件模式, 事件规则, 事件驱动, 代理支持, 告警, 开源框架, 持续部署, 持续集成, 构造库, 流水线, 状态检测, 监控, 网络调试, 自动化, 自动化攻击, 资源库