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

OpenRewrite:如何在YAML中添加key:value对及指定目标文件?

问题解决指南

1. 为什么AppendToSequence不生效?

你要修改的parameters是YAML映射(键值对集合),而非序列(数组,以-开头的列表结构)。AppendToSequence仅用于向数组中添加元素,因此用错了Recipe。正确做法是使用org.openrewrite.yaml.AddToMap:

修正后的基础配置

recipeList:
  - org.openrewrite.yaml.AddToMap:
      mapPath: $.stages[*].parameters
      key: newKey
      value: newValue
  • mapPath:定位到所有stages数组元素下的parameters映射对象
  • key/value:分别指定要添加的键名和对应值

如果需要精准匹配某一个stages项(比如匹配特定template值),可以结合条件过滤:

recipeList:
  - org.openrewrite.yaml.YamlCondition:
      condition: $.stages[?(@.template == 'myproject/scops-java-service-v1.yaml@codeway')]
    recipeList:
      - org.openrewrite.yaml.AddToMap:
          mapPath: $.stages[?(@.template == 'myproject/scops-java-service-v1.yaml@codeway')].parameters
          key: newKey
          value: newValue

2. 如何指定操作特定YAML文件?

有两种简单方式实现:

  • 方式一:给Recipe添加文件匹配规则
    直接在修改类Recipe中加入filePattern参数,仅匹配目标文件:

    recipeList:
      - org.openrewrite.yaml.AddToMap:
          mapPath: $.stages[*].parameters
          key: newKey
          value: newValue
          filePattern: "**/your-target-file.yaml"
    

    filePattern支持通配符,比如**/*.ci.yaml可匹配所有后缀为.ci.yaml的文件。

  • 方式二:先筛选文件再执行修改
    先用FindSourceFiles定位目标文件,再应用修改操作:

    recipeList:
      - org.openrewrite.file.FindSourceFiles:
          filePattern: "**/your-target-file.yaml"
      - org.openrewrite.yaml.AddToMap:
          mapPath: $.stages[*].parameters
          key: newKey
          value: newValue
    

内容的提问来源于stack exchange,提问作者manish kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 15:12:42