in

Get the variable value if variable name is stored as string in Groovy Script


I have some pre defined variable in Groovy Script (of Jenkins pipeline) and need to pick any one variable from them dynamically based on job/user input.
The example context of requirement is as provided below.
Here variable env is my input and based on that I should get correct userid.

env = "dev" //Input
def stg_userid = "abc"
def dev_userid = "xyz"
uid_var_name = "${env}_userid"
print "${uid_var_name}" // It is giving "dev_userid

print ‘abc’ if we give ‘stg’ for env
print ‘xyz’ if we give ‘dev for env

Tried searching online for dynamic variable name use case in Groovy , but didn’t got anything useful.



Source: https://stackoverflow.com/questions/70542561/get-the-variable-value-if-variable-name-is-stored-as-string-in-groovy-script

Best of Best WordPress Tutorials of 2021 on WPBeginner

Telegram Launched These New Cool Features for Users