Search code examples
amazon-web-servicesjenkinsgroovyproperties

is possiable write variable '${properties.${params.deployed_region)_env_region}' in Jenkins groovy


Hello am getting loading variable from propeties file in jenkins pipeline am defing varibles in properties file

Mumbai_env_region: ap-south-1
N.virginia_env_region: us-east-1

here Mumbai is choice parameters


pipeline {
    agent any
    parameters {
        choice(
            name: 'deployed_region',
            choices: ['Mumbai', 'N_virginia', 'Singapore'],
            description: 'Note: user can opt for deployment environment'
        )
    }
    stages {
       stage ('Prepare properties ') {
          steps {
            script {
              properties = readProperties file: "jenkinfiles/aws-properties/${params.deployed_region}.properties"

              }
            }
          }
        stage  ('echo test') {
          steps {
              sh """
               ls
               cd jenkinfiles
               echo '${BUCKET_NAME}'
               echo '${properties.${params.deployed_region}env_az}'
               echo '${properties.env_timezone}'
               echo '${properties.INPUT_env_region}'
               echo '${properties.N_virginia_env_region}'
              """
            }
        }

    }
}      

i want be execute sh script like this

echo '${properties.${params.deployed_region)_env_region}'

but am geeting _env_region Such No propery error

but am geeting _env_region Such No propery error


Solution

  • Without using the dot notation try the following.

    def propValue = properties["${params.deployed_region}_env_region"]
    

    Updated Full Pipeline

    pipeline {
        agent any
        parameters {
            choice(
                name: 'deployed_region',
                choices: ['Mumbai', 'N_virginia', 'Singapore'],
                description: 'Note: user can opt for deployment environment'
            )
        }
        stages {
           stage ('Prepare properties ') {
              steps {
                script {
                  properties = readProperties file: "jenkinfiles/aws-properties/${params.deployed_region}.properties"
    
                  }
                }
              }
            stage  ('echo test') {
              steps {
                script {
                    def propValue = properties["${params.deployed_region}_env_region"]
                      sh """
                          echo '${propValue}'
                      """
                    }
                }
            }
        }
    }