简体   繁体   English

从其他jenkins文件访问地图并获取密钥

[英]Accessing a map from a different jenkins file and getting key

I have a constants.groovy like below 我有一个constants.groovy如下

import groovy.transform.Field

@Field
def emailDistributionList = ['catalogSuccess':'ccc@ff.com, fff.ddd@gmail.com', 'catalogFailure':'fffee@ofr.com']

return this;

Now I have a Jenkins pipeline main script as below 现在我有一个Jenkins管道主脚本,如下所示

node ('node1') {
    stage("Read Constants") {
        script {
           def constants = evaluate readTrusted('jenkins_pipeline/constants.groovy')
           def catalogDistributionList = "${constants.emailDistributionList}"
           echo "${catalogDistributionList}"
           def successList = "${catalogDistributionList.catalogSuccess}"
           echo "${successList}"
        }
    }
}

Now first echo prints the Field from constants file sucessfully. 现在,首先echo成功地从常量文件中打印Field But when I try read a key from that and print it using the second echo it throws error 但是,当我尝试从中读取密钥并使用第二个echo其打印时,它将引发错误

groovy.lang.MissingPropertyException: No such property: catalogSuccess for class: org.codehaus.groovy.runtime.GStringImpl

I think it's reading the Field from constants.groovy as a String and not a Map ? 我认为它是从constants.groovy读取Field作为String而不是Map

I figured it out. 我想到了。

We cannot read the map to a variable and then get the key . 我们无法将map读取到变量然后获取key Instead everything has to be done in one shot. 相反,所有事情都必须一次性完成。

It has to be done this way 必须以这种方式完成

def catalogDistributionList = "${constants.emailDistributionList.catalogSuccess}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM