简体   繁体   English

如何在 Groovy/Jenkins 中循环遍历 YAML 列表文件

[英]How to loop through a YAML list file in Groovy/Jenkins

I have a YAML file template which is set out out like so:我有一个 YAML 文件模板,如下所示:

cars:
    - brand: bmw 
      fuel: petrol
      transmission: manual
    - brand: mercedes
      fuel: diesel
      transmission: auto
    - brand: audi
      fuel: electric
      transmission: semi-auto

I am trying to write a function for a Jenkins pipeline that reads through the YAML file, picks up the brand, fuel and transmission value of each array set, and execute commands based on these variables for each set, I assume it would have to be some kind of loop.我正在尝试为 Jenkins 管道编写 function 管道,该管道读取 YAML 文件,获取每个数组集的品牌、燃料和传输值,并假设它基于这些变量设置某种循环。

I have tried using readYaml however it doesn't seem to pick up the first “brand” in this case and get errors, not sure if this YAML layout is ideal for my use case我尝试过使用 readYaml,但是在这种情况下它似乎没有选择第一个“品牌”并出现错误,不确定这个 YAML 布局是否适合我的用例

I have tried:我努力了:

def cars = readYaml file = cars.yaml 
def brand = cars[brand]

Expecting echo brand to return a list of brand names, which I can then use to look up the fuel and transmission values of that brand.期望 echo brand 返回一个品牌名称列表,然后我可以使用它来查找该品牌的燃料和传输值。 However I get this error但是我收到此错误

groovy.lang.MissingPropertyException: No such property: mkp for class: WorkflowScript

Any ideas?有任何想法吗?

The proper syntax is正确的语法是

def data = readYaml file: "cars.yaml"
def brand = data.cars[0].brand

You can find the definition (and examples) in the pipeline utility steps documentation .您可以在管道实用程序步骤文档中找到定义(和示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM