Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a Jenkins environment variable based on a job parameter?

Tags:

jenkins

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.

like image 794
Marty Avatar asked Dec 05 '22 19:12

Marty


2 Answers

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

like image 72
Ayaz Alifov Avatar answered Jan 01 '23 04:01

Ayaz Alifov


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!

like image 23
Marty Avatar answered Jan 01 '23 02:01

Marty