You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Cloud Workflows YAML中正确拼接含变量的字符串?

在Google Cloud Workflows中正确拼接包含变量的字符串

以下是几种可行的正确实现方式,以及你之前尝试失败的原因分析:

方法1:使用双引号包裹的字符串插值

这是最简洁的方式,注意要使用双引号而非单引号——YAML单引号会将内容视为字面量,不会解析其中的${}表达式:

url: "${API_URL}/verifications/${verification.id}"

方法2:使用string.concat函数

string.concat接受多个独立的字符串参数(而非数组),且在表达式内部直接引用变量即可,无需额外嵌套${}:

url: ${string.concat(API_URL, '/verifications/', verification.id)}

方法3:预先赋值变量后拼接

如果需要拆分步骤,可先将各部分赋值给变量,再通过插值或函数完成拼接:

- assign_vars:
    assign:
      - base_url: ${API_URL}
      - verification_path: "/verifications/"
      - target_id: ${verification.id}
      - full_url: "${base_url}${verification_path}${target_id}"
- make_request:
    url: ${full_url}

或者用string.concat实现:

- assign_vars:
    assign:
      - base_url: ${API_URL}
      - verification_path: "/verifications/"
      - target_id: ${verification.id}
      - full_url: ${string.concat(base_url, verification_path, target_id)}
- make_request:
    url: ${full_url}

你之前尝试失败的原因

  1. 单引号插值失效:单引号会让YAML将整个字符串当作纯文本,${API_URL}和${verification.id}不会被解析为变量值。
  2. +运算符不支持:Google Cloud Workflows没有提供+拼接字符串的语法,必须使用内置的string.concat函数或插值语法。
  3. string.concat参数格式错误:你传入了数组作为参数,但该函数需要的是多个独立的字符串参数,而非数组;同时在函数内部无需再用${}包裹变量。
  4. 预先赋值后的拼接逻辑错误:同样是string.concat的参数格式问题,且部分场景未用正确的表达式包裹拼接逻辑。

内容的提问来源于stack exchange,提问作者Mysterious Otter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 00:32:32