用途
使用该动作可以在运维任务中调用外部HTTP/HTTPS请求,以实现云资源运维任务与外部HTTP服务的交互。
以下是 ACS::ExecuteHttpRequest 动作的一些可能的场景:
API 集成:您可以使用 ACS::ExecuteHttpRequest 动作调用第三方服务API,从而查询信息、执行操作或集成第三方服务。
自定义通知:如果您需要在脚本执行的特定阶段发送通知,可以通过 ACS::ExecuteHttpRequest 发送 HTTP 请求到自定义的Webhook或通知服务,实现诸如发送邮件、短信或推送通知等功能。
数据收集与报告:借助 ACS::ExecuteHttpRequest 动作,您可以将运维任务执行信息发送到远程服务器或数据平台,以用于审计、监控或报告。
自动化流程触发:您可以利用 ACS::ExecuteHttpRequest 在 OOS 模板执行时触发其他自动化工具或脚本的执行,如调用 CI/CD 系统中的部署流程,或者启动其他外部系统的运维任务。
外部验证与授权:在运维过程中,您可能需要对操作进行验证。您可以通过 ACS::ExecuteHttpRequest 向外部认证系统发送请求以验证操作者的身份或校验操作权限。
等待异步任务完成或校验调用结果:在调用HTTP请求时,存在一些异步任务场景,需要轮询检查Response的特殊字段,或针对调用结果进行校验,您可以通过WaitFor/CheckFor字段实现。
语法
Tasks:
- Name: executeHttpRequestExample
Action: ACS::ExecuteHttpRequest
Properties:
Method: POST # 可选,HTTP方法,支持POST和GET,默认为POST
URL: 'https://example.com' # 必填,HTTP请求的统一资源定位符
Headers: # 可选,HTTP请求头信息
Content-Type: 'application/json'
Query: # 可选,HTTP请求参数信息
Parameter1: value1
Parameter2: value2
Body: # 可选,HTTP请求体,当Method为POST时有效
Parameter3: value3
Parameter4: value4
CheckFor: # 可选,对Http请求返回结果进行校验
# Rules下可添加多个对比组别,组别之间为"AND"关系
Rules:
- PropertySelector: "jq selector" # JQ语法
Operator: "Equals" # 比较器,支持"Equals", "In", "NotIn"
Value: "1" #期望值
WaitFor: # 可选,对Http请求返回结果进行轮询校验
# Rules同上
Rules:
- PropertySelector: "jq selector"
Operator: "Equals"
Value: "1"
FailRules:
# 满足条件直接失败
- PropertySelector: "jq selector"
Operator: "Equals"
Value: "1"
# Retry模块设置重试相关参数
Retry:
Retries: #可选,表示最大重试次数,默认值10,填写正整数,如5,取值范围[0,300]。
DelayType: #Exponential(默认),Constant,Linear。Exponential类型的重试间隔为:2 ^ times(重试次数);Constant类型的重试间隔固定为Delay;Linear类型的重试间隔为:Delay + BackOff * times(重试次数)。
Delay: #DelayType为Constant或Linear时需要添加此属性,表示重试间隔时间,填写正整数,如10,表示重试间隔为10s,默认值2,取值范围[1,3600]。
BackOff: #DelayType为Linear时需要添加此属性,表示重试间隔补偿时间,填写正整数,默认值为2,取值范围[1,3600]。
MaxRetryInterval: #最大重试间隔, 单位秒,默认值为1800,填写正整数,取值范围[1, 1800]。
Outputs:
OutputParameter1:
ValueSelector: 'jq selector' # jq的选择器语法,以OpenAPI的返回作为JSON输入,jq的语法请参考 https://stedolan.github.io/jq/
Type: String/Boolean/List/Number/Object
{
"Tasks": [
{
"Name": "executeHttpRequestExample",
"Action": "ACS::ExecuteHttpRequest",
"Properties": {
"Method": "POST",
"URL": "https://example.com",
"Headers": {
"Content-Type": "application/json"
},
"Query": {
"Parameter1": "value1",
"Parameter2": "value2"
},
"Body": {
"Parameter3": "value3",
"Parameter4": "value4"
},
"CheckFor": {
"Rules": [
{
"PropertySelector": "jq selector",
"Operator": "Equals",
"Value": "1"
}
]
},
"WaitFor": {
"Rules": [
{
"PropertySelector": "jq selector",
"Operator": "Equals",
"Value": "1"
}
],
"FailRules": [
{
"PropertySelector": "jq selector",
"Operator": "Equals",
"Value": "1"
}
],
"Retry": {
"Retries": null,
"DelayType": null,
"Delay": null,
"BackOff": null,
"MaxRetryInterval": null
}
}
},
"Outputs": {
"OutputParameter1": {
"ValueSelector": "jq selector",
"Type": "String/Boolean/List/Number/Object"
}
}
}
]
}
示例
以下为通过调用HTTP请求触发github action将分支代码部署到指定实例的示例模板。
FormatVersion: OOS-2019-06-01
Description:
en: OOS template for deploying ECS instances using GitHub Actions.
zh-cn: OOS模板,用于使用GitHub Actions部署ECS实例
Parameters:
regionId:
Type: String
Label:
en: RegionId
zh-cn: 地域ID
AssociationProperty: RegionId
Default: '{{ ACS::RegionId }}'
targets:
Type: Json
Label:
en: TargetInstance
zh-cn: 目标实例
AssociationProperty: Targets
AssociationPropertyMetadata:
ResourceType: ALIYUN::ECS::Instance
RegionId: regionId
Status: Running
gitHubBranch:
Type: String
Description:
en: Branch where the deployment will occur.
zh-cn: 要部署的git仓库分支
Tasks:
- Name: getInstance
Description:
en: Views the ECS instances
zh-cn: 获取ECS实例
Action: ACS::SelectTargets
Properties:
ResourceType: ALIYUN::ECS::Instance
RegionId: '{{ regionId }}'
Filters:
- '{{ targets }}'
Outputs:
instanceIds:
Type: List
ValueSelector: Instances.Instance[].InstanceId
- Name: DeployCodeByGitHubAction
Action: ACS::ExecuteHttpRequest
Properties:
Method: POST
URL: "https://api.github.com/repos/<YOUR-GITHUB-ACCOUNT>/<YOUR-GITHUB-REPO>/actions/workflows/<YOUR-WORKFLOW-ID>/dispatches"
Headers:
Accept: application/vnd.github+json
Authorization: "Bearer <YOUR-TOKEN>"
X-GitHub-Api-Version: 2022-11-28
Body:
ref: "{{gitHubBranch}}"
inputs:
instance_ids: "{{getInstance.instanceIds}}"
{
"FormatVersion": "OOS-2019-06-01",
"Description": {
"en": "OOS template for deploying ECS instances using GitHub Actions.",
"zh-cn": "OOS模板,用于使用GitHub Actions部署ECS实例"
},
"Parameters": {
"regionId": {
"Type": "String",
"Label": {
"en": "RegionId",
"zh-cn": "地域ID"
},
"AssociationProperty": "RegionId",
"Default": "{{ ACS::RegionId }}"
},
"targets": {
"Type": "Json",
"Label": {
"en": "TargetInstance",
"zh-cn": "目标实例"
},
"AssociationProperty": "Targets",
"AssociationPropertyMetadata": {
"ResourceType": "ALIYUN::ECS::Instance",
"RegionId": "regionId",
"Status": "Running"
}
},
"gitHubBranch": {
"Type": "String",
"Description": {
"en": "Branch where the deployment will occur.",
"zh-cn": "要部署的git仓库分支"
}
}
},
"Tasks": [
{
"Name": "getInstance",
"Description": {
"en": "Views the ECS instances",
"zh-cn": "获取ECS实例"
},
"Action": "ACS::SelectTargets",
"Properties": {
"ResourceType": "ALIYUN::ECS::Instance",
"RegionId": "{{ regionId }}",
"Filters": [
"{{ targets }}"
]
},
"Outputs": {
"instanceIds": {
"Type": "List",
"ValueSelector": "Instances.Instance[].InstanceId"
}
}
},
{
"Name": "DeployCodeByGitHubAction",
"Action": "ACS::ExecuteHttpRequest",
"Properties": {
"Method": "POST",
"URL": "https://api.github.com/repos/<YOUR-GITHUB-ACCOUNT>/<YOUR-GITHUB-REPO>/actions/workflows/<YOUR-WORKFLOW-ID>/dispatches",
"Headers": {
"Accept": "application/vnd.github+json",
"Authorization": "Bearer <YOUR-TOKEN>",
"X-GitHub-Api-Version": "2022-11-28T00:00:00.000Z"
},
"Body": {
"ref": "{{gitHubBranch}}",
"inputs": {
"instance_ids": "{{getInstance.instanceIds}}"
}
},
"Query": {}
}
}
]
}
该文章对您有帮助吗?