1

My Jenkinsfiles use the environment{} directive. I've been trying to set a condition where I invoke different variables depending on the GIT branch being builded.

I've tried something like e. g. :

switch(branch_name) {
   case 'dev' :
    Var1 = x;
   case 'master' :
    Var1 = y;
}

That would allow me skip using different Jenkinsfiles for each branch on a repo. But groovy syntax seems not to work inside this environment{} directive.

Is there a way tackling it? Or would you suggest another approach for handling different global variables for each branch?

1 Answer 1

5

I often have an extra Prepare stage to calculate these logics:

stage('Prepare') {
   steps {
      script {
         switch(branch_name) {
            case 'dev' :
               env.VAR_1 = x;
            case 'master' :
               env.VAR_1 = y;
         }
      }
   }
}

env. will set the global environment for you.

1
  • the ".env" did the trick. Much appreciated.
    – the_piper
    Commented Jul 12, 2021 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.