如何在Jenkinsfile groovy映射中定义和获取/放置值

问题描述 投票:1回答:1

我在下面有这个Jenkins文件。我想获得地图的关键,但我得到"java.lang.NoSuchMethodError: No such DSL method 'get' found among steps"。有人可以帮我解决这个问题吗?

def country_capital = {
    [Australia : [best: 'xx1', good: 'xx2', bad: 'xx3'],
    America : [best: 'yy1', good: 'yy2', bad: 'yy3']]
}

pipeline {
    agent any    
    stages {
        stage('Test Map') {
            steps {
                script {
                    echo country_capital.get('Australia')['best']
                }
            }
        }
    }
}
jenkins groovy jenkins-pipeline
1个回答
2
投票

您可以使用这种方式获得价值

def country_capital = [
    Australia: [
        best: 'xx1',
        good: 'xx2',
        bad: 'xx3'
    ],
    America: [
        best: 'yy1',
        good: 'yy2',
        bad: 'yy3'
    ]
]

pipeline {
    agent any    
    stages {
        stage('Test Map') {
            steps {
                script {
                    echo country_capital['Australia'].best
                }
            }
        }
    }
}
// Output
xx1
© www.soinside.com 2019 - 2024. All rights reserved.