代码之家  ›  专栏  ›  技术社区  ›  Ceesiebird

如何使用Jenkins插件管道实用程序步骤中的readYAML方法解析Jenkins管道中的YAML文件

  •  1
  • Ceesiebird  · 技术社区  · 6 年前

    我正在尝试使用Jenkins插件“pipeline Utility Steps”中的readYaml方法解析Jenkins管道中的YAML文件。

    我在论坛上读到readYml方法应该在管道的节点块中调用。

    在修改这个readYml方法之前,我的管道工作得完美无缺。

    但是在将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)
    

    我不会让你厌烦完整的管道代码,因为问题实际上是在编辑我的节点块之后。

    我调用插件readYml方法的方式如下。

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

    我该如何使它正常工作并消除错误?

    提前谢谢。

    1 回复  |  直到 6 年前
        1
  •  5
  •   Ceesiebird    6 年前

    我已经知道问题出在哪里了。

    正如上面提到的,调用插件在去极化管道中的节点块中不起作用。

    然而,仅仅把它放在一个台阶上也不起作用。

    最后的修复方法是将它放在步骤块中的脚本块中。

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

    注意,echo只是为了验证*.yml文件是否被正确解析。

        2
  •  0
  •   cherusk    5 年前

    最方便的是我在尝试基于javaISH字符串进行模板化之后使用基本shell脚本。

    $ cat logic/pipelines/obfuscate.sh 
    #!/bin/bash
    
    echo "${REQUEST}" | json2yaml > "${PARAM_FILE}"
    

    然后再从剧本中读到这篇文章:

    ---
    #!/bin/bash
    
    # improvised dynamic extraction
    
    SOME=`yq -r .scope.some $PARAM_FILE`
    echo -n "${SOME}"