Answers: <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>
  
Answers: CC BY-SA 4.0
Answers: 2021-07-12T17:45:31.057Z
Answers: the ".env" did the trick. Much appreciated.
Answers: the_piper
Answers: CC BY-SA 4.0
Answers: 2021-07-11T14:13:23.243Z
Answers: 2021-07-11T14:13:23.243Z
Answers: minhluantran017
Body: <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>
  
Tags: <jenkins><git><jenkinsfile>
Title: Using a conditional environment{ } directive in Jenkinsfiles
UserDisplayName: the_piper
ThreadText: # 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:
  
  ```java
  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.  
  
  ---
  
  