11

I have a project where I need to set an environment variable based on a choice parameter the user chooses. Each project has a theme project dependency. I'd like to have the user choose the project and then load the theme name from a property file. Something like

proj1=theme1 proj2=theme2 proj3=theme3 

If the user chooses proj1 from the PROJECT_NAME choice parameter, I want to automatically set THEME_NAME to be theme1. What would be the best way to go about this?

I don't want to modify the Jenkins job config whenever a new project is added. Instead, I want to have the mapping in a file so I can have it in version control.

3
  • 1
    take a look at EnvInject plugin, haven't tried myself, but the description matches your needs if i understood correctly Commented Jan 8, 2016 at 1:22
  • I have looked at it a little and unless I'm missing something, it seems like I can inject some variables but not necessarily conditionally. So, I can't inject theme1 as THEME_NAME only when they've chosen proj1. Instead, I inject a set of predefined variables. I'd love to be proven wrong though. Commented Jan 8, 2016 at 17:45
  • An additional option I've considered and might have to fall back on is to use a naming convention in the mapping file. So, for example I would have proj1_THEME=theme1 That way, I could use the EnvInject plugin to load the file and then reference the theme as ${PROJECT_NAME}_THEME. I think that could work. Commented Jan 8, 2016 at 18:06

4 Answers 4

10

Sometimes its nice to be able to set parameters through Jenkins and in turn trickle those as Jenkins env. vars. Things like passwords can be protected in parameters but cannot through the Environment Injector Plugin.

An example to do this:

environment { AWS_ECR_ACCOUNT_ID="${params.AWS_ECR_ACCOUNT_ID}" AWS_ACCESS_KEY_ID="${params.AWS_ACCESS_KEY_ID}" AWS_SECRET_ACCESS_KEY="${params.AWS_SECRET_ACCESS_KEY}" AWS_SESSION_TOKEN="${params.AWS_SESSION_TOKEN}" } parameters { string(name: 'AWS_ECR_ACCOUNT_ID', defaultValue: '', description: 'AWS ECR account ID') password(name: 'AWS_ACCESS_KEY_ID', defaultValue: '', description: 'The AWS access key ID') password(name: 'AWS_SECRET_ACCESS_KEY', defaultValue: '', description: 'The AWS secret access key') password(name: 'AWS_SESSION_TOKEN', defaultValue: '', description: 'The AWS session token') } 

And just set the params in the UI:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

7

I will provide an alternative solution which I used. I hope it can also be useful for others.

I used Environment Injector Plugin. Go to the plugin manager and install it. enter image description here

Check Inject environment variables to the build process property of the Build Environment. Define the following in Groovy Script:

enter image description here

1 Comment

OMG. ... Where did you find the documentation on this??? A true life saver!
6

Aha, I found a simple solution! Using the EnvInject plugin, in the job config:

Build Environment

[X] Inject environment variables to the build process

Properties File Path C:\pathtofile\mapping.properties

Properties Content THEME_NAME=${${PROJECT_NAME}}

Works like a charm!

Comments

1

I hope you are talking about parameterized build in jenkins. So if you give choice parameter the name PROJECT_NAME and the choices to be :-

proj1 proj2 proj3 

then, Jenkins will automatically assign one of these value(i.e proj1, proj2, proj3) to variable PROJECT_NAME as per the choice triggered to start the build. You can infact use $PROJECT_NAME as a variable anywhere in the job configuration page.

But you require the values (theme1, theme2, theme3)..such mapping to my knowledge is not provided by jenkins.

However you could use a build shell to perform your mapping:-

if [ $PROJECT_NAME = "proj1" ] then <your logic goes here for implementing theme1> fi ..... 

1 Comment

Thanks, but I was hoping to stay away from this sort of solution. When a new project was added, someone would need to add to the shell script in Jenkins which is much more intimidating for a non-coder than adding projX=themeY to a text file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.