Answers: <p>See the <a href="https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables-dynamically" rel="nofollow noreferrer">&quot;using environment variables dynamically&quot; section of the official docs</a>.  I think this will answer your question.  Here is the snippet of example code from those docs:</p>
  <pre><code>pipeline {
      agent any 
      environment {
          // Using returnStdout
          CC = &quot;&quot;&quot;${sh(
                  returnStdout: true,
                  script: 'echo &quot;clang&quot;'
              )}&quot;&quot;&quot; 
          // Using returnStatus
          EXIT_STATUS = &quot;&quot;&quot;${sh(
                  returnStatus: true,
                  script: 'exit 1'
              )}&quot;&quot;&quot;
      }
      stages {
          stage('Example') {
              environment {
                  DEBUG_FLAGS = '-g'
              }
              steps {
                  sh 'printenv'
              }
          }
      }
  }
  </code></pre>
  <p>So your code might end up looking something like this:</p>
  <pre><code>pipeline {
      agent any 
      environment {
          mongo_url = &quot;&quot;&quot;${
              switch(env.BRANCH_NAME) {
                  case 'bugfix/*'
                  case 'feature/*'
                  case 'development'
                  case 'hotfix/*'
                      'YOUR_DEV_MONGO_URL_HERE'
                      break
                  case 'staging'
                      'YOUR_STAGING_MONGO_URL_HERE'
                      break
              }&quot;&quot;&quot;
      }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-11-08T07:13:09.143Z
Answers: I have tried your example code. But it looks like I cannot use the `switch` statement in a declarative pipeline.
Answers: ThatTechGuy
Answers: CC BY-SA 4.0
Answers: 2022-11-07T17:15:41.587Z
Answers: 2022-11-07T17:15:41.587Z
Answers: jayhendren
Body: <p>I'm trying to set up an automated Jenkins pipeline for the <code>Development</code> &amp; <code>Staging</code> branches within our Git repository. I have most of the pipeline working. But I do not know how to override globally set <code>environment {}</code> variables depending on the git branch.</p>
  <p>This is my Jenkinsfile:</p>
  <pre><code>pipeline {
    agent any
  
    environment {
      dev_mongo_url = credentials('DEV_MONGO_URL')
      stg_mongo_url = credentials('STG_MONGO_URL')
    }
  
    stages {
  
      stage('Env Setup') {
        steps {
  
          script
          {
          if (env.BRANCH_NAME == 'bugfix/*' || env.BRANCH_NAME == 'feature/*' || env.BRANCH_NAME =='development' || env.BRANCH_NAME =='hotfix/*') {
              env.environment = 'development' 
              echo 'Build &amp; deployment to development environment'
              }
  
  
          if (env.BRANCH_NAME == 'staging'){
              env.environment = 'staging'
              echo 'Build &amp; deployment to staging environment'
              
          }
    }
  }
  }   
  
      stage('Migration') {
        when {
          anyOf {
            branch 'development';
            branch 'feature/*';          
            branch 'staging'
           }
        }
        steps {
          echo &quot;Installing the project NodeJS dependencies...&quot;
          sh 'npm ci'
          echo &quot;Executing the MongoDB migration script &amp; displaying the status...&quot;
          dir('db-migrations') {
            sh 'npm ci'
            sh('npm run migrate --silent -- --mongodb-url=$mongo_url')
            sh 'npm run migrate:status'
          }
  
        }
      }
  
      
      
  }
      }
  </code></pre>
  <p>Within the above pipeline, I have the <code>Env Setup</code> stage, which assigns the <code>environment</code> type variable (development/staging) depending on the git branch. We have two MongoDB instances - one for development &amp; one for the staging environment. I have defined both of the MongoDB server URL's under the <code>environment {}</code> block. Now, I want to dynamically connect to the dev/staging mongo servers in the <code>Migration</code> stage, depending on the environment type, and apply the migrations. I'm trying to automatically inject <code>$mongo_url</code> value from the <code>dev_mongo_url</code> &amp; <code>stg_mongo_url</code> global variables depending on the environment. I'm stuck, and I have no clue how to achieve it.</p>
  <p>Any help?</p>
  
Tags: <jenkins><jenkins-pipeline><jenkinsfile>
Title: How to override global "environment {}" Jenkins Variables in a stage?
UserDisplayName: ThatTechGuy
ThreadText: # How to override global "environment {}" Jenkins Variables in a stage?
  
  (Asked by: username_0 on 2022-11-05)
  
  I'm trying to set up an automated Jenkins pipeline for the `Development` & `Staging` branches within our Git repository. I have most of the pipeline working. But I do not know how to override globally set `environment {}` variables depending on the git branch.
  
  This is my Jenkinsfile:
  
  ```
  pipeline {
    agent any
  
    environment {
      dev_mongo_url = credentials('DEV_MONGO_URL')
      stg_mongo_url = credentials('STG_MONGO_URL')
    }
  
    stages {
  
      stage('Env Setup') {
        steps {
  
          script
          {
          if (env.BRANCH_NAME == 'bugfix/*' || env.BRANCH_NAME == 'feature/*' || env.BRANCH_NAME =='development' || env.BRANCH_NAME =='hotfix/*') {
              env.environment = 'development' 
              echo 'Build & deployment to development environment'
              }
  
          if (env.BRANCH_NAME == 'staging'){
              env.environment = 'staging'
              echo 'Build & deployment to staging environment'
              
          }
    }
  }
  }   
  
      stage('Migration') {
        when {
          anyOf {
            branch 'development';
            branch 'feature/*';          
            branch 'staging'
           }
        }
        steps {
          echo "Installing the project NodeJS dependencies..."
          sh 'npm ci'
          echo "Executing the MongoDB migration script & displaying the status..."
          dir('db-migrations') {
            sh 'npm ci'
            sh('npm run migrate --silent -- --mongodb-url=$mongo_url')
            sh 'npm run migrate:status'
          }
  
        }
      }
  
      
      
  }
      }
  
  ```
  
  Within the above pipeline, I have the `Env Setup` stage, which assigns the `environment` type variable (development/staging) depending on the git branch. We have two MongoDB instances - one for development & one for the staging environment. I have defined both of the MongoDB server URL's under the `environment {}` block. Now, I want to dynamically connect to the dev/staging mongo servers in the `Migration` stage, depending on the environment type, and apply the migrations. I'm trying to automatically inject `$mongo_url` value from the `dev_mongo_url` & `stg_mongo_url` global variables depending on the environment. I'm stuck, and I have no clue how to achieve it.
  
  Any help?
  
  **Tags:** jenkins, jenkins-pipeline, jenkinsfile
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-11-07. Score: 1
  
  See the ["using environment variables dynamically" section of the official docs](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#setting-environment-variables-dynamically). I think this will answer your question. Here is the snippet of example code from those docs:
  
  ```
  pipeline {
      agent any 
      environment {
          // Using returnStdout
          CC = """${sh(
                  returnStdout: true,
                  script: 'echo "clang"'
              )}""" 
          // Using returnStatus
          EXIT_STATUS = """${sh(
                  returnStatus: true,
                  script: 'exit 1'
              )}"""
      }
      stages {
          stage('Example') {
              environment {
                  DEBUG_FLAGS = '-g'
              }
              steps {
                  sh 'printenv'
              }
          }
      }
  }
  
  ```
  
  So your code might end up looking something like this:
  
  ```
  pipeline {
      agent any 
      environment {
          mongo_url = """${
              switch(env.BRANCH_NAME) {
                  case 'bugfix/*'
                  case 'feature/*'
                  case 'development'
                  case 'hotfix/*'
                      'YOUR_DEV_MONGO_URL_HERE'
                      break
                  case 'staging'
                      'YOUR_STAGING_MONGO_URL_HERE'
                      break
              }"""
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: I have tried your example code. But it looks like I cannot use the `switch` statement in a declarative pipeline.  
  
  ---
  
  