从jenkins管道groovy脚本加载配置文件的推荐方法

Nat*_*han 5 groovy jenkins-pipeline

我想从jenkins管道脚本加载配置值(类似于json,yaml,xml或ini).当我尝试使用时,org.yaml.snakeyaml.Yaml我得到了

脚本不允许使用新的org.yaml.snakeyaml.Yaml

我知道我可以解锁org.yaml.snakeyaml.Yam,但是消息告诉我这似乎不是加载配置文件的标准方法.

有没有办法加载已经解锁的配置文件?

f-s*_*ety 8

如果有人在jenkinsfile中寻找yaml解析器,我推荐以下内容

 def yamlData = readYaml file: 'cae.yaml'
Run Code Online (Sandbox Code Playgroud)

参考:https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#code-readyaml-code-read-yaml-from-files-in-the-workspace-or-text


Ran*_*niz 3

尝试使用JsonSlurper

def config = new JsonSlurper().parse(new File("config.json"))
Run Code Online (Sandbox Code Playgroud)

  • 需要这样写:groovy,json.JsonSlurper(或者导入模块)。谢谢! (2认同)