简体   繁体   English

如何使用Jenkins插件Pipeline Utility步骤中的readYAML方法在Jenkins管道中解析YAML文件

[英]How to parse YAML files in Jenkins pipeline using readYAML method from the Jenkins plugin Pipeline Utility Steps

I am trying to parse YAML files in Jenkins pipeline using the readYaml method from the Jenkins plugin "Pipeline Utility Steps". 我正在尝试使用Jenkins插件“ Pipeline Utility Steps”中的readYaml方法来解析Jenkins管道中的YAML文件。

I read on the forums that the readYml method should be called in the node block of the pipeline. 我在论坛上读到,应该在管道的节点块中调用readYml方法。

Before tinkering with this readYml method my pipeline worked flawlessly. 在修改这个readYml方法之前,我的管道运行完美。

But after adding readYml to my pipeline's node block I get the following error. 但是在将readYml添加到管道的节点块之后,出现以下错误。


org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: Expected to find someKey "someValue" @ line 5, column 14.
           node {
                ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
    at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:131)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:125)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:560)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:521)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:290)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:421)

I won't bore you with the complete pipeline code since the issue is really after editing my node block. 我不会对完整的管道代码感到厌烦,因为问题确实出在编辑我的节点块之后。

The way I call the plugins readYml method is as follows. 我将插件称为readYml方法的方式如下。

pipeline {
    agent {
        node {
            label 'lsrv9557.linux.rabobank.nl'
            customWorkspace '/appl/jenkins/workdir'
            datas = readYaml file: "manifest.yml"
        }
   }

How do I get this to work properly and get rid of the error? 如何使它正常工作并摆脱错误?

Thanks in advance. 提前致谢。

I have figured out what the issue was. 我已经弄清楚了问题所在。

As the kind fellows above mentioned as well,calling the plugin won't work in a node block in a declaritive pipeline. 就像上面提到的那样,调用插件在声明性管道的节点块中不起作用。

However, simply putting it in a step block also wasn't working. 但是,仅将其放在步骤块中也不起作用。

The fix in the end was putting it in a script block within the step block. 最后的解决方法是将其放在步骤块中的脚本块中。

        stage('Read YAML file') {
        steps {
            script{ datas = readYaml (file: 'manifest.yml') }
            echo datas.ear_file.deploy.toString()

        }
    }
}

note that the echo is just to verify for myself if the *.yml file was properly parsed. 请注意,回声只是为自己验证* .yml文件是否已正确解析。

Most convenient was for mine to work with base shell scripts after trying to template based on the javaISH strings. 最方便的是,在尝试基于javaISH字符串进行模板化后,我可以使用基本的shell脚本。

$ cat logic/pipelines/obfuscate.sh 
#!/bin/bash

echo "${REQUEST}" | json2yaml > "${PARAM_FILE}"

Then reading from this PARAM_FILE in scripts further on: 然后在脚本中进一步读取此PARAM_FILE:

---
#!/bin/bash

# improvised dynamic extraction

SOME=`yq -r .scope.some $PARAM_FILE`
echo -n "${SOME}"

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

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