20169

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

Question:

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>

Check <strong>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>

<strong>Build Environment</strong>

[X] Inject environment variables to the build process

Properties File Path C:\pathtofile\mapping.properties

Properties Content THEME_NAME=${${PROJECT_NAME}}

</blockquote>

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 .....

Recommend

  • Customize Vaadin Navigator
  • org.hibernate.AnnotationException: Unable to create unique key constraint
  • Perl , html data and characters encoded in utf-8
  • ThreadPool behaviour: not growing from minimum size
  • How to clear specified format data from clipboard?
  • Read a file in “chunks” using PHP
  • Time taken for Hadoop job to execute
  • Catch 500 internal server error with System.WebClient
  • Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?
  • Most efficient way to move table rows from one table to another
  • How do I get the list of bad records that didn't load in Bigquery?
  • Scala multiline string placeholder
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Zurb Foundation _global.scss meta styles for js?
  • How to define and use opencv mat of user type
  • Could not find rake using whenever rails
  • How to delay loading a property with linq to sql external mapping?
  • Q promise. Difference between .when and .then
  • Master page gives error
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Nant, Vault & Windows Integrated Authentication
  • Bug in WPF DataGrid
  • Cassandra Data Model
  • Join two tables and save into third-sql
  • Deserializing XML into class C#
  • Redux, normalised entities and lodash merge
  • what is the difference between the asp.net mvc application and asp.net web application
  • How to make Safari send if-modified-since header?
  • jQuery tmpl and DataLink beta
  • How can I estimate amount of memory left with calling System.gc()?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Circular dependency while pushing http interceptor
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • How to include full .NET prerequisite for Wix Burn installer
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • costura.fody for a dll that references another dll
  • UserPrincipal.Current returns apppool on IIS
  • Python/Django TangoWithDjango Models and Databases
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize