步骤和动作是 Keep 工作流引擎中工作流的构建块。虽然它们共享相似的结构和语法,但步骤和动作之间的区别主要是语义上的:
- 步骤:专注于从提供者查询数据或触发获取类操作(例如,查询数据库、获取日志或检索信息)。
- 动作:面向通知或触发结果,例如发送通知、更新工单或调用外部服务。
步骤和动作共同允许工作流既收集必要的数据又对其采取行动。
总体结构
步骤和动作使用类似的模式定义:
步骤
用于查询或获取数据。
步骤使用每个提供者的 _query 方法。
yaml
steps:
- name: <step-name>
provider:
type: <provider-type>
config: <provider-config>
with:
<provider-specific-parameters>动作
用于通知或触发效果。
动作使用每个提供者的 _notify 方法。
yaml
actions:
- name: <action-name>
provider:
type: <provider-type>
config: <provider-config>
with:
<provider-specific-parameters>示例
从 MySQL 数据库获取数据
yaml
steps:
- name: get-user-data
provider:
type: mysql
config: "{{ providers.mysql-prod }}"
with:
query: "SELECT * FROM users WHERE id = 1"
single_row: true从 Datadog 获取日志
yaml
steps:
- name: get-service-logs
provider:
type: datadog
config: "{{ providers.datadog }}"
with:
query: "service:keep and @error"
timeframe: "1h"从 Kubernetes 查询运行中的 pods
yaml
steps:
- name: get-pods
provider:
type: k8s
config: "{{ providers.k8s-cluster }}"
with:
command_type: get_pods发送电子邮件
yaml
actions:
- name: send-email
provider:
type: email
config: "{{ providers.email }}"
with:
to: "user@example.com"
subject: "Account Updated"
body: "Your account details have been updated."发送 Slack 消息
yaml
actions:
- name: notify-slack
provider:
type: slack
config: "{{ providers.slack-demo }}"
with:
message: "Critical alert received!"在 ServiceNow 中创建工单
yaml
actions:
- name: create-servicenow-ticket
provider:
type: servicenow
config: "{{ providers.servicenow }}"
with:
table_name: INCIDENT
payload:
short_description: "New incident created by Keep"
description: "Please investigate the issue."组合步骤和动作
工作流通常将步骤(用于查询数据)与动作(用于通知或结果)结合使用。
以下是几个示例:
查询并通知
yaml
workflow:
id: query-and-notify
description: "Query a database and notify via Slack"
steps:
- name: get-user-data
provider:
type: mysql
config: "{{ providers.mysql-prod }}"
with:
query: "SELECT email FROM users WHERE id = 1"
single_row: true
actions:
- name: send-notification
provider:
type: slack
config: "{{ providers.slack-demo }}"
with:
message: "User email: {{ steps.get-user-data.results.email }}"告警和事件管理
yaml
workflow:
id: alert-management
description: "Handle alerts and create incidents"
steps:
- name: get-alert-details
provider:
type: datadog
config: "{{ providers.datadog }}"
with:
query: "service:keep and @alert"
timeframe: "1h"
actions:
- name: create-incident
provider:
type: servicenow
config: "{{ providers.servicenow }}"
with:
table_name: INCIDENT
payload:
short_description: "Alert from Datadog: {{ steps.get-alert-details.results.alert_name }}"
description: "Details: {{ steps.get-alert-details.results.alert_description }}"错误处理和重试
步骤和动作都支持错误处理,以确保工作流能够从故障中恢复。
yaml
steps:
- name: fetch-data
provider:
type: http
with:
url: "https://api.example.com/data"
on-failure:
retry:
count: 3
# Retry every 5 seconds
interval: 5