Marty
Marty

Reputation: 678

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

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.

Upvotes: 11

Views: 35203

Answers (4)

ftzeng12
ftzeng12

Reputation: 451

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

Upvotes: 9

Ayaz Alifov
Ayaz Alifov

Reputation: 8588

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

Upvotes: 8

Marty
Marty

Reputation: 678

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!

Upvotes: 6

prvn
prvn

Reputation: 694

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

Upvotes: 1

Related Questions