GCP工作流:追加到数组



使用GCP/Google工作流,我想获得一个数组,并对每个调用http端点的数组应用转换。为此,我正在寻找一种方法来应用转换,然后将结果重新组合到另一个数组中。实现这一点的一种方法是experial.executions.map,但这是一个实验功能,可能会发生变化,所以我有点犹豫是否使用它。另一种方法是从一个空数组开始,并在应用转换时附加到该数组。有办法做到这一点吗?连接字符串似乎可以工作,但似乎没有内置的附加到数组的away。我尝试了这样的方法,它确实成功地执行了,但没有产生预期的结果:

main:
steps:
- assignVariables:
assign:
- sourceArray: ["one", "two", "three"]
- hashedArray: []
- hashedString: ""
- i: 0
- checkCondition:
switch:
- condition: ${i < len(sourceArray)}
next: iterate
next: returnResult
- iterate:
assign:
- hashedArray[i]: ${"#" + sourceArray[i]}
- hashedString: ${hashedString + "#" + sourceArray[i] + " "}
- i: ${i + 1}
next: checkCondition
- returnResult:
return:
- hashedString: ${hashedString}
- hashedArray: ${hashedArray}

实际结果:

[
{
"hashedString": "#one #two #three "
},
{
"hashedArray": []
}
]

预期结果:

[
{
"hashedString": "#one #two #three "
},
{
"hashedArray": ["#one", "#two", "#three"]
}
]

现在支持使用list.concat:将项目添加到列表中

assign:
- listVar: ${list.concat(listVar, "new item")}

对于那些想知道如何使用实验性实验.executions.map功能来解决这个问题的人,你可以这样做:

首先创建一个可调用的工作流来转换数组。我将此工作流命名为hash-item。这个名称很重要,因为我们稍后将其称为workflow_id

main:
params: [item]
steps:
- transform:
assign:
- hashed: ${"#" + item}
- returnResult:
return: ${hashed}

然后创建主工作流,使用experimental.executions.map:调用hash-item

main:
steps:
- assignVariables:
assign:
- sourceArray: ["one", "two", "three"]
- transform:
call: experimental.executions.map
args:
workflow_id: hash-item
arguments: ${sourceArray}
result: result
- returnResult:
return: ${result}

用于执行主工作流的服务帐户需要workflows.executions.create权限(通常通过roles/workflows.invoker角色(。要分配此项,您可以使用Cloud SDK CLI:执行此操作

gcloud projects add-iam-policy-binding <project-id> --member="serviceAccount:<service-account-email>" --role="roles/workflows.invoker"

执行主工作流程将输出所需结果:

[
"#one",
"#two",
"#three"
]

注:experial.executions.map是实验性的,因此可能会更改。