【问题标题】:Recommended way of loading config file from jenkins pipeline groovy script从 jenkins 管道 groovy 脚本加载配置文件的推荐方法
【发布时间】:2016-10-25 07:33:06
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: groovy jenkins-pipeline


    【解决方案1】:

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

     def yamlData = readYaml file: 'cae.yaml'
    

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

    【讨论】:

    • 你能在 Jenkinsfile 的上下文中展示这个吗?我期待使用“导入”语句,但这不起作用。 def 语句是放在“管道”指令的内部还是外部?
    【解决方案2】:

    尝试使用JsonSlurper

    def config = new JsonSlurper().parse(new File("config.json"))
    

    【讨论】:

    • 需要这样写:groovy,json.JsonSlurper(或者导入模块)。谢谢!
    • 我收到关于 JsonSlurper 的相同错误:不允许使用方法 groovy.json.JsonSlurper 的脚本
    • @TudorCarean 为时已晚,但要解决此问题,您需要在 Jenkins > Manage Jenkins > In-Process Script Approvals 中允许该方法,然后点击 Approve
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2022-06-22
    • 2018-05-14
    • 2016-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多