Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you add environment variables in a Jenkins Multibranch Pipeline

I am working on a Django project, I have integrated it withJenkins Multibranch Pipeline. I cannot inject environment variables by the Multibrach Pipeline option even after installing the Environment Injector plugin.

I have evironment variables like DB_PASSWORD that must be included in the envvars.

Any insight will be highly appreciated.

like image 771
Erick Mwazonga Avatar asked Dec 20 '25 03:12

Erick Mwazonga


1 Answers

Since you require secrets, the best practice way is to use the withCredentials plugin which can load credentials stored in Jenkins credential store and expose them as ENV variables to code executed within its block/closure. Using withCredentials does not expose them in Jenkins logs

withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'DB_Creds', 
usernameVariable: 'DB_USERNAME', 
passwordVariable: 'DB_PASSWORD']]) {// do stuff }

For non sensitive Env vars use withEnv

withEnv(["AWS_REGION=${params.AWS_REGION}"]) {// do stuff }

If for whatever reason you want Env vars set across your entire pipeline (not entirely recommended but sometimes necessary):

env.MY_VAR='var'
echo("My Env var = ${env.MY_VAR}")
like image 89
metalisticpain Avatar answered Dec 21 '25 19:12

metalisticpain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!