ACS::ExecuteHttpRequest

用途

使用该动作可以在运维任务中调用外部HTTP/HTTPS请求,以实现云资源运维任务与外部HTTP服务的交互。

以下是 ACS::ExecuteHttpRequest 动作的一些可能的场景:

  1. API 集成:您可以使用 ACS::ExecuteHttpRequest 动作调用第三方服务API,从而查询信息、执行操作或集成第三方服务。

  2. 自定义通知:如果您需要在脚本执行的特定阶段发送通知,可以通过 ACS::ExecuteHttpRequest 发送 HTTP 请求到自定义的Webhook或通知服务,实现诸如发送邮件、短信或推送通知等功能。

  3. 数据收集与报告:借助 ACS::ExecuteHttpRequest 动作,您可以将运维任务执行信息发送到远程服务器或数据平台,以用于审计、监控或报告。

  4. 自动化流程触发:您可以利用 ACS::ExecuteHttpRequest 在 OOS 模板执行时触发其他自动化工具或脚本的执行,如调用 CI/CD 系统中的部署流程,或者启动其他外部系统的运维任务。

  5. 外部验证与授权:在运维过程中,您可能需要对操作进行验证。您可以通过 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": {}
      }
    }
  ]
}