<p>I often have an extra <code>Prepare</code> stage to calculate these logics:</p>
<pre class="lang-java prettyprint-override"><code>stage('Prepare') {
   steps {
      script {
         switch(branch_name) {
            case 'dev' :
               env.VAR_1 = x;
            case 'master' :
               env.VAR_1 = y;
         }
      }
   }
}
</code></pre>
<p><code>env.</code> will set the global environment for you.</p>

CC BY-SA 4.0
2021-07-12T17:45:31.057Z
the ".env" did the trick. Much appreciated.
the_piper
CC BY-SA 4.0
2021-07-11T14:13:23.243Z
2021-07-11T14:13:23.243Z
minhluantran017
<p>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.</p>
<p>I've tried something like e. g. :</p>
<pre><code>switch(branch_name) {
   case 'dev' :
    Var1 = x;
   case 'master' :
    Var1 = y;
}
</code></pre>
<p>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.</p>
<p>Is there a way tackling it? Or would you suggest another approach for handling different global variables for each branch?</p>

<jenkins><git><jenkinsfile>
Using a conditional environment{ } directive in Jenkinsfiles
the_piper
# Using a conditional environment{ } directive in Jenkinsfiles

(Asked by: username_0 on 2021-07-10)

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?

**Tags:** jenkins, git, jenkinsfile


---

## 1 answers

### Answer by username_1 on 2021-07-11. Score: 5 | Accepted answer

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.

> **Comments:**
> **username_0**: the ".env" did the trick. Much appreciated.  

---

