I'm trying to set an environment variable in a Jenkins pipeline job based on a parameter passed into the job; this way I can use the environment variable in every stage of the pipeline that requires it. I tried using a switch statement in the environment block:
parameters { choice(name: 'ENVIRONMENT', choices: 'dev\nst\nprod', description: 'Environment') } environment { script { switch(env.ENVIRONMENT) { case 'dev': BRANCH = master break case 'st': BRANCH = 2020Q1 break case 'prod': BRANCH = 2019Q4 break } } } However this didn't work, the job tried to evaluate all the lines before the equals sign as the KEY name:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 9: "script { switch(env.ENVIRONMENT) { case 'dev': BRANCH" is not a valid identifier and cannot be used for an environment variable. Identifiers must start with a letter or underscore and can contain only letters, numbers or underscores. @ line 9, column 7. script { How do I get this to work?
ENVIRONMENTvalue is in theparamsmap and not theenvmap at that point.switch(params.ENVIRONMENT)