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

时间:2019-03-08 18:19:59

标签: jenkins groovy jenkins-pipeline

我在下面有这个Jenkinsfile。我正在尝试获取地图的钥匙,但我正在获取"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']
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

对于上述示例,也可以做到

country_capital.each { capital_key, capital_value ->
try {
  echo "Testing ${capital_value.best}..."
}
catch(ex){
  echo "Test failed: ${capital_value.bad}"                    }
}