'What is the proper way to use readYaml in Jenkins declarative pipeline

I saw examples of how to read a YAML file from a scripted Jenkins. I am looking for an example of how to properly read YAML (using readYaml?) in Jenkins declarative pipeline



Solution 1:[1]

The documentation: https://www.jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readyaml-read-yaml-from-files-in-the-workspace-or-text

In a single line, you can use readYaml like this:

def yaml = readYaml text: yourYamlContent

and you should have a yaml object you can extract data from, such as:

env.SOURCE_BRANCH = yaml.source.branch.name

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 M B