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

如何用YQ合并YAML文件或为数组添加元素?

使用yq合并YAML文件中的数组条目

可以通过yq轻松实现需求,以下是针对mikefarah/yq v4+版本(当前主流版本)的可行操作方案:

方法一:直接合并两个文件的installations数组

执行以下命令,将file-b.yaml中的installations条目追加到file-a.yaml的对应数组中,并直接修改file-a.yaml:

yq eval '.unclassified.sonarGlobalConfiguration.installations += load("file-b.yaml").unclassified.sonarGlobalConfiguration.installations' file-a.yaml -i

方法二:手动添加单个条目

如果不需要读取file-b,而是直接指定条目内容添加,可执行:

yq eval '.unclassified.sonarGlobalConfiguration.installations += [{"credentialsId": "ID-3", "name": "sonarqube-3", "serverUrl": "https://example.org", "triggers": {"skipScmCause": false, "skipUpstreamCause": false}}]' file-a.yaml -i

执行完成后,file-a.yaml的unclassified.sonarGlobalConfiguration.installations下会包含3个条目,同时保留原有的buildWrapperEnabled配置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 14:35:12