用途
使用該動作可以在運維任務中調用外部HTTP/HTTPS請求,以實現云資源運維任務與外部HTTP服務的交互。
以下是 ACS::ExecuteHttpRequest 動作的一些可能的場景:
API 集成:您可以使用 ACS::ExecuteHttpRequest 動作調用第三方服務API,從而查詢信息、執行操作或集成第三方服務。
自定義通知:如果您需要在腳本執行的特定階段發送通知,可以通過 ACS::ExecuteHttpRequest 發送 HTTP 請求到自定義的Webhook或通知服務,實現諸如發送郵件、短信或推送通知等功能。
數據收集與報告:借助 ACS::ExecuteHttpRequest 動作,您可以將運維任務執行信息發送到遠程服務器或數據平臺,以用于審計、監控或報告。
自動化流程觸發:您可以利用 ACS::ExecuteHttpRequest 在 OOS 模板執行時觸發其他自動化工具或腳本的執行,如調用 CI/CD 系統中的部署流程,或者啟動其他外部系統的運維任務。
外部驗證與授權:在運維過程中,您可能需要對操作進行驗證。您可以通過 ACS::ExecuteHttpRequest 向外部認證系統發送請求以驗證操作者的身份或校驗操作權限。
語法
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
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"
}
},
"Outputs": {
"OutputParameter1": {
"ValueSelector": "jq selector",
"Type": "String/Boolean/List(Array)/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": {}
}
}
]
}
文檔內容是否對您有幫助?