在執行要求中傳遞執行階段引數

您可以在工作流程執行要求中傳遞執行階段引數,並使用工作流程變數存取這些引數。

設定接收執行階段引數的工作流程

如要設定工作流程,接收您在執行要求中傳遞的執行階段引數,請按照下列步驟操作:

  1. 按照步驟建立新工作流程,或選擇現有工作流程來更新,但請勿部署。

  2. 在主要工作流程的定義中新增 params 欄位。請確認引數名稱放在方括號內,且主要工作流程放在 main 區塊中:

    YAML

    main:
      params: [ARG_NAME]
      steps:
        ...

    JSON

    {
      "main": {
        "params": ["ARG_NAME"],
        "steps": [
          ...
        ]
        ...
      }
    }
      

    main 區塊接受單一引數,即任何有效的 JSON 資料類型名稱,例如陣列、物件或字串。

    最佳做法是傳遞含有多個具名引數的物件,這樣比較容易瞭解引數用途,也方便新增引數。您也可以使用點記號存取引數。

    其他子工作流程可以有多個引數。

    舉例來說,下列工作流程會向您以執行階段引數形式傳遞姓名的人員傳回「Hello」問候語:

    YAML

    main:
      params: [args]
      steps:
        - step1:
            assign:
              - outputVar: ${"Hello, " + args.firstName + " " + args.lastName + "!"}
        - step2:
            return: ${outputVar}

    JSON

    {
      "main": {
        "params": [
          "args"
        ],
        "steps": [
          {
            "step1": {
              "assign": [
                {
                  "outputVar": "${\"Hello \" + args.firstName + \" \" + args.lastName}"
                }
              ]
            }
          },
          {
            "step2": {
              "return": "${outputVar}"
            }
          }
        ]
      }
    }
      
  3. 部署工作流程,完成建立或更新程序。

在執行要求中傳遞資料

設定工作流程以接收執行階段引數後,您可以在執行要求中,將 JSON 格式的字串 (例如 {"firstName":"Workflows","lastName":"User"}) 傳送至工作流程。

控制台

  1. 如要執行工作流程,請前往 Google Cloud 控制台的「Workflows」頁面:

    前往「Workflows」頁面

  2. 在「Workflows」頁面中選取工作流程,前往詳細資料頁面。

  3. 在「Workflow Details」(工作流程詳細資料) 頁面中,按一下 「Execute」(執行)

  4. 在顯示的「執行工作流程」頁面中,於「輸入」區域輸入包含參數名稱和引數值的 JSON 字串,例如 {"firstName":"Workflows","lastName":"User"}

    輸入區域會填入 JSON 字串範例

  5. 按一下 [Execute] (執行)

    在「執行詳細資料」頁面中,您可以查看執行結果,包括輸出內容 Hello, Workflows User!、執行 ID 和狀態,以及工作流程執行的目前或最後一個步驟。詳情請參閱「存取工作流程執行結果」。

gcloud

在用於執行工作流程的 gcloud workflows execute 指令中,加入 --data 標記。這個標記會採用資料的 JSON 字串。舉例來說,如要將 firstNamelastName 傳遞至先前的範例工作流程:

gcloud workflows run WORKFLOW_NAME \
--data='{"firstName":"FIRST","lastName":"LAST"}'

更改下列內容:

  • WORKFLOW_NAME:工作流程名稱
  • FIRST:要傳遞至工作流程的字串,用於 firstName
  • LAST:要傳遞至工作流程的字串,用於 lastName

畫面會顯示如下的輸出內容:

Waiting for execution [9379b067-306a-4db1-a58d-c9fc99aebfd4] to complete...done.
argument: '{"firstName":"Workflows","lastName":"User"}'
endTime: '2022-07-19T13:52:47.659199466Z'
name: projects/1051295516635/locations/us-central1/workflows/workflow-6/executions/9379b067-306a-4db1-a58d-c9fc99aebfd4
result: '"Hello, Workflows User!"'
startTime: '2022-07-19T13:52:47.623862835Z'
state: SUCCEEDED
status:
  currentSteps:
  - routine: main
    step: step2
workflowRevisionId: 000002-138

用戶端程式庫

視用戶端程式庫語言而定,您可以在執行要求中傳遞執行階段引數。

舉例來說,使用 JavaScript:

// Execute workflow
try {
  const createExecutionRes = await client.createExecution({
    parent: client.workflowPath(projectId, location, workflow),
    execution: {
      argument: JSON.stringify({"firstName":"Workflows","lastName":"User"})
    }
});
const executionName = createExecutionRes[0].name;

或者,使用 Java:

// Creates the execution object.
CreateExecutionRequest request =
    CreateExecutionRequest.newBuilder()
        .setParent(parent.toString())
        .setExecution(Execution.newBuilder().setArgument("{\"firstName\":\"Workflows\",\"lastName\":\"User\"}").build())
        .build();

如要進一步瞭解如何使用 Google API 用戶端程式庫執行工作流程,請參閱「執行工作流程」。

REST API

  1. 在用於執行工作流程的指令中附加 data 旗標。data 的值是 JSON 格式的字串,其中包含一個引數,該引數的值是一或多個逸出的參數值組合。舉例來說,如要將 firstNamelastName 傳遞至上一個範例工作流程:

    curl --request POST \
       --header "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
       --header 'Content-Type: application/json' \
       --data '{"argument":"{\"firstName\":\"FIRST\",\"lastName\":\";LAST\"}"}' \
       "https://workflowexecutions.googleapis.com/v1/projects/PROJECT_NUMBER/locations/us-central1/workflows/WORKFLOW_NAME/executions"

    更改下列內容:

    • PROJECT_NUMBER:您的 Google Cloud 專案編號
    • WORKFLOW_NAME:工作流程名稱
    • FIRST:要傳遞至 firstName 工作流程的字串
    • LAST:要傳遞至 lastName 工作流程的字串

    畫面會顯示如下的輸出內容:

    {
      "name": "projects/PROJECT_NUMBER/locations/us-central1/workflows/WORKFLOW_NAME/executions/EXECUTION_ID",
      "startTime": "2020-11-09T23:51:31.765761331Z",
      "state": "ACTIVE",
      "argument": "{\"firstName\":\"Workflows\",\"lastName\":\"User\"}",
      "workflowRevisionId": "000001-08c"
    }
     ```
    
  2. 如要取得執行結果,請執行下列指令:

    curl --request GET \
       --header "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
       --header 'Content-Type: application/json' \
       "https://workflowexecutions.googleapis.com/v1/projects/PROJECT_NUMBER/locations/us-central1/workflows/WORKFLOW_NAME/executions/EXECUTION_ID"
    

    EXECUTION_ID 取代為第一個指令傳回的輸出內容中的 ID。

    輸出內容會包含類似下列內容的行:

    "argument": "{\"firstName\":\"Workflows\",\"lastName\":\"User\"}",
    "result": "\"Hello, Workflows User!\"",
    

如要進一步瞭解如何使用 REST API 執行工作流程,請參閱執行工作流程

存取執行階段引數

這個範例會存取傳遞至工作流程的執行階段引數,這些引數是執行要求的一部分。所有引數都儲存在同一個對應中,並宣告為主要工作流程的參數。

執行這項工作流程時,請使用下列格式傳遞執行階段引數:

{"firstName":"Sherlock", "lastName":"Holmes"}

YAML

main:
  params: [args]
  steps:
    - step1:
        assign:
          - outputVar: ${"Hello " + args.firstName + " " + args.lastName}
    - step2:
        return: ${outputVar}

JSON

{
  "main": {
    "params": [
      "args"
    ],
    "steps": [
      {
        "step1": {
          "assign": [
            {
              "outputVar": "${\"Hello \" + args.firstName + \" \" + args.lastName}"
            }
          ]
        }
      },
      {
        "step2": {
          "return": "${outputVar}"
        }
      }
    ]
  }
}

您可以搭配標準程式庫函式 map.get 使用 default,存取選用的執行階段引數,並在找不到鍵時傳回預設值。在以下範例中,如果未指定 region,則會使用 northamerica-northeast1

YAML

  main:
      params: [input]
      steps:
      - init:
          assign:
            - region: ${default(map.get(input, "region"), "northamerica-northeast1")}
  

JSON

  {
    "main": {
      "params": [
        "input"
      ],
      "steps": [
        {
          "init": {
            "assign": [
              {
                "region": "${default(map.get(input, \"region\"), \"northamerica-northeast1\")}"
              }
            ]
          }
        }
      ]
    }
  }

後續步驟