我有一个公共库,可以打印yml文件中的变量。
def getHelmSvc(Map helmSvc,String mservice){
def domain = helmSvc[mservice].domain
def microservice_name = helmSvc[mservice].microservice_name
def ingress_required = helmSvc[mservice].ingress_required
def var_map = [:]
var_map['microservice_name'] = microservice_name
var_map['ingress_required'] = ingress_required
return var_map
}
我在jenkinsfile中的库上方调用
text1=new File('/jenkins/workspace/helm/helm_svc.yml').text
Yaml yaml = new Yaml()
def obj = yaml.load(text1)
ret=getHelmSvc(obj, mservice)
println ret
它正在控制台上从yml打印变量映射,后来试图使用shell将这些值打印到txt文件中,但是出现序列化问题。
基本上是在控制台上这样打印的:
{microservice_name=some-processor, ingress_required=false}
后来我使用Shell来打印到文本文件,但由于序列化问题而失败。
sh("""
echo "$ret" > "/jenkins/workspace/helm/map.txt"
""")
错误:
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@2b3f4d48
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@2ee0ffec
in field com.cloudbees.groovy.cps.Continuable.e
in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@108a4042
in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
in object org.jenkinsci.plugins.workflow.cps.CpsThread@28b2f39d
in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@1a7da7eb
in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@1a7da7eb
Caused: java.io.NotSerializableException: org.yaml.snakeyaml.Yaml
如果我们在另一个脚本级别使用上面的shell命令可以正常工作,那么只有在加载yaml文件的位置添加相同的脚本级别时,此命令才会失败。
“ mservice = some-service ”,作为管道参数提供。
基本上我们有一个名为/jenkins/workspace/helm/helm_svc.yml的文件,它在地图中定义了一堆变量
processor-create:
microservice_name: processor-create
service_required: true
ingress_required: false
processor-update:
microservice_name: processor-update
service_required: true
ingress_required: false
scheduler_required: false
service-create:
microservice_name: service-create
service_required: true
ingress_required: true
scheduler_required: false
我正在尝试从管道参数(例如:processor-create)提供输入,因为它将从文件上方获取值并将其分配给ret
变量,然后我们需要将该ret变量值打印到文本文件,以便我可以在同一阶段将该文本文件提供给ansible剧本。