使用 readYaml 从声明性 Jenkinsfile 中的文件返回的对象

编程入门 行业动态 更新时间:2024-10-25 03:18:39
本文介绍了使用 readYaml 从声明性 Jenkinsfile 中的文件返回的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过 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 中的文件返回的对象

本文发布于:2023-05-31 10:07:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389576.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   声明   文件   readYaml   Jenkinsfile

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!