title: How to pass dynamic values to an environment block during the pipeline execution in Jenkins?
body: <p>This is related to one question I asked before: <a href="https://stackoverflow.com/questions/61654487/using-groovy-to-parse-json-object-in-shell-scripts-for-jenkin">Using groovy to parse JSON object in shell scripts for Jenkin</a></p>
  
  <p>basically I will need to pass a dynamic value as returned from sh scripts to an environment block, so that the following stage can re-use that value and pass the version as a label to JIRA plugin called Xray. But I aware that I cannot pass dynamic values to an environment block during the pipeline execution. So, I think I am going to need try a different route for that, not sure if anyone could give me some tips please?</p>
  
  <pre><code>def setLatestAppVersionLabel() {
    def response = sh(script: "curl --silent ${APP_ARTIFACTORY_URL}/${XRAY_PLATFORM}/builds/latest.json", returnStdout: true).trim() as String
    def jsonResponse = readJSON text: response
    LATEST_VERSION = jsonResponse.id
    echo "LATEST_VERSION -&gt; ${LATEST_VERSION}"
  }
  </code></pre>
  
  <p>JSON response looks like that:</p>
  
  <pre><code>{"id":"1.0.0-6",
  "version":"1.0.0",
  "build":6,
  "tag":"android-v1.0.0-6",
  "commitHash":"5a78c4665xxxxxxxxxxe1b62c682f84",
  "dateCreated":"2020-03-02T08:11:29.912Z"}
  </code></pre>
  
  <p>and there is an environment block where I would like to pass the value to one of the variable defined there@</p>
  
  <pre><code>environment {
          AWS_DEFAULT_REGION = 'uk-xxx'
          XRAY_ENVIRONMENT = 'e2e'
          VERSION_KEY = 'id'
          XRAY_PLATFORM = 'Android'
          APP_ARTIFACTORY_URL = 'https://artifactory.example.com/mobile'
          LATEST_VERSION = ''
  }
  </code></pre>
  
  <p>If this path not working, what else could I use? Want to re-use the latest version taken from JSON response for the next stage in the pipeline to use.
  Next stage looks like this:</p>
  
  <pre><code>stage('Import Result to Xray') {
              when {
                  expression { return fileExists('xxx-executor/target/AndroidxxxxE2EResults/cucumber-reports/Cucumber.json')}
              }
              steps {
                  xrayResultsImport('xxx-executor/target/AndroidxxxxxE2EResults/cucumber-reports/Cucumber.json', 'xxx_ANDROID_E2E_xxxxxxx_Tests', XRAY_LABELS, ['E2E', 'Android', LATEST_VERSION], env.BUILD_URL)
              }
          }
  </code></pre>
  
  <p>Sorry I have to put xxxx to make this question general due to project confidentiality. </p>