如何使用 Jenkins 插件管道实用程序步骤中的 readYAML 方法解析 Jenkins 管道中的 YAML 文件
How to parse YAML files in Jenkins pipeline using readYAML method from the Jenkins plugin Pipeline Utility Steps
我正在尝试使用 Jenkins 插件中的 readYaml 方法解析 Jenkins 管道中的 YAML 文件 "Pipeline Utility Steps"。
我在论坛上看到应该在管道的节点块中调用 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"
}
}
如何让它正常工作并消除错误?
提前致谢。
我知道问题出在哪里了。
正如上面提到的好心人,在声明性管道的节点块中调用插件将不起作用。
但是,简单地把它放在一个步骤块中也是行不通的。
最后的修复是将它放在步骤块中的脚本块中。
stage('Read YAML file') {
steps {
script{ datas = readYaml (file: 'manifest.yml') }
echo datas.ear_file.deploy.toString()
}
}
}
请注意,回显只是为了我自己验证 *.yml 文件是否已正确解析。
对我来说最方便的是在尝试基于 javaISH 字符串创建模板后使用基础 shell 脚本。
$ cat logic/pipelines/obfuscate.sh
#!/bin/bash
echo "${REQUEST}" | json2yaml > "${PARAM_FILE}"
然后在脚本中进一步阅读 PARAM_FILE:
---
#!/bin/bash
# improvised dynamic extraction
SOME=`yq -r .scope.some $PARAM_FILE`
echo -n "${SOME}"
我正在尝试使用 Jenkins 插件中的 readYaml 方法解析 Jenkins 管道中的 YAML 文件 "Pipeline Utility Steps"。
我在论坛上看到应该在管道的节点块中调用 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"
}
}
如何让它正常工作并消除错误?
提前致谢。
我知道问题出在哪里了。
正如上面提到的好心人,在声明性管道的节点块中调用插件将不起作用。
但是,简单地把它放在一个步骤块中也是行不通的。
最后的修复是将它放在步骤块中的脚本块中。
stage('Read YAML file') {
steps {
script{ datas = readYaml (file: 'manifest.yml') }
echo datas.ear_file.deploy.toString()
}
}
}
请注意,回显只是为了我自己验证 *.yml 文件是否已正确解析。
对我来说最方便的是在尝试基于 javaISH 字符串创建模板后使用基础 shell 脚本。
$ cat logic/pipelines/obfuscate.sh
#!/bin/bash
echo "${REQUEST}" | json2yaml > "${PARAM_FILE}"
然后在脚本中进一步阅读 PARAM_FILE:
---
#!/bin/bash
# improvised dynamic extraction
SOME=`yq -r .scope.some $PARAM_FILE`
echo -n "${SOME}"