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.Answer1:
I will provide an alternative solution which I used. I hope it can also be useful for others.
I used <a href="https://wiki.jenkins.io/display/JENKINS/EnvInject+Plugin" rel="nofollow">Environment Injector Plugin</a>. Go to the plugin manager and install it. <a href="https://i.stack.imgur.com/OZ6RM.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/OZ6RM.png" data-original="https://i.stack.imgur.com/OZ6RM.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
Inject environment variables to the build process</strong> property of the <strong>Build Environment</strong>. Define the following in Groovy Script:
<a href="https://i.stack.imgur.com/ZF6nu.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/ZF6nu.png" data-original="https://i.stack.imgur.com/ZF6nu.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>Answer2:
Aha, I found a simple solution! Using the EnvInject plugin, in the job config:<blockquote>
[X] Inject environment variables to the build process
Properties File Path
Works like a charm!Answer3:
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 .....