Prasa2166
Prasa2166

Reputation: 479

Groovy - Access variable which has value of another variable

I've been trying to use a common property file in Jenkins which will have details of multiple servers. Based on the selection in Jenkins(By selecting "Build with parameters"), corresponding server details need to be obtained from the property file. For this, I need to access a value of variable created by value of another variable. Is this supported in groovy?

I have defined the properties in a property file and the sample values are like

PROD_SERVERNAME = sampleprodserver;

DEV_SERVERNAME = sampledevserver;

def environment = "PROD"; // this will be given as a parameter   
def servername = environment + "_SERVERNAME";
def Propertyfile = readProperties file:propertyfile;
def server = Propertyfile.servername

I expect the value of server should be sampleprodserver but the value i'm getting is null.

Any help would be highly appreciated.

Upvotes: 0

Views: 818

Answers (1)

daggett
daggett

Reputation: 28564

the code

Propertyfile.servername

tries to get property with name servername from Propertyfile variable

and to get the property value by variable value use one of:

Propertyfile.getProperty(servername)
//or
Propertyfile[servername]

Upvotes: 2

Related Questions