Search code examples
jenkinsgroovyjenkins-pipeline

How to define and get/put the values in Jenkinsfile groovy map


I have this Jenkinsfile below. I am trying to get the key of a map but I am getting "java.lang.NoSuchMethodError: No such DSL method 'get' found among steps". Can someone help me to resolve this?

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']
                }
            }
        }
    }
}

Solution

  • You can get the value using this way

    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