6

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.

Marty
  • 588
  • 1
  • 7
  • 15
  • 1
    take a look at [EnvInject](https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin) plugin, haven't tried myself, but the description matches your needs if i understood correctly – saljuama Jan 08 '16 at 01: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. – Marty Jan 08 '16 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. – Marty Jan 08 '16 at 18:06

4 Answers4

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

Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
6

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

ftzeng12
  • 271
  • 4
  • 4
5

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!

Marty
  • 588
  • 1
  • 7
  • 15
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
.....
prvn
  • 684
  • 6
  • 21
  • 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. – Marty Jan 08 '16 at 18:05