我想通过 readYaml 实用程序读取声明性 Jenkinsfile 中的 yaml 文件的内容.我的理解是 readYaml 应该返回一个 Map,但是,我看到返回的对象类型是一个字符串.这违背了将数据放在 yaml 文件中的初衷.
I want to read the contents of a yaml file in a declarative Jenkinsfile via the readYaml utility. My understanding is that readYaml should return a Map, however, the object type I am seeing returned is a String. This defeats the purpose of putting the data in a yaml file in the first place.
具体来说,我想从 helm values.yaml 文件中获取一些值,以便在 Jenkinsfile 的全局环境部分中设置 env 值,以便所有后续阶段都能够使用.
Specifically, I want to get some values from a helm values.yaml file to set env values in the global environment section of the Jenkinsfile for all subsequent stages to be able to use.
println valuesYaml.getClass() 返回 java.lang.String 我认为这是不正确的,因为这个对象来自嵌套的 yaml 文件,所以我认为返回的对象应该是一个地图.
The println valuesYaml.getClass() returns java.lang.String which I think is not correct because this object comes from a nested yaml file so I think the returned object should be a map.
jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readyaml-read-yaml-from-files-in-the-workspace-or-text
当以下 echo 语句运行 echo valuesYaml.appName.toString() 时,它会出错并显示以下错误:
When the following echo statement runs echo valuesYaml.appName.toString() it errors out with the following error:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String appName
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: No such field found: field java.lang.String appName
这是我正在尝试阅读的 values.yaml 的片段:
This is a snippet of the values.yaml I'm trying to read:
replicaCount: 1 appName: test def loadValuesYaml(){ def valuesYaml = readYaml (file: './chart/values.yaml') return valuesYaml; } pipeline { agent { label "jenkins-maven" } environment { valuesYaml = loadValuesYaml() } stages { stage('CICD Initialize') { steps { script{ echo valuesYaml println valuesYaml.getClass() } echo valuesYaml.appName.toString() } } } 推荐答案您正在 environment 块内设置 valuesYaml 变量,这使它成为 string.将您的变量声明移至 script 块,该变量将在后续阶段可访问.
You are setting your valuesYaml variable inside environment block, which makes it string. Move your variable declaration to script block, the variable will be accessible in subsequent stages.
def loadValuesYaml(){ def valuesYaml = readYaml (file: './chart/values.yaml') return valuesYaml; } pipeline { agent { label "jenkins-maven" } stages { stage('CICD Initialize') { steps { script{ valuesYaml = loadValuesYaml() println valuesYaml.getClass() } } } stage('Deploy') { steps { echo valuesYaml.appName } } } }或者,如果您想在 environment 块中声明它们,您可以重写 loadValuesYaml 函数以返回特定字符串,但是,这将调用 readYaml 多次.
Alternatively, if you want to declare them inside environment block, you can rewrite your loadValuesYaml function to return a specific string, however, this will call readYaml multiple times.
def loadValuesYaml(x){ def valuesYaml = readYaml (file: './chart/values.yaml') return valuesYaml[x]; } pipeline { agent { label "jenkins-maven" } environment { APP=loadValuesYaml('appName') REPLICACOUNT=loadValuesYaml('replicaCount') } stages { stage('CICD Initialize') { steps { script{ println APP println REPLICACOUNT } } } } }更多推荐
使用 readYaml 从声明性 Jenkinsfile 中的文件返回的对象
发布评论